Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > JSP Basics
|
JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 8th, 2007, 09:50 PM
Authorized User
 
Join Date: Aug 2005
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem with getting cookies

here r 2 JSPs, the login.jsp gets params and after validating them, it adds the param into cookies and forwards 2 home.jsp, in home.jsp I want 2 get the cookie that was added in login.jsp, but when 1st time home.jsp is loaded, there iz no such cookie... so, can n.e body tell me what iz the problem with this code?

login.jsp
=========
<%!
String uname,pwd;
Cookie userCookie;
%>
<%
uname=request.getParameter("uname");
pwd=request.getParameter("pwd");
if(uname.equals("hello") && pwd.equals("world"))
{
    userCookie=new Cookie("user",uname);
    response.addCookie(userCookie);
%>
    <jsp:forward page="home.jsp"/>
<%
}
else
{
    out.println("<h3>Invalid user</h3>");
}
%>

home.jsp
========
<%!
Cookie[] cookies;
Cookie userCookie;
boolean found;
%>
<%
cookies=request.getCookies();
found=false;
for(int i=0;i<cookies.length;i++)
{
    userCookie=cookies[i];
    if(userCookie.getValue().equals("hello"))
    {
        found=true;
        break;
    }
    if(found==true)
    {
        out.println("<h1> Hello World </h1>");
    }
    else
    {
        out.println("<h1> Sorry </h1>");
    }
}
%>

 
Old June 18th, 2007, 12:51 AM
Authorized User
 
Join Date: Jun 2007
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Generaly, you have two methods to navigate your clients to another page :

1. Forward
2. Redirect

When you use redirect method, server will sends a http response to client and asks him to send a new request for destination page. Using forward method, will not sends any response to client, but navigates same request object to destination page directly.

As I see in your code snippets, you are using first method (forwarding) to navigate your client to 'home.jsp'. So in 'home.jsp' you are accessing same request object that you had in 'login.jsp', that has no cookie. Because your response object (containing your cookie) has not been sent to client. Your cookies will be accessible from next incoming request.





Similar Threads
Thread Thread Starter Forum Replies Last Post
cookies problem kanoorani Servlets 0 December 22nd, 2006 10:54 AM
Cookies Problem dparsons ASP.NET 1.0 and 1.1 Professional 0 November 28th, 2006 01:47 PM
Problem with using cookies JSB Beginning PHP 0 November 25th, 2005 11:58 AM
Problem with cookies :( Varg_88 Classic ASP Databases 2 December 13th, 2004 11:34 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.