java2db

How to create a servlet , approaches to create a servlet



There are three approaches to create a servlet.

1. Create a java class that implements javax.servlet.Servlet.

2 Create a java class that extends javax.servlet.GenericServlet.

3 Create a java class that extends javax.servlet.HttpServlet.

 
create a servletCreate a java class that implements javax.servlet.Servlet interface and implement all the 5 methods.

create a servlet The list of methods in javax.servlet.Servlet interface .

 Return Type  Method
void  destroy()
ServletConfig  getServletConfig()
java.lang.String  getServletInfo()
void  init(ServletConfig config)
void  service(ServletRequest req, ServletResponse res)

 

create a servlet Example to create a servlet from javax.servlet.Servlet interface .

 

create a servletCreate a java class that extends javax.servlet.GenericServlet (abstract) class and implement the abstract void service(ServletRequest req, ServletResponse res) method.

create a servlet Example to create a servlet from javax.servlet.GenericServlet interface .

 

 

create a servletCreate a java class that extends javax.servlet.HttpServlet (abstract) class and override one of the two service(-,-) methods or one of the seven doXxx(-,-) methos.

 

create a servlet The list of methods form javax.servlet.HttpServlet class .

  Return Type  Method
protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
protected void doHead(HttpServletRequest req, HttpServletResponse resp)
protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
protected void doPut(HttpServletRequest req, HttpServletResponse resp)
protected void doTrace(HttpServletRequest req, HttpServletResponse resp)
protected void getLastModified(HttpServletRequest req)
protected void service(HttpServletRequest req, HttpServletResponse resp)
void service(ServletRequest req, ServletResponse res)

 

create a servlet Example to create a servlet from javax.servlet.HttpServlet interface .

create a servletIt is recommended to create a servlet that extends Javax.servlet.HttpServlet, because the web servers are designed based on http protocol.



Related Posts :
Kill or invalidate session using session id ( concurrent login )
Servlet Filter ( Request and Response filters in servlets ) example
URL rewriting in servlets ( Servlet session tracking )
Servlet Cookies ( Servlets session tracking with cookie )
Hidden form field in servlets ( Servlet session tracking )
Request attributes in servlet with an example ( Attribute scope )
Session attributes in servlet with example (Attribute scope)
ServletConext Attributes in servlet ( Attribute scope )
   [will not be published]



^ <