2:01 pm - Tuesday June 27, 2017

httpSession in Servlet

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

 Http Session:

HttpSession is also known as server side session,  session information will be saved in a web container inself. Since session are stored in web container it can be used for secure data.

To enable session in web container:


HttpSession sesssion=req.getSession(true);

True

New session will be created for every request

False

Old session will be reused

To save a session object:

 session.setAttribute(“key”,”value”);

To retrieve session object:

Object value = session.getAttribute(“key”);

  Project Structure:

 

 

 

 

 

 

 

 

Server.java:


package com.candid;

import java.io.PrintWriter;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class Server
*/
public class Server extends HttpServlet {
private int sercount = 0;
private static final long serialVersionUID = 1L;

/**
* Default constructor.
*/
public Server() {
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
HttpSession ses = req.getSession(true);
int i = 0;

Integer t = (Integer) ses.getAttribute("cc");
if (t != null) {
i = t.intValue() + 1;
}
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
pw.println("<body bgcolor=pink>");
pw.println("<html>");
pw.println("<font color=blue size=7><center>you hit this browser" + i
+ "time(s) and you hit server" + (++sercount)
+ "time(s)</font></center>");
ses.setAttribute("cc", new Integer(i));
pw.println("</html>");
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}

web.xml:


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Server</display-name>
<servlet>
<description>
</description>
<display-name>Server</display-name>
<servlet-name>Server</servlet-name>
<servlet-class>com.candid.Server</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Server</servlet-name>
<url-pattern>/Server</url-pattern>
</servlet-mapping>
<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>
</web-app>

OUTPUT:

DOWNLOAD+SOURCE CODE                                                                                                                                                       BACK

 

 

Filed in: Z-A Servlet codes

No comments yet.

Leave a Reply