Showing posts with label servlet. Show all posts
Showing posts with label servlet. Show all posts

Friday, April 29, 2011

HTTP methods and the corresponding servlet methods

HTTP method       HttpServlet method

GET                        doGet()
HEAD                    doHead()
POST                     doPost()
PUT                       doPut()
DELETE                doDelete()
OPTIONS             doOptions()
TRACE                  doTrace()

Monday, May 3, 2010

Access file using JSP & Servlets

Given source code used for accessing file from server using

Servlets


String path = getServletContext( ).getRealPath ("xyz.xml" ) ;
System.out.println ( path ) ;
File file = new File ( path ) ;

JSP
String path = application.getRealPath ("xyz.xml" ) ;
System.out.println ( path ) ;
File file = new File ( path ) ;

In above jsp code, "application" is jsp implicit object.



Tuesday, April 13, 2010

Servlet Chaining

Servlet Chaining means the output of one servlet given as a input to another servlet. Servlet Aliasing allows us to invoke more than one servlet in sequence when the URL is opened with a common servlet alias. The output from first Servlet is sent as input to other Servlet and so on. The Output from the last Servlet is sent back to the browser. The entire process is called Servlet Chaining.

How to do Servlet Chaining in Servlet Programming?

Using include
RequestDispatcher rd = req.getRequestDispatcher("SecondServlet"); 
rd.include(request, response);

Using forward
RequestDispatcher rd = req.getRequestDispatcher("SecondServlet"); 
rd.forward(request, response);



Saturday, April 10, 2010

Difference between GET Method and POST Method

GET Method
  1. Target resource type is Active or passive
  2. Type of data is Text
  3. The amount of data handled by GET Method is not more than 255 characters.
  4. Data is part of the URL and is visible to the user in the URL field of browser.
  5. Data can be cached in the browser's URL History.
POST Method
  1. Target resource type is Active
  2. Type of data is Text as well as Binary
  3. The amount of data handled by POST Method is unlimited.
  4. Data is not a part of the URL and is sent as the request message body. It is not visible to the user in the URL field of browser.
  5. Data is not cached in the browser's URL History.

Friday, February 12, 2010

Servlet Life Cycle

Servlet Life Cycle Methods

The following are the life cycle methods of a servlet instance:
  • init()
  • service()
  • destroy()
We will look into the each method in detail.

init()

This method is called once for a servlet instance. When first time servlet is called, servlet container creates instance of that servlet and loaded into the memory. Future requests will be served by the same instance without creating the new instance. Servlet by default multithreaded application.init() method is used for inilializing servlet variables which are required to be passed from the deployment descriptor web.xml. ServletConfig is passed as the parameter to init() method which stores all the values configured in the web.xml. It is more convenient way to initialize the servlet.

service()

This method is called for the each request. This is the entry point for the every servlet request and here we have to write our businesslogic or any other processes. This method takes HttpServletRequest and HttpServletresponse as the parameters. It is not mandatory to write this method, normally developers are interested in writing doGet() or doPost() methods which is by default called from the service() method. If you override service(), it is your reponsibility to call the appropriate methods. If you are not overridden the service() method, based on the types of the request the methods will be called.

destroy()

This method will be called once for a instance. It is used for releasing any resources used by the servlet instance. Most of the times it could be database connections, Fill IO operations, etc. destroy() is called by the container when it is removing the instance from the servlet container. Servlet instance is deleted or garbage collected by the container only when the web server issues shut down or the instance is not used for a long time.