|
|
IGNOU > IGNOU Assignments > MCA > MCA 2009 Assignments >Laboratory Course
IGNOU MCA Assignments
Question 1 Develop a web page using Servlet for session tracking which ask for your name and address and print a message of welcome along with the number of times you have visited the page. If you are visiting the page first time the message “Welcome! For the first visit” to be displayed on the page.
Ans.
Session.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Session tracker</title>
</head>
<body>
<form action="session_c" method="get">
<div align="center">Session Tracker</div>
<table align="center"><tr>
<td>Name: </td><td><input type="text" name="name"/></td>
</tr>
<tr>
<td valign="top">Address:</td><td> <textarea name="address" cols="30"
rows="5"></textarea></td>
</tr>
<tr><td colspan="2"><input type="submit" value="Submit"></td></tr>
</table>
</form>
</body>
</html>
session_c.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.sql.*;
public class session_c extends HttpServlet
{
public void service(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
String name = req.getParameter("name");
String add = req.getParameter("address");
HttpSession ses=req.getSession();
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
pw.println("<b>Name:</b>"+name+"<br>");
pw.println("<b>Address:</b>"+add+"<br>");
if(ses.isNew())
{
int con = 1;
ses.putValue("count", con);
pw.println("<b>Count:</b>"+con+"<br>");
}
else
{
String c = ses.getValue("count").toString();
int con1 = Integer.parseInt(c)+1;
ses.putValue("count", con1);
pw.println("<b>Count:</b>"+con1+"<br>");
}
}
}
  
|
|