Page 1: Myindex.html
<html>
<head>
<title></title>
<meta
http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form id
="myform" action="/myweb/SessionServlet">
<label> Username :
</label>
<input
type="text" name="uname" /><br>
<label> Password:
</label>
<input
type="password" name="pass" /></br>
<input
type="submit" value="submit"/>
</form>
</body>
</html>
Output :
Page 2: SessionServlet.java
import java.io.*;
import java.net.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
public class SessionServlet
extends HttpServlet
{
protected void
doGet(HttpServletRequest request, HttpServletResponse response)
throwsServletException,
IOException
{
try
{
response.setContentType("text/html");
PrintWriter out =
response.getWriter();
String n =
request.getParameter("uname");
out.print("welcome
--->"+n);
HttpSession session =
request.getSession();
session.setAttribute("uname",
n);
out.print("<a
href='SessionTrack'> Check Session Tracking</a> ");
out.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
protected void doPost(HttpServletRequest
request, HttpServletResponse response)
throwsServletException,
IOException
{
}
public String
getServletInfo() {
return "Short
description";
}
}
Output :
Page 3: SessionTrack.java
import java.io.*;
import java.net.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
public class SessionTrack
extends HttpServlet
{
protected void
doGet(HttpServletRequest request, HttpServletResponse response)
throwsServletException,
IOException
{
try
{
response.setContentType("text/html");
PrintWriter out =
response.getWriter();
HttpSession session =
request.getSession(false);
String my =
(String)session.getAttribute("uname");
out.print("hello
"+my);
out.close();
}catch(Exception e)
{
System.out.println(e);
}
}
public String
getServletInfo()
{
return "Short
description";
}
}
Output :
Page 4: web.xml
<?xml
version="1.0" encoding="UTF-8"?>
<web-app
version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>SessionServlet</servlet-name>
<servlet-class>SessionServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>SessionTrack</servlet-name>
<servlet-class>SessionTrack</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SessionServlet</servlet-name>
<url-pattern>/SessionServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SessionTrack</servlet-name>
<url-pattern>/SessionTrack</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
No comments:
Post a Comment