Wednesday 4 September 2013

Session Tracking - Cookies





Maintaining the sessions: there are 3 ways

1. Cookies
2. URL rewriting
3.


Session Tracking - Cookies




Cookies

Now we have seen that using the session id concepts: The server is able to maintain and manage sessions across multiple requests.
But how exactly the session id information is passed to the client and again inturn the session id sent back to the server right
There has to be some means the session id should be passed between the client and the server inorder to maintain the session
There are basically 3 ways which we can do it: One of the ways is through cookies
So Basically what happens is, the server creates a cookie for the client and sends across cookie to the client
Cookies are basically they are small files which are stored on the client machine. So this is the physical file actually some where in the client machine
Cookies will contain information about the client and everytime the client will send a request to the server and cookie information is also sent to the server from the client
Basically it sends the request and sends back to the client.

Example program for Cookies:

Steps:
1. create any folder (ex: cookiesex)
2. maintain the folder structure
3. set the classpath for tomcat server
4. deploy your application(your folder ex" cookiesex) in webapps folder under Tomcat server
5. start the server
6. execute your application in any browser


1. create any folder (ex: cookiesex)
2. maintain the folder structure
   1.  Home.html
   2. AddCookiesServlet.java
   3. GetCookiesServlet.java
   4.  create WEB-INF folder
         Under WEB-INF folder
        (a)  create classes folder
        (b)  create lib folder(optional)
        (c)  create web.xml file for configuration

   1.  Home.html
<!--Home.html-->
<html><body>
<form action="addcookie" method="get"> 
<pre>
Name : <input type="text" name="cname"/>
Value : <input type="text" name="cvalue"/>
<input type="submit" value="Add Cookie"/>
</pre>
</form></body></html>

2. AddCookiesServlet.java

package com.rajendra.servlet;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;

public class AddCookieServlet extends HttpServlet {

public void service (HttpServletRequest req, HttpServletResponse res) 
throws ServletException, IOException {

String name=req.getParameter("cname");
String value=req.getParameter("cvalue");

Cookie c=new Cookie(name,value);

res.addCookie(c);

res.setContentType("text/html");
PrintWriter out=res.getWriter();

String output="<html><body>";
output+="Cookie Added Successfully <br/>";
output+="<a href='index.jsp'>Add One More</a><br/>";
output+="<a href='viewcookies'>View Cookies</a>";
output+="</body></html>";

out.println(output);
}//service
}//class

3. GetCookiesServlet.java

//GetCookieServlet
package com.santosh.servlet;

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

public class GetCookieServlet extends HttpServlet {

public void service (HttpServletRequest req, HttpServletResponse res) 
throws ServletException, IOException {

res.setContentType("text/html");
PrintWriter out=res.getWriter();

out.println("<html><body><table border=1>");
out.println("<tr><th>Name</th><th>Value</th></tr>");

Cookie[] c=req.getCookies();
if (c!=null){
for (int i=0;i<c.length;i++){
out.println("<tr><td>"+c[i].getName()+"</td>");
out.println("<td>"+c[i].getValue()+"</td></tr>");
}//for
}//if
out.println("</table><br/><br/>");
out.println("<a href='Home.html'>Add One More</a><br/>");
out.println("</body></html>");
}//service
}//class

1. set the classpath
D:\\cookiesex>set CLASSPATH=C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar ;.;
2. Now compil e the servlet(java) programs to get the class files
D:\\cookiesex>javac -d . AddCookieServlet.java
D:\\cookiesex>javac -d . GetCookieServlet.java
3. Now copy the class files of AddCookieServlet.class, GetCookieServlet.class
4. paste into classes folder of WEB-INF folder

(c)  create web.xml file for configuration under WEB-INF folder
<!--web.xml-->
<web-app>
<servlet>
<servlet-name>ts1</servlet-name>
<servlet-class>com.rajendra.servlet.AddCookieServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>ts2</servlet-name>
<servlet-class>com.rajendra.servlet.GetCookieServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ts1</servlet-name>
<url-pattern>/addcookie</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ts2</servlet-name>
<url-pattern>/viewcookies</url-pattern>
</servlet-mapping>
</web-app>
4. deploy your application(your folder ex" cookiesex) in webapps folder under Tomcat server

  steps for deploying  your application
 1. you can create war file also to deploy
preparing war file using cmd prompt

D:\\cookiesex>jar -cvf  cookiesex.war Home.html WEB-INF
(6) start the tomcat server
(7) deploy the servlet(servlet folder or war file into the webapps folder of Tomcat
(or) paste the ur application (ex: cookiesex.war) into the webapps folder of tomcat
(8)runing the servlet program
http://localhost:8888/cookiesex/Home.html

9) enter the name and value

10) click on Add  cookie button

11) click on viewcookies


3. hidden parameters ( this is in jsp)
Session Tracking - Cookies(
http://raj-servlets.blogspot.in/2013/09/session-tracking-cookies.html)

No comments:

Post a Comment