Who said that the Servlet compilation must inherit from the HttpServlet class and must implement doGet) or doPost). Isn't it possible to implement the Servlet interface?
In fact, there are three main methods to compile Servlet: one is to implement the Servlet interface, the other is to inherit the abstract class GenericServlet, and the third is to inherit the HttpServlet class. The following three methods are used to implement Servlet:
1) It is difficult to compile a Servlet from the Servlet interface, because all the methods in the Servlet interface must be implemented. The Servlet interface mainly defines five methods:
(1) void init (ServletConfig config) throws ServletException
(2) ServletConfig getServletConfig ()
(3) void service (ServletRequest req, ervletResponse res) throws ServletException, IOException
(4) String getServletInfo ()
(5) void destroy ()
The following describes how to compile a Servlet using the Servlet interface:
- Import javax. servlet .*;
- Import java. io .*;
- Public class TestServlet implements Servlet {
- Public void init (ServletConfig config) throws ServletException {
- System. out. println ("init ");
- }
- Public ServletConfig getServletConfig (){
- Reture null;
- }
- Public void service (ServletRequest req, ServletResponse res)
- Throws ServletException, IOException {
- // The content to be done after the request is implemented here
- PrintWriterOut=Response. GetWriter ();
- Out. println ("Hello World! ");
-
- }
- Public String getServletInfo (){
- Return null;
- }
- Public void destroy (){
- System. out. println ("destory ");
- }
- }
(2) inherit the abstract class GenericServlet to compile Serlvet, which is easier than implementing Servlet interfaces. The GenericServlet class only has one abstract method, namely serviceServletRequest req and ServletResponse res ), you only need to implement this method. The following is an example:
- import javax.servlet.*;
- import java.io.IOException;
-
- public TestGenericServlet extends GenericServlet{
- public abstract void service(ServletRequest req,ServletResponse res)
- throws ServletException,IOException{
- PrintWriter out = response.getWriter();
- out.println("Hello World!");
- }
- }
3) it should be the easiest and most common to inherit from the HttpServlet class to write Servlet. We generally need to write Servlet to directly inherit this class, rewrite doGet) or doPost) method. The following is an example:
- import javax.servlet.http.*;
- import javax.servlet.*;
- import java.io.*;
-
- public TestHttpServlet extends HttpServlet{
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws IOException, ServletException {
- response.setContentType("text/html;charset=gb2312");
- PrintWriter out = response.getWriter();
-
- out.println("<html>");
- out.println("<head>");
- out.println("<title>HelloWorld</title>");
- out.println("</head>");
- out.println("<body bgcolor=\"white\">");
- out.println("<hr>");
- out.println("HelloWorld");
- out.println("</body></html>");
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws IOException, ServletException {
- doGet(request, response);
- }
-
- }
- Scala Servlet in Scala Language
- Servlet API Discussion
- Introduction to Servlet containers and Context
- Servlet Source file to Class Process
- Details about Listener listening to Http sessions