I'm having problems with building cookies in my java file.
Here are my problems:
1)How to I create a cookie?
2)How to check if a cookie is created?(if created,how to write into that particular cookie)
3)Where are the cookies stored when I run test?
Below is my codes(I'm not sure if I did it right)
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class Prac4Task2 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Reading Request Parameters and HTTP Headers";
// Generate HTML document head
out.println("<html>\n<head>\n" +
"<title>" + title + "</title>\n</head>");
// Display a title in teh HTML document
out.println("<body bgcolor=\"#FDF5E6\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n");
Cookie[] cookies = request.getCookies();
if(cookies!=null)
{
for(int i=0;i<cookies.length;i++){
if(cookies[i].getName().equals(request.getParameter("StudentName"))
out.println(cookies[i].getValue());
}
}
else{
Cookie userCookie=new Cookie(request.getParameter("StudentName"),request.getParameter("StudentNumber"));
response.addCookie(userCookie);
}
// Close teh HTML docuemnt
out.println("</body></html>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
