Loading
View RSS Feed

Angad Kumar Shukla's blog

Cookie Session Implementation Code in J2EE

Rating: 93 votes, 5.00 average.
Home.html

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="CookieServlet">
Enter user name:<input type="text" name="t1"></input><br/>
Enter password:<input type="password" name="t2"></input><br/>
<input type="submit" name="sub" value="!Go"></input>
</form>
</body>
</html>

CookieServlet.java

Code:
package controller;

import java.io.*;
import javax.servlet.http.*;
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 CookieServlet
 */
public class CookieServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CookieServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session=request.getSession(true);
		int pageNo=1;
		Cookie cookies[]=request.getCookies();
		PrintWriter pr=response.getWriter();
		boolean flag=false;
		String uname=request.getParameter("t1");
		if(session.getAttribute("pno")!=null){
			pageNo=Integer.parseInt((String)session.getAttribute("pno"));
			pageNo++;
			session.setAttribute("pno",pageNo+"");
		}
		else
			session.setAttribute("pno",pageNo+"");
		if(cookies!=null)
			for(int index=0;index<cookies.length;index++)
				if(uname.equals(cookies[index].getValue())){
					flag=true;
					break;
				}
		if(flag)
			pr.print("<html><body>"+session.getId()+"<h1>Welcome back</h1><br/><h3>Page no is:"+pageNo+"</h3><br/><a href=\"SessionServlet\">Back</a></body></html>");
		else{
			Cookie newCookie=new Cookie("name"+pageNo,uname);
			newCookie.setMaxAge(3600);
			response.addCookie(newCookie);
			System.out.print(uname+"\n");
			pr.print("<html><body>"+session.getId()+"<h1>New user</h1><br/><h3>Page no is:"+pageNo+"</h3><br/><a href=\"SessionServlet\">Back</a></body></html>");
		}
	}

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

}
SessionServlet.java

Code:
package controller;

import java.io.*;
import javax.servlet.http.*;
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 SessionServlet
 */
public class SessionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SessionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session=request.getSession(true);
		PrintWriter pr=response.getWriter();
		pr.print("<html><body>"+session.getId()+"<br/>"+session.getAttribute("pno")+"<a href=\"Home.html\">home</a></body></html>");
	}

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

}

Submit "Cookie Session Implementation Code in J2EE" to Digg Submit "Cookie Session Implementation Code in J2EE" to del.icio.us Submit "Cookie Session Implementation Code in J2EE" to StumbleUpon Submit "Cookie Session Implementation Code in J2EE" to Google

Categories
Java , Web Design

Comments




Disclaimer: Users of techforum4u.com are responsible for ensuring that any material they post (article, blog posts, images or other mulitimedia content) does not violate or infringe upon the copyright, patent, trademark, or any personal or proprietary rights of any third party, and is posted with the permission of the owner of such rights.Anyone who violates these rules may have their access privileges removed without warning.