tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Huragan <leoura...@googlemail.com>
Subject unable to set cookies
Date Sun, 03 Feb 2008 21:06:03 GMT

I am new to j2ee and have been trying to understand how cookies work. I am
using tomcat 6.0.13. I have tried setting cookies on client browsers(both IE
& Firefox). However, it never succeeds. mostly am getting a
NullPointerException. I narrowed it down to the part of code where cookies
get retrieved. There are no cookies being returned with the response object.
When i displayed the header info there were no cookies being set at all. I
know that cookies are the default option for session handling and therefore,
are turned on by default. As you can see the code works if i comment out the
cookies retrieval.
However, I still tried to set it to true in the context.xml file. which
didn't work. Then tried to set a context element in Host in server.xml.
Needless to say that didn't work either. Now am at my wits end as to why it
is not working. i am attaching the servlet code below:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class CookieServlet2 extends HttpServlet {
	public void doGet(HttpServletRequest request,
					  HttpServletResponse response)
		throws ServletException, IOException {
		Cookie c1 = new Cookie("userName", "Sandeep");
		Cookie c2 = new Cookie("password", "password");
		response.addCookie(c1);
		response.addCookie(c2);
		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<HTML>");
		out.println("<HEAD>");
		out.println("<TITLE>Cookie Test</TITLE>");
		out.println("</HEAD>");
		out.println("<BODY>");
		out.println("Please click on the button to see the " +
					"cookies sent to you.");
		out.println("<BR>");
		out.println("<FORM METHOD=POST>");
		out.println("<INPUT TYPE=SUBMIT VALUE=SUBMIT>");
		out.println("</FORM>");
		out.println("</BODY>");
		out.println("</HTML>");
	}
	
	public void doPost(HttpServletRequest request,
					  HttpServletResponse response)
		throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		out.println("<HTML>");
		out.println("<HEAD>");
		out.println("<TITLE>Cookie Test</TITLE>");
		out.println("</HEAD>");
		out.println("<BODY>");
		out.println("<H2>Here are all the headers: </H2>");
		
		Enumeration enumer = request.getHeaderNames();
		while(enumer.hasMoreElements()) {
			String header = (String) enumer.nextElement();
			out.print("" + header + "");
			out.print(request.getHeader(header) + "<BR>");
		}
		
		out.println("<BR><BR><H2>And here are all the cookies</H2>");
		/*Cookie[] cookies = request.getCookies();
		int length = cookies.length;
		out.println(length);
		for(int i=0;i<length;i++) {
			Cookie c=cookies[i];
			out.println("Cookie Name: " + c.getName() + "<BR>");
			out.println("Cookie Value: " + c.getValue() + "<BR>");
		}*/
		out.println("</BODY>");
		out.println("</HTML>");
	}
}
any help will be appreciated
-- 
View this message in context: http://www.nabble.com/unable-to-set-cookies-tp15258280p15258280.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message