Results 1 to 3 of 3
  1. #1
    jurka is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default Simple JSP page got exception by using cookies

    Here is code, which is mostly taken from web.

    Exception:
    Java Code:
    org.apache.jasper.JasperException: An exception occurred processing JSP page /jsp/TestWeb/WebRoot/Parameter.jsp at line 39
    
    36:   </table>
    37:   <%
    38:   } else {
    39:   	response.addCookie(new Cookie("Cookie One", "Chocolate Chip"));
    40:   	response.addCookie(new Cookie("Cookie 2", "Peanut butter"));
    41:   	response.addCookie(new Cookie("cookie 3", "Sugar"));
    42:    %>
    
    
    Stacktrace:
    	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    
    root cause
    
    java.lang.IllegalArgumentException: Cookie name "Cookie One" is a reserved token
    	javax.servlet.http.Cookie.<init>(Cookie.java:141)
    	org.apache.jsp.jsp.TestWeb.WebRoot.Parameter_jsp._jspService(Parameter_jsp.java:107)
    	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    Java Code:
    <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'Parameter' starting page</title>
        
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    	<meta http-equiv="description" content="This is my page">
    	<!--
    	<link rel="stylesheet" type="text/css" href="styles.css">
    	-->
    
      </head>
      
      <body>
      <%
      Cookie[] cookies = request.getCookies();
      if (cookies.length > 1) {
      %>
      <table>
      	<tr>
      <% for (int i = 0; i < cookies.length; i++) { %>
      		<td><%= cookies[i].getName() %></td>
      		<td><%= cookies[i].getValue() %></td>
      <% } %>
      </table>
      <%
      } else {
      	response.addCookie(new Cookie("Cookie One", "Chocolate Chip"));
      	response.addCookie(new Cookie("Cookie 2", "Peanut butter"));
      	response.addCookie(new Cookie("cookie 3", "Sugar"));
       %>
       <h2>Cookies has been initialized</h2>
       <% } %>
      </body>
    </html>

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    Cookie name "Cookie One" is a reserved token
    Have you tried using a different name?

  3. #3
    jurka is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    Oh now i see what's wrong there space between Cookie One, when removed that it worked also for other names. But how there where space in book ? Or i am reading too old book ?

Similar Threads

  1. Go back to previous page using session in JSP page
    By gopikarikati009 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-23-2011, 10:30 AM
  2. setting the view to a jsp page from a self refeshing page
    By deepal_205 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 08-15-2008, 04:41 PM
  3. warning that page has expired and i need to refresh the page again
    By 82rathi.angara in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 07-15-2008, 01:48 PM
  4. Problem with JSP exception handling page
    By sidster in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-19-2008, 07:28 PM
  5. Replies: 2
    Last Post: 05-04-2008, 05:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •