Struktura servletu
init()
service()
destroy()
Servlet - příklad
package prg;import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorldServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html;charset=windows-1250");
PrintWriter out = res.getWriter();
out.println("<HTML><HEAD><TITLE>Hello World!</TITLE>"+
"</HEAD><BODY><H2>Hello World!</H2></BODY></HTML>");
out.println("<hr><em>"+getServletInfo()+"</em>");
out.close();
}
public String getServletInfo() {
return "HelloWorldServlet 1.0";
}
}
Servlet – příklad – web.xml
< ?xml version="1.0" encoding="ISO-8859-2"? >
< !DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
< web-app >
< servlet >
< servlet-name>Hello</servlet-name >
< servlet-class >prg.HelloWorldServlet</ servlet-class >
< /servlet >
< servlet-mapping >
< servlet-name >Hello< /servlet-name >
< url-pattern >/myHello< /url-pattern >
< /servlet-mapping >
< /web-app >
Autor: Filip Koval