Ex 6a-6b Session Track WE LAB
Ex 6a-6b Session Track WE LAB
DATE:
AIM:
To write an html program using various form elements for invoking Servlet from html.
ALGORITHM:
1. In html program, define the html, head and title tag. The title is Student Information
Form and closes the title and head tag.
2. Define the body tag to create form and table simultaneously.
3. The table consists of following information Roll no, Student name, Address, Phone no
and total marks.
4. In the Servlet program, import the summary package and create a own servlet class
extends with generic Servlet.
5. In the service method, define request and response.
6. Create an object for printwriter() and getwriter() value.
7. The enumeration object gets the Servlet request parameter.
8. Create objects for string method and it is displayed another object value received get
parameter of name received and displayed the value received value.
PROGRAM:
FILE NAME: invokeServlet.html
<html> <head>
<title>Student's Information</title>
</head> <body>
<center>
<form name="f" action="http://localhost:8080/examples/servlet/ServletDemo">
<h3>Enter Student's Info in the following Table </h3>
<table border="1">
<tr>
<td>Roll_no</td>
<td><input type="text" name="Roll Number" value="" size="25" /> </td>
</tr>
<tr>
<td>Students Name</td>
<td> <input type="text" name="Students Name" value="" size="25" /> </td>
</tr>
<tr>
<td>Student Address</td>
<td> <input type="text" name="Student Address" value="" size="25" /> </td>
</tr>
<tr>
<td>Phone</td>
<td> <input type="text" name="Phone" value="" size="25" /> </td>
</tr>
<tr>
<td>Total Marks</td>
<td> <input type="text" name="Total Marks" value="" size="25" /> </td>
</tr>
</table>
<input type="submit" value="Submit" />
</form>
</center>
</body>
</html>
FILE NAME: ServletDemo.java
import java.util.*;
import java.io.*;
import javax.servlet.*;
public class ServletDemo extends GenericServlet
{
public void service(ServletRequest req,ServletResponse res)throws ServletException
,IOException
{
PrintWriter out =res.getWriter();
Enumeration en=req.getParameterNames();
while(en.hasMoreElements())
{
String name_received=(String)en.nextElement();
out.print("\n"+name_received+" =");
String value_received= req.getParameter(name_received);
out.println(value_received);
out.println(" ");
}
out.close();
}
}
OUTPUT:
//Student information to be entered and submit
//After submit button is clicked,
RESULT:
Thus the java program to invoke Servlets from HTML forms was successfully
implemented.
EX: NO: 6(b) Session Tracking - Hit Count
DATE:
AIM:
To write a java program for session tracking to display the number of visits of
a specific web page.
ALGORITHM:
1. Create HttpSession object associated with the current client.
2. Using methods for the HttpSession object, retrieve information like creation
time, last access time.
3. Declare an integer object bound to a name “visitCount”.
4. Check whether the session is new or already exists. If new, assign visitCount=0
else increment the value in visitCount by 1.
5. Display all the name/value pairs of session information.
PROGRAM:
//SessionTracking.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class SessionTracking extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
HttpSession session = request.getSession(true);
Date createTime = new Date(session.getCreationTime());
Date lastAccessTime = new
Date(session.getLastAccessedTime());
String title = "Welcome to my website";
Integer visitCount = new Integer(0);
String visitCountKey = new String("visitCount");
String userIDKey = new String("userID");
String userID = new String("abc");
if (session.isNew()){
title = "Welcome to my website";
session.setAttribute(userIDKey, userID);
}
else
{
visitCount = (Integer)session.getAttribute(visitCountKey);
visitCount = visitCount + 1;
userID = (String)session.getAttribute(userIDKey);
}
session.setAttribute(visitCountKey, visitCount);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType="<!doctype html public \"-//w3c//dtd html 4.0 " +
" transitional //en \" > ";
out.println(docType + "<html>\n" +
"<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<h2 align=\"center\">Session Infomation</h2>\n" + "<table border=\"1\"
align=\"center\">\n" + "<tr bgcolor=\"#949494\">\n" +
" <th>Session info</th><th>value</th></tr>\n" + "<tr>\n" +
" <td>id</td>\n" + " <td>" + session.getId() + "</td></tr>\n" + "<tr>\n" +
" <td>Creation Time</td>\n" + " <td>" + createTime +
" </td></tr>\n" + "<tr>\n" + " <td>Time of Last Access</td>\n" + " <td>" +
lastAccessTime " </td></tr>\n" + "<tr>\n" + " <td>User ID</td>\n" + " <td>"
+ userID + " </td></tr>\n" + "<tr>\n" + " <td>Number of visits</td>\n" +
" <td>" + visitCount + "</td></tr>\n" + "</table>\n" + "</body></html>");
}
}
web.xml
<servlet>
<servlet-name>Session</servlet-name>
<servlet-class>SessionTracking</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Session</servlet-name>
<url-pattern>/SessionTrack</url-pattern>
</servlet-mapping>
OUTPUT:
RESULT:
Thus the java program for session tracking to display the number of visits of a
specific web page was implemented successfully.