Step8- Session Management + code

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






Insert title here





email
password

<a href="profile">View Profile</a>





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;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






Insert title here



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




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




ExSession

index.html
index.htm
index.jsp
default.html
default.htm
default.jsp



a
ab.Login


a
/login



b
ab.Profile


b
/profile



c
ab.Logout


c
/logout



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






Insert title here





email
password

<a href="profile">View Profile</a>




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)&&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






Insert title here



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



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




ExCookies

index.html
index.htm
index.jsp
default.html
default.htm
default.jsp



a
ab.Login


a
/login



b
ab.Profile


b
/profile



c
ab.Logout


c
/logout


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






Insert title here



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



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




ExUrlWr

index.html
index.htm
index.jsp
default.html
default.htm
default.jsp



z
ab.A


z
/first


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






Insert title here














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




ExHidden

index.html
index.htm
index.jsp
default.html
default.htm
default.jsp



a
ab.A


a
/first


 

Advertisements