Guys!
In order to check Cookies with
JSP I wrote as follows,
Servlet -------
package com.example;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class CookieTest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String user = request.getParameter("username");
Cookie cookie = new Cookie("name",user);
cookie.setMaxAge(30*60);
response.addCookie(cookie);
RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request,response);
}
}
JSP
---
Cookie[] cookies = request.getKookies();
for( int i = 0 ; i < cookies.length ; i++ ) {
Cookie cook = cookies[i];
if ( (cook.getName()).equals("name") ) {
out.println(cook.getValue());
}
}
When I deploy and run I get a NullPointerException Why?