Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

JSp with Cookies HFSJ pg 386

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?


 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mukunthan Shanmuganathan:


request.getKookies();



That's right. Let's round all the kookies up and put them away!

Ok, but seriously, where exactly is the NPE? If you'd point out the line it would help determine what is causing the problem...this is pretty vague. And also, it looks like you're typing your code; please don't do that as it can mask errors and will be pretty frustrating for all involved.
 
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will need to round trip back to the browser to see the cookie. If you set the cookie into the response then, before you go back to the browser with the response and therefore set the cookie on the client, you go to the JSP, the cookie was never returned to the client and therefore will not be in the request.

When you make a second request to your web app, the cookie will be in the request.
 
Mukunthan Shanmuganathan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Tarun Yadav and Michael Ku,

Thank You.

Tarun Yadav,I apologize for writing the code here. I won't do it again.

Michael Ku, I understand your point.

Thank You very much both of you Guys.

reply
    Bookmark Topic Watch Topic
  • New Topic