Pattern Design:



CPAN423 Enterprise Java Programming

Lecture #2: More on Servlets

Tracking users using Sessions and Cookies:

HTTP is stateless protocol, meaning that the server doesn’t maintain contextual information about a client. The client opens a separate connection to the web server each time it needs to request a document from that server.

Cookies are textual information sent by the server to the client. The next time the client visit the same page, the cookies information are sent back to the server. The server can use this information to identify a user. The server can then perform different tasks, like customizing the website, store the user choices, or remember the user password.

Cookies have some limitations: In general browsers can accept 20 cookies per site and total of 300 cookies. The size of each cookie is limited to 4 kilobytes. Users can turn cookies off due to privacy issues; therefore the web application should not use cookies extensively, especially for sensitive information.

Servlets provide HttpSessin API interface that enable programmer to keep tracking of users using sessions. The session object is stored on the serve side and can help you to keep track of your web site visitors. Data can be put in the session and retrieved from it, much like a Hashtable.  A different set of data is kept for each visitor to the site.

Persistent Servlet State:

The ServletConetxt interface provide methods for storing persistence data that can be shared by all the Servlets and JSP files in the same web application. The methods setAttribute and getAttribute of this interface allows us to store and restore data that are associated with a specific key. Data stored in a ServletContext object are not tied to a particular user and are available through the method getServletContext of ServletConfig interface

In JSP the data stored in the ServletContext are available in the implicit object application.

Sending and Receiving Email with Servlet

The Java Mail API provides a group of classes and interfaces to send and receive email. To install the Mail API with Resin, you need to download the Mail API and the JAF from sun’s Java website. Unzip the two packages and store the jar files in the Resin-ee.x.x.x\lib folder. You need to restart the server for the changes to take effect.

Also you can use SmtpClient class from the package .smtp included in the JDK to send email using a valid SMTP server. See the Java documentation for details about the mail APIs.

Examples

Ex1:Session tracking example using Servlet

The user in this example must invoke an HTML form called idForm.html that has the following content:

Session track form

Type your name:

This form will be submitted to a servlet called SaveSession. The following configuration should be added to web.xml for this servlet:

SaveServletEx

SaveSessionServlet

/SaveSession

SaveServletEx

Following is the source code for SaveSessionServlet :

import javax.servlet.*;

import javax.servlet.http.*;

import java.util.*;

import java.io.*;

public class SaveSessionServlet extends HttpServlet

{

public void doPost (HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException

{

res.setContentType("text/html");

String name = req.getParameter( "username" );

/*

store the user name in the seesion object.

The user name is stored as a value for an attribute called theName

*/

HttpSession session= req.getSession(true);

session.setAttribute("theName",name);

PrintWriter out=res.getWriter();

out.println("click this "+session.getAttribute("theName")+"");

}

}

The SaveSessionServlet saves the user's name in the session, and puts a link to another servlet called NextServlet. The following configuration should be added to web.xml for this NextServlet:

NextServletEx

NextServlet

/NextServlet

NextServletEx

Following is the source code for NextServlet:

import javax.servlet.*;

import javax.servlet.http.*;

import java.util.*;

import java.io.*;

public class NextServlet extends HttpServlet

{

public void doGet (HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException

{

res.setContentType("text/html");

String name = req.getParameter( "username" );

/*

store the user name in the seesion object.

The user name is stored as a value for an attribute called theName

*/

HttpSession session= req.getSession(true);

PrintWriter out=res.getWriter();

out.println("Hello "+session.getAttribute("theName"));

}

}

Ex2:cookies example using Servlet

In this example we will use an HTML form called cookie.html to send user information to a servlet called cookieServlet. cookie.html has the following content:

Cookies Example

User name

User password

Store Cookie

 

The Servlet cookieServlet gets the user information and send them back as a cookie to the client. The next time the client request the cookieServlet, all the available cookies will be displayed as an HTML table.

import java.io.*;

import java.util.*;

import javax.servlet.http.*;

import javax.servlet.*;

public class cookieServlet extends HttpServlet {

Cookie[] myCookie;

public void doPost ( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException

{

res.setContentType("text/html");

PrintWriter pw = res.getWriter();

/*

get the user information and store them in a variables. Remember variable will store the value of the remeber check box.

*/

String name=req.getParameter("uname");

String password=req.getParameter("upassword");

String remember=req.getParameter("remember");

if (remember==null) remember="";

boolean found=false;

// get all the avaliabile cookies from the request object

myCookie=req.getCookies();

/*

check if the user information is already stored in the cookie

*/

if (myCookie!=null)

{

for(int i=0;i ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download