Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-22-2011, 04:31 AM   PM User | #1
SKY-ProToSs
Regular Coder

 
Join Date: Apr 2006
Posts: 111
Thanks: 0
Thanked 2 Times in 2 Posts
SKY-ProToSs is an unknown quantity at this point
Importing a cookie stored in a java file

I was asked to display a cookie that was created in a register.java page to a jsp page that is displayed when a user has registered.

Basically the value of the cookie should display after "Welcome: "

The problem is that every single method I try has some kind of screw up because it's not exactly what's needed.

I have had a really ****ty life lately and can't get my head around this. Would appreciate some help.

The error I'm getting is that it wont find the "Cookiename".getValue()

The method of putting it in the page that I googled wont work. It wont work with a useBean either.

Here's the code:

Code:
package download;

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

import business.User;
import data.UserIO;

public class RegisterUserServlet extends HttpServlet
{    
    public void doPost(HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException
    {      
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        String emailAddress = request.getParameter("emailAddress");
        
        User user = new User();
        user.setFirstName(firstName);
        user.setLastName(lastName);
        user.setEmailAddress(emailAddress);

        ServletContext sc = getServletContext();
        String path = sc.getRealPath("WEB-INF/EmailList.txt");

        UserIO.add(user, path);
        
        HttpSession session = request.getSession();
        session.setAttribute("user", user);
        String productCode = (String) session.getAttribute("productCode");
        
        Cookie emailCookie = new Cookie("emailCookie", emailAddress);
        emailCookie.setMaxAge(60*60*24*365*2); //set its age to 2 years
        emailCookie.setPath("/"); //allow the entire application to access it
        response.addCookie(emailCookie);
        
        Cookie fnameCookie = new Cookie("fnameCookie", firstName);
        fnameCookie.setMaxAge(60*60*24*365*2); //set its age to 2 years
        fnameCookie.setPath("/"); //allow the entire application to access it
        response.addCookie(fnameCookie);
        
        String url = "/" + productCode + "_download.jsp";
        RequestDispatcher dispatcher =
            getServletContext().getRequestDispatcher(url);
        dispatcher.forward(request, response);        
    }
}
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Murach's Java Servlets and JSP</title>
</head>
<body>

<h1>List of albums</h1>

<p>
  <%@ page language="java" import="business.User"%>
  <%@page import="RegisterUserServlet"%>
  
   Welcome: <%= fnameCookie.getValue() %>
    
<a href="<%=response.encodeURL("checkUser?productCode=8601")%>">
    86 (the band) - True Life Songs and Pictures
</a><br>

<a href="<%=response.encodeURL("checkUser?productCode=pf01")%>">
    Paddlefoot - The first CD
</a><br>

<a href="<%= response.encodeURL("checkUser?productCode=pf02")%>">
    Paddlefoot - The second CD
</a><br>

<a href="<%= response.encodeURL("checkUser?productCode=jr01")%>">
    Joe Rut - Genuine Wood Grained Finish
</a>
</p>

</body>
</html>
SKY-ProToSs is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:11 PM.


Advertisement
Log in to turn off these ads.