Monday, May 17, 2010

Cookie

A cookie is nothing more than a little piece of data (a name/value String pair) exchanged between the client and server. The server sends the cookie to the client, and the client returns the cookie when the client makes another request

Simple Custom Cookie Example

Step 1:Create a java class named "CookieTest " under src folder---> Servlet that creates and SETS the cookie

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class CookieTest extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType(“text/html”);
String name = request.getParameter(“username”);
Cookie cookie = new Cookie(“username”, name);
cookie.setMaxAge(30*60);
response.addCookie(cookie);
RequestDispatcher view = request.getRequestDispatcher(“cookieresult.jsp”); view.forward(request, response);
} }

Step 2: Create a new JSP file named "cookieresult.jsp under WEB-INF folder --->JSP to render the view from this servlet.

< html >< body >
 < a href=”checkcookie.do” >click here< /a >
< /body >< /html >

Step 3: Create a java class named "CheckCookie" under src folder---> Servlet that GETS the cookie.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class CheckCookie extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
Cookie[] cookies = request.getCookies();
if ( cookies != null ) {
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookie.getName().equals(“username”)) {
String userName = cookie.getValue(); out.println(“Hello “ + userName);
 break;
} } }} }

 Step 4 : Create a html file  named "Form.html" under WEB-INF folder---> Client to select username

< html >
< head >< meta http-equiv="Content-Type" content="text/html; " >
< title >User Selection< /title >
< /head >
< body >
< h1 align="center" >UserName SelectionPage< /h1 >
< form method="POST" action="CookieTest " >
Select any user< p >
User:
< select name="user" size="1" >
< option value="John" > John < /option >
< option value="Antony" > Antony< /option >
< option value="Rocky" > Rocky < /option >
< option value="Nicey" > Nicey< /option >
< /select >< br >< br >
< center >< input type="SUBMIT" >< /center >
< /form >< /body >< /html >

STEP 5: Include below code in a Xml file “web.xml” under WEB-INF folder.

<  web-app >
< welcome-file-list >
< welcome-file >Form.html< /welcome-file >
< welcome-file >cookieresult.jsp < /welcome-file >
< /welcome-file-list >
< servlet >
< servlet-name >CookieTest < /servlet-name >
< servlet-class >com.examp.CookieTest < /servlet-class >
< /servlet >
< servlet-mapping >
< servlet-name >CookieTest < /servlet-name >
< url-pattern >/CookieTest < /url-pattern >
< /servlet-mapping >
< servlet >
< servlet-name >CheckCookie< /servlet-name >
< servlet-class >com.examp.CheckCookie < /servlet-class >
< /servlet >
< servlet-mapping >
< servlet-name >CheckCookie< /servlet-name >
< url-pattern >/CheckCookie< /url-pattern >
< /servlet-mapping >
< /web-app >

STEP 6: Export the project” FirstSample” into a war file named ” FirstSample.war” and place it in the deploy folder of JBOSS server.

STEP 7: To see the output use this url ” http://localhost:8080/ FirstSample /Form.html”

No comments:

Post a Comment