Basic concept of session- store some data of client temporarily which should be available until client destroy it.

Session management definition- It’s a mechanism used by web container to save state of client.

Session management includes-
1- session (interface)
2- cookies(class)
3- url rewriting
4- Hidden form field

1-Session-
step 1- creation of sessions-

(i)HttpSession ses=request.getsession();

-Returns existing session object else creates new session object.

(ii)HttpSession ses=request.getsession(true);

-Always returns existing session object.

(iii)HttpSession ses=request.getsession(false);

-Always creates new session object.

step 2- Initialization of servlet-

ses.setAttribute(key, value);

Note- Remove Attribute-
ses.removeAttribute(key);

step 3- Access of session-

ses.getAttribute(key);

4- Deletion of session-

ses.invalidate ();

Example 7- Wep app to create session at correct login, get session data from at profile page, and then logout to delete session.

Output-ezgif-com-3239aa70ad

Project Structure-

prjstr

6 files-

1-index.html(inside WebContent)

2-Login.java(inside pacage=’ab’)

3-Profile.java(inside pacage=’ab’)

4-profile.html(inside WebContent)

5-Logout.java(inside pacage=’ab’)

6-web.xml(inside WEB-INF)

1-index.html


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="login">

<label>email</label><input name="e">
<label>password</label><input name="p">
<input type="submit">
<a href="profile">View Profile</a>
</form>

</body>
</html>

2-Login.java(servlet)


package ab;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;

public class Login extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String E1="mrjava@gmail.com";
String P1="java123";

String E2=request.getParameter("e");
String P2=request.getParameter("p");

if(E1.equals(E2)&amp;amp;amp;&amp;amp;amp;P1.equals(P2)){

HttpSession ses=request.getSession();
ses.setAttribute("mail", E2);
response.sendRedirect("profile");

}else{
response.sendRedirect("index.html");
}

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}

}

3-Profile.java(servlet)


package ab;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class Profile extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

PrintWriter out=response.getWriter();
HttpSession ses=request.getSession();
String m=(String) ses.getAttribute("mail");

if(m!=null){
response.sendRedirect("profile.html");
}else{
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request, response);
out.println("You are not logged in.");
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

}

4-profile.html


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

Welcome user
<a href="logout">Click here for logout</a>

</body>
</html>

5-Logout.java(servlet)


package ab;

import java.io.*;
import javax.servlet.http.*;

public class Logout extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

HttpSession ses=request.getSession();
ses.invalidate();
response.sendRedirect("index.html");
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

}

6-web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>ExSession</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>a</servlet-name>
<servlet-class>ab.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>b</servlet-name>
<servlet-class>ab.Profile</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>b</servlet-name>
<url-pattern>/profile</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>c</servlet-name>
<servlet-class>ab.Logout</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>c</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>

</web-app>

2- Cookies-

-cookie is a class.
-It stores textual data at client side.
-Less secure than session.

step 1- creation of cookie-
Cookie ck= new cookie(key, value);

step 2- send to the client side(browser)-
response. addCookie(ck);

step 3- access the cookie-
Cookie[] c= request.getCookies();

step 4- deletion of cookie-
ck.setAge(0);
response.addCookie(ck);

Example 8- Wep app to create cookie and add into browser at correct login, get cookies data from at profile page, and then logout to delete cookie.

output-ckks

Project Structure-

cks

6 files-

1-index.html(inside WebContent)

2-Login.java(inside pacage=’ab’)

3-Profile.java(inside pacage=’ab’)

4-profile.html(inside WebContent)

5-Logout.java(inside pacage=’ab’)

6-web.xml(inside WEB-INF)

1-index.html


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="login">

<label>email</label><input name="e">
<label>password</label><input name="p">
<input type="submit">
<a href="profile">View Profile</a>
</form>

</body>
</html>

2-Login.java(servlet)


package ab;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;

public class Login extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 String E1="mrjava@gmail.com";
 String P1="java123";

 String E2=request.getParameter("e");
 String P2=request.getParameter("p");

 if(E1.equals(E2)&amp;&amp;P1.equals(P2)){

 Cookie ck=new Cookie("mail", E2);
 response.addCookie(ck);
 response.sendRedirect("profile");

 }else{
 response.sendRedirect("index.html");
 }

 }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}

}

3-Profile.java(servlet)


package ab;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class Profile extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 PrintWriter out=response.getWriter();

 Cookie[] ck2=request.getCookies();
 System.out.println(ck2);

 if(ck2!=null){
 response.sendRedirect("profile.html");
 }else{
 RequestDispatcher rd=request.getRequestDispatcher("index.html");
 rd.include(request, response);
 out.println("You are not logged in.");
 }
 }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

}

4-profile.html


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

Welcome user
<a href="logout">Click here for logout</a>

</body>
</html>

5-Logout.java(servlet)


package ab;

import java.io.*;
import javax.servlet.http.*;

public class Logout extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

 Cookie[] ck2=request.getCookies();
 for (Cookie c : ck2) {
 c.setMaxAge(0);
 response.addCookie(c);
 }
 response.sendRedirect("index.html");
 }

6-web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>ExCookies</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>a</servlet-name>
<servlet-class>ab.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>b</servlet-name>
<servlet-class>ab.Profile</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>b</servlet-name>
<url-pattern>/profile</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>c</servlet-name>
<servlet-class>ab.Logout</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>c</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
</web-app>

3- Url Rewriting-
Step 1- add “?” At the end of url
Step 2 -add data in following format-
Attribute=value
Step 3- separate these attribute and value with “&” symbol

Example 9- Web app to add data with url and get it on next servlet.

output-

uul

Project Structure-

urrl

3 files-

1-index.html(inside WebContent)

2-A.java(inside pacage=’ab’)

3-web.xml(inside WEB-INF)

1-index.html


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<a href="first?name=mr.Java&email=a@a.com">Click here</a>

</body>
</html>

2-A.java(servlet)


package ab;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;
public class A extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String s1=request.getParameter("name");
String s2=request.getParameter("email");

PrintWriter out=response.getWriter();
out.println(s1);
out.println(s2);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}

}

 

3-web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>ExUrlWr</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>z</servlet-name>
<servlet-class>ab.A</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>z</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>
</web-app>

4- hidden form field-
Step1- create input box with type=hidden
Step2- assign it’s value
Step3- get value of input box on next servlet.

Example 10- Wep app get data from hidden input box on next servlet.

output-

hidd

Project Structure-

exhid

3 files-

1-index.html(inside WebContent)

2-A.java(inside pacage=’ab’)

3-web.xml(inside WEB-INF)

1-index.html


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="first">
<input name="a">

<input name="b">

<input type="hidden" name="c" value="Mr.java">
<input type="submit">
</form>

</body>
</html>

2-A.java(servlet)


package ab;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;
public class A extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String s1=request.getParameter("a");
String s2=request.getParameter("b");
String s3=request.getParameter("c");

PrintWriter out=response.getWriter();
out.println(s1);
out.println(s2);
out.println(s3);

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}

}

3-web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>ExHidden</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>a</servlet-name>
<servlet-class>ab.A</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>
</web-app>

 

Advertisements