AdvanceServlet PDF
AdvanceServlet PDF
AdvanceServlet PDF
RequestDispatcher in Servlet
It can work within the server only. It can be used within and outside the
server.
Example: Example:
request.getRequestDispacher("servlet response.sendRedirect("servlet2");
2").forward(request,response);
Session Tracking in Servlets
• Session simply means a particular interval of time.
• Session Tracking is a way to maintain state (data) of an user. It is also
known as session management in servlet.
• Http protocol is a stateless so we need to maintain state using session
tracking techniques. Each time user requests to the server, server
treats the request as the new request. So we need to maintain the
state of an user to recognize to particular user.
• HTTP is stateless that means each request is considered as the new
request
Session Tracking is used to recognize a
particular user
Session Tracking Techniques
Constructor Description
Method Description
public void setMaxAge(int expiry) Sets the maximum age of the cookie in
seconds.
public String getName() Returns the name of the cookie. The name
cannot be changed after creation.
• Cookie ck[]=request.getCookies();
• for(int i=0;i<ck.length;i++){
out.print("<br>"+ck[i].getName()+" "+ck[i].getValue());
//printing name and value of cookie
}
example
Hidden Form Field
• In case of Hidden Form Field a hidden (invisible) textfield is used for
maintaining the state of an user.
• In such case, we store the information in the hidden field and get it
from another servlet. This approach is better if we have to submit
form in all the pages and we don't want to depend on the browser.