Your own Cookie!
In this post we will see how to create a simple web app which uses cookies to hold the user data (remember, in http ever request is a new request and cookie is a way to store the user data for subsequent requests to the server).
Refer Cookies in Servlet for information about cookies
Setup tomcat on your machine. (I have used apache-tomcat-8.0.15)
I am using eclipse for installation. Create a dynamic web project, Cookies
My final project structure looks like this:
Your web.xml file:
<web-app>
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>com.cookie.servlets.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
</web-app>
<-- You can deploy and check at this point -->
Next we create our next servlet, SecondServlet.java
package com.cookie.servlets;
import java.io.PrintWriter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SecondServlet extends HttpServlet{
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response){
try{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie ck[]=request.getCookies();
out.print("Hello "+ck[0].getValue());
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
Refer Cookies in Servlet for information about cookies
Setup tomcat on your machine. (I have used apache-tomcat-8.0.15)
I am using eclipse for installation. Create a dynamic web project, Cookies
My final project structure looks like this:
How to deploy our war file in tomcat.
Export project -> as WAR file -> save it as Cookies in the webapps folder of tomcat.
Start tomcat using startup.bat in bin folder.
Access http://localhost:8080/Cookies/
Create an index.html file:
<form action="servlet1" method="post">
Name : <input type="text" name="name">
<input type="submit" value="Go">
</form>
Create FirstServlet.java class.
package com.cookie.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FirstServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) {
try {
String name = request.getParameter("name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie c = new Cookie("name", name);
response.addCookie(c);
out.write("Hello " + name);
out.print("<form action='servlet2' method='post'>");
out.print("<input type='submit' value='go'>");
out.print("</form>");
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<web-app>
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>com.cookie.servlets.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
</web-app>
<-- You can deploy and check at this point -->
Next we create our next servlet, SecondServlet.java
package com.cookie.servlets;
import java.io.PrintWriter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SecondServlet extends HttpServlet{
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response){
try{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie ck[]=request.getCookies();
out.print("Hello "+ck[0].getValue());
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
Add the following to web.xml
<servlet>
<servlet-name>servlet2</servlet-name>
<servlet-class>com.cookie.servlets.SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
Your final web.xml looks like this:
<web-app>
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>com.cookie.servlets.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>servlet2</servlet-name>
<servlet-class>com.cookie.servlets.SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
</web-app>
Deploy and run.
Use cookie.setMaxAge(0) to delete the cookie information.
Refer Cookies in Servlet for information about cookies
Comments