Following is a list of topics we'll cover in this article :
* Overview ( this page )
* Servlet Life Cycle
* HttpServlet Class
* ServletConfig Class
* ServletContext Class
* Application Initialization Parameters
* Summary
Servlet Life Cycle====
Servlets are normal Java classes which are created when needed and destroyed when not needed. Since Servlets run within a Servlet Container, creation and destruction of Servlets is the duty of Servlet Container and not yours. Implementing the init() and destory() methods of Servlet interface allows you to be told by the Servlet Container that when it has created an instance of your Servlet and when it has destroyed that instance. An important point to remember is that your Servlet is not created and destroyed for every request it receives, rather it is created and kept in memory where requests are forwarded to it and your Servlet then generates response.
Servlet Initialization
So you have created your Servlet class by extending the HttpServlet class and have placed it in /WEB-INF/classes/ directory of your application. Now when will Servlet Container create an instance of your Servlet? there are two situations :
* When you have specifically told the Servlet Container to preload your Servlet when the Servlet Container starts by setting the load-on-startup tag to a non-zero value e.g.
So when the Servlet Container starts it will preload your Servlet in the memory.
* If your Servlet is not already loaded, it's instance will be created as soon as a request is received for it by the Servlet Container.
During the loading of the Servlet into the memory, Servlet Container will call your Servlet's init() method. Since init() is going to be called only once you can put Servlet initialization code in it like getting hold of a database connection or whatever.
Responding to Requests
Once your Servlet is initialized and it's init() method called, any request that the Servlet Container receives will be forwarded to your Servlet's service() method. HttpServlet class breakes this service() method into more useful doGet(), doPost(), doDelete(), doOptions(), doPut() and doTrace() methods depending on the type of HTTP request it receives. So in order to generate respose you should override the doGet() or doPost() method as required.
At this moment all the requests will be forwarded to the appropriate doGet() or doPost() or whatever method as required. No new instance will be created for your Servlet.
Servlet Destruction
When your application is stopped or Servlet Container shuts down, your Servlet's destroy() method will be called. This allows you to free any resources you may have got hold of in your Servlet's init() method.
Always Remember
init() and destroy() methods will be called only once during the life time of your Servlet while service() and it's broken down methods ( doGet(), doPost() etc ) will be called as many times as requests are received for them by the Servlet Container.
We have now finished learning about Servlet life cycle and we now know about init() , service() and destroy() methods. We can now move on to learn more about HttpServlet class which almost all of your Servlets will extend.
HttpServlet Class
We know that a Servlet is a simple Java class which must implement javax.servlet.Servlet interface. GenericServlet class provides a default implementation of this interface so that we don't have to implement every method of it. HttpServlet class extends GenericServlet to provide an HTTP protocol specific implementation of Servlet interface. If your Servlet has to work with HTTP protocol you should simply extend HttpServlet class and override the methods you need.
Examining Methods of HttpServlet class
Let's examine the methods which this class provides one by one :
* init()Called only once during the initialization of the Servlet.
* destroy()Called only once when Servlet instance is about to be destroyed.
* service()Do not override this method.
* doGet(), doPost(), doPut(), doDelete(), doOptions, doTrace()These methods are called according to the type of HTTP request received. Override them to generate your own response.
* log()Writes messages to the Servlet's log files.
* getLastModified()Override this method to return your Servlet's last modified date.
* getServletInfo()Override this method to provide a String of general info about your Servlet such author, version, copyright etc.
* getServletName()Override this method to return name of the Servlet.
* getInitParameter(), getInitParameterNames()First one returns value of given initialization parameter, second one returns an Enumeration object containing names of all initialization parameters provided. We have already discussed initialization parameters in our earlier article.
When you invoke a Servlet, the servlet engine passes the information on to the Servlets
service() method. This method determines the type of request made (GET, POST,
HEAD, ...) and calls the function doTYPE, like doGet, doPost. GET and POST just
differ in the way form data is sent from the browser to the server. The method doGet
handles data that has been attached to the url in the form url questionmark
(name=value ampersand)+. Typically in a CGI you would have to read the environment
variable QUERY_STRING to get this string of concatenated parameternames and
values. With the doPost method, form data comes in through standard input stream,
a cgi would just need to open the input stream and read until EOF to get the form data.
With Servlets, you don't need to read in the concatenated string of parameternames
and values. The parsing is all done behind the scenes. You just need to call
getParameter regardless of how the form data is actually sent in.
package wellho;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.PrintWriter;
import java.io.IOException;
public class demolet extends HttpServlet implements SingleThreadModel
{
private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
/**
* Process the HTTP doGet request.
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String var0show = "";
try
{
var0show = request.getParameter("showthis");
}
catch(Exception e)
{
e.printStackTrace();
}
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("msg in html tags");// use html tags
out.close();
}
/**
* Process the HTTP doPost request.
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String var0show = "";
try
{
var0show = request.getParameter("showthis");
}
catch(Exception e)
{
e.printStackTrace();
}
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("msg in html tag");// use html tags
out.close();
}
}
0 comments:
Post a Comment