Program to demonstrate data stored in cookies being shared across web-app.
1. Open Link: http://localhost:8080/Excercise/view.html
2. Fill in user name and password.
3. Select the items to shop.
4. 3rd page displays the uesr name and password(though unlikely in real world) stored from cookies and List of items to be shopped.
web.xml(To be stored in folder called apache-tomcat/webapps/Excercise/WEB-INF in webapps):
<web-app ...>
<servlet>
<servlet-name>cookie1</servlet-name>
<servlet-class>pack1.work.Controller.NameStore</servlet-class>
</servlet>
<servlet>
<servlet-name>cookie2</servlet-name>
<servlet-class>pack1.work.Controller.ItemStore</servlet-class>
</servlet>
1. Open Link: http://localhost:8080/Excercise/view.html
2. Fill in user name and password.
3. Select the items to shop.
4. 3rd page displays the uesr name and password(though unlikely in real world) stored from cookies and List of items to be shopped.
web.xml(To be stored in folder called apache-tomcat/webapps/Excercise/WEB-INF in webapps):
<web-app ...>
<servlet>
<servlet-name>cookie1</servlet-name>
<servlet-class>pack1.work.Controller.NameStore</servlet-class>
</servlet>
<servlet>
<servlet-name>cookie2</servlet-name>
<servlet-class>pack1.work.Controller.ItemStore</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cookie1</servlet-name>
<url-pattern>/Namedisplay</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>cookie2</servlet-name>
<url-pattern>/Itemdisplay</url-pattern>
</servlet-mapping>
</web-app>
view.html (To be stored in folder webapps/Excercise )
<html>
<body>
Enter user name and password for detail display of shopping List:
<form method = "POST" action = "Namedisplay">
User name: <input name="username" type="text">
Password: <input name="pwd" type="password"></br>
<input value ="Submit" type="SUBMIT">
</form>
</body>
</html>
NameStore,java( .class to be stored in folder apache-tomcat/webapps/Excercise/WEB-INF/classes/pack1/work/Controller )
/* File to store username and password in cookies*/
package pack1.work.Controller;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class NameStore extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
response.setContentType("text/html");
String usrname = request.getParameter("username");
String password = request.getParameter("pwd");
HttpSession session = request.getSession();
session.setMaxInactiveInterval(20*60);
Cookie cookie1 = new Cookie("Name", usrname);
Cookie cookie2 = new Cookie("Cryptic", password);
response.addCookie(cookie1);
response.addCookie(cookie2);
cookie1.setMaxAge(20*60);
cookie2.setMaxAge(20*60);
RequestDispatcher view = request.getRequestDispatcher("ShopCart.jsp");
view.forward(request, response);
}
}
ItemStore.java( .class to be stored in folder apache-tomcat/webapps/Excercise/WEB-INF/classes/pack1/work/Controller)
/*Program retrieves the username and password cookie.*/
package pack1.work.Controller;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ItemStore extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie cookies[] = request.getCookies();
String user="", password="";
for(int i=0; i<cookies.length; i++){
if(cookies[i].getName().equals("Name")){
user = cookies[i].getValue();
}
else if(cookies[i].getName().equals("Cryptic")){
password = cookies[i].getValue();
}
}
String[] options = request.getParameterValues("Shop");
out.println("</br>");
out.println("Hi, Your login id is: " + user + "Your password is : " + password);
out.println("</br> Above info is brought to u by COOKIES!!!");
out.println("</br> You want ");
for(int i=0; i<options.length; i++){
out.println(options[i] + " ");
}
out.println("from this site");
}
}
view.html (To be stored in folder webapps/Excercise )
<html>
<body>
Enter user name and password for detail display of shopping List:
<form method = "POST" action = "Namedisplay">
User name: <input name="username" type="text">
Password: <input name="pwd" type="password"></br>
<input value ="Submit" type="SUBMIT">
</form>
</body>
</html>
NameStore,java( .class to be stored in folder apache-tomcat/webapps/Excercise/WEB-INF/classes/pack1/work/Controller )
/* File to store username and password in cookies*/
package pack1.work.Controller;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class NameStore extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
response.setContentType("text/html");
String usrname = request.getParameter("username");
String password = request.getParameter("pwd");
HttpSession session = request.getSession();
session.setMaxInactiveInterval(20*60);
Cookie cookie1 = new Cookie("Name", usrname);
Cookie cookie2 = new Cookie("Cryptic", password);
response.addCookie(cookie1);
response.addCookie(cookie2);
cookie1.setMaxAge(20*60);
cookie2.setMaxAge(20*60);
RequestDispatcher view = request.getRequestDispatcher("ShopCart.jsp");
view.forward(request, response);
}
}
ShopCart.jsp((To be stored in folder apache-tomcat/webapps/webapps/Excercise ))
/*Jsp file that takes you to other servlet*/
<html>
<body>
Welcome to Shopping World!! Shop unlimited on following items!!</br>
Hi
<form method="Post" action="Itemdisplay">
<input type="checkbox" name="Shop" value = "Dress" >Dresses</br>
<input type="checkbox" name="Shop" value = "Acessories">Acessories</br>
<input type="checkbox" name = "Shop" value = "Sandals">Sandals</br>
<input type="checkbox" name = "Shop" value = "Electronics">Electronic Items</br>
<input type="checkbox" name = "Shop" value = "Vehicle">Vehicles</br>
<input type = "submit" value = "Items selected!!!">
</form>
</body>
</html>
ItemStore.java( .class to be stored in folder apache-tomcat/webapps/Excercise/WEB-INF/classes/pack1/work/Controller)
/*Program retrieves the username and password cookie.*/
package pack1.work.Controller;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ItemStore extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie cookies[] = request.getCookies();
String user="", password="";
for(int i=0; i<cookies.length; i++){
if(cookies[i].getName().equals("Name")){
user = cookies[i].getValue();
}
else if(cookies[i].getName().equals("Cryptic")){
password = cookies[i].getValue();
}
}
String[] options = request.getParameterValues("Shop");
out.println("</br>");
out.println("Hi, Your login id is: " + user + "Your password is : " + password);
out.println("</br> Above info is brought to u by COOKIES!!!");
out.println("</br> You want ");
for(int i=0; i<options.length; i++){
out.println(options[i] + " ");
}
out.println("from this site");
}
}
No comments:
Post a Comment