Skip to main content

Posts

Showing posts from July 10, 2013

Managing Session using Servlet session API

You can use the classes and interfaces defined in the Servlet Session API to create and manage user sessions. Various interfaces provided by Servlet Session API to create and manage user session are, javax.servlet.http.HttpSession, javax.servlet.http.HttpSessionListener, and javax.servlet.http.HttpSessionBindingListener. The javax.servlet.http.HttpSession interface provides methods for tracking the session of a user. You can create an object of HttpSession interface to store session information as name/value pairs. You can later retrieve this information to manage user sessions. httpservlet.html <!-- To change this template, choose Tools | Templates and open the template in the editor. --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > <html> <head> <title></title> <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > </head> <bod

Managing Session using Cookies

Cookies are small text files that are stored by an application server in the client browser to keep track of all the users. A cookie has values in the form of name/value pairs. For example, a cookie can have a name, user with the value, Michael. They are created by the server and are sent to the client with the HTTP response headers. The client saves the cookies in the local hard disk and sends them along with the HTTP request headers to the server. A Web browser is expected to support 20 cookies per host and the size of each cookie can be a maximum of 4 bytes each. Various characteristics of cookies are: Coockie.html <!-- To change this template, choose Tools | Templates and open the template in the editor. --> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > < html > < head > < title ></ title > < meta http-equiv ="Content-Type" content ="text/html;

Managing Session using URL rewriting

URL rewriting is a session management technique that manages user session by modifying a URL. Usually, this technique is used when information that is to be transferred is not very critical because the URL can be intercepted easily during transfer. For example, in an online shopping portal, a servlet can modify a URL to include user information, such as a username. The servlet can then display the URL. When the user clicks the URL hyperlink. The information is sent to another servlet that retrieves the user information and displays a welcome message. You can use the following code to create the servlet, RewriteServletURL that modifies and displays a URL. urlRewriting .html <!-- To change this template, choose Tools | Templates and open the template in the editor. --> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > < html > < head > < title ></ title > < meta

Managing Session using Hidden form field

You can use hidden form fields to maintain the session information of a user while the user interacts with the Web application. A hidden form field is embedded in an HTML page and is not visible when viewed in a browser. The following code snippet shows a hidden form field in an HTML page: Programs: HiddenForm.html <!-- To change this template, choose Tools | Templates and open the template in the editor. --> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > < html > < head > < title ></ title > < meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" > </ head > < body > < form action ="FirstServlet1" method ="post" > Name: < input type ="text" name ="uname" />< br /> < input type ="submit" value ="go" /> </ form > </ body > &l