Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

servlets and session tracking with cookies

807599May 9 2007 — edited May 9 2007
hello
i am a newbie to j2ee. i have made 2 servlets. one is will set the cookie and the other will show show the cookies created. both have compiled without any errors. i startup the Tomcat server. no errors are shown. when i give the login and password. it gives:
HTTP Status 500 - 

--------------------------------------------------------------------------------

type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

java.lang.IllegalArgumentException: Cookie name "Session Cookie1" is a reserved token
	javax.servlet.http.Cookie.<init>(Cookie.java:140)
	mycookies.SetCookies.doGet(SetCookies.java:22)
	mycookies.SetCookies.doPost(SetCookies.java:13)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.


--------------------------------------------------------------------------------

Apache Tomcat/5.5.17
setcookies.java
package mycookies;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class SetCookies extends HttpServlet{

	public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
	{
		doGet(req, res);
	}

	public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
	{
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		
		for(int i = 1; i<4;i++){
			Cookie mysetcookie = new Cookie("Session Cookie"+i,"cookie-value-S"+i);
			res.addCookie(mysetcookie);
			
			mysetcookie = new Cookie("persistant cookie"+i, "cookie-value-p"+i);
			mysetcookie.setMaxAge(3600);
			res.addCookie(mysetcookie);
		}
		
	
			
		res.setContentType("text/html");
		PrintWriter pw = res.getWriter();
		//String Title = "setting cookies";
		pw.println("<HTML>");
		pw.println("<BODY>");
		pw.println("<h1><center>setting cookies</center></h1>");
		pw.println("you have logged in ur username is: "+username+" and password: "+password+"<br>");
		pw.println("There are six cookies associated with this page.<br>");
		pw.println("To see them, visit the");
		pw.println("<A HREF=\"/contentlogincookie/uti/SetCookiepath/mycookies.ShowCookies\">");
		pw.println("<CODE>ShowCookies</CODE> servlet</A>.");
		pw.println("<p>");
		pw.println("Three of the cookies are associated only with the ");
		pw.println("current session, while three are persistent");
		pw.println("Quit the browser, restart, and return to the");
		pw.println("<CODE>ShowCookies</CODE> servlet to verify that");
		pw.println("the three long-lived ones persist across sessions");
		pw.println("</BODY></HTML>");	
		
	}
}
ShowCookies.java
package mycookies;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class ShowCookies extends HttpServlet{
		
		public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
		{
			doGet(req, res);
		}

		public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
		{
			res.setContentType("txt/html");
			PrintWriter pw = res.getWriter();
			pw.println("<html>");
			pw.println("<BODY BGCOLOR=\"#FDF5E6\">");
	//		pw.println("<H1 ALIGN=\"CENTER\">");
			pw.println("<TABLE BORDER=1 ALIGN=\"CENTER\">");
			pw.println("<TR BGCOLOR=\"#FFAD00\">");
			pw.println(" <TH>Cookie Name");
			pw.println(" <TH>Cookie Value");
			
			Cookie cookies[] = req.getCookies();
			Cookie cookie;
			for(int i=0; i<cookies.length; i++)
			{
				cookie=cookies;

pw.println("<TR>");
pw.println("<TD>"+cookie.getName());
pw.println("<TD>"+cookie.getValue());
//pw.println("<TD>"+cookie.getMaxAge());
}
pw.println("</table>");
pw.println("</body>");
pw.println("</html>");
}

}


MyLogin.html
<html>
  <body>
  		<form action="/contentlogincookie/uti/SetCookiepath" method="post">
  			LoginName: <input type="text" name="username"> <br><br>
  			Password: <input type="password" name="password"> <br><br><br><br>
  			<input type="submit" value="Enter">
  		</form>
  		
  </body>
</html>
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
	
	<servlet>
		<display-name>SetCookie</display-name>
		<servlet-name>SetCookie</servlet-name>
		<servlet-class>mycookies.SetCookies</servlet-class>
	</servlet>

	<servlet>
		<display-name>ShowCookie</display-name>
		<servlet-name>ShowCookie</servlet-name>
		<servlet-class>mycookies.ShowCookies</servlet-class>
	</servlet>

     <servlet-mapping>
        <servlet-name>SetCookie</servlet-name>
        <url-pattern>/uti/SetCookiepath</url-pattern>
    </servlet-mapping>


     <servlet-mapping>
        <servlet-name>ShowCookie</servlet-name>
        <url-pattern>/uti/ShowCookiepath</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
    	
        <welcome-file>MyLogin.html</welcome-file>
        
    </welcome-file-list>

</web-app>
server.xml
<Context path= "/contentlogincookie" docBase="J:\javaprojects\CookieSession" reloadable="true"></Context>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 6 2007
Added on May 9 2007
1 comment
409 views