This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Re: Cookie problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Re: Cookie problem" Watch "Re: Cookie problem" New topic
Author

Re: Cookie problem

chintan doshi
Greenhorn

Joined: Apr 11, 2006
Posts: 3
n my application i cant read the cookie created from a servlet in a JSP page.
when i create the cookie using JSP & then read it using another JSP page it works fine.
can anyone please help me out with this problem.



the task is to create a simple login application. it runs perfect on my local machine but when i run it on my university server it doesnt.

code:


the 1st jsp page has two text field and a check box. if the check box is on then that username & id are stored in a cookie and hence when the 1st page is reloaded the appear in.

the code for 1st JSP page is :

String unamev="";
String upassv="";


Kookie kookies[] = request.getKookies();

if (kookies != null) {
for (int i=0; i cookies.length; i++) {
if (kookies[i].getName().equals("uname10")) {
unamev = kookies[i].getValue();
//out.println(unamev);
}}

for (int j=0; j kookies.length; j++) {
if (kookies[j].getName().equals("upass10")) {
upassv = kookies[j].getValue();
} }}



FORM method=GET ACTION="http://................."
p align="right" Login INPUT TYPE = text name="L" value=%=unamev%
p align="right" Password INPUT TYPE = "password" name="P" value=%=upassv%
p align="right" Remember Login INPUT TYPE = "checkbox" name="R"
p align="right" INPUT TYPE = submit value = "Sign in"


this works well on both local machine as well as uni server if it is directed to second jsp page:


String s1 = request.getParameter("L");
String s2 = request.getParameter("P");
String x = request.getParameter("R");
if (x!=null) {
Kookie mycookie1 = new Kookie("uname10",s1);
mycookie1.setMaxAge(24*60*60);
response.addCookie(mycookie1);

Kookie mycookie2 = new Kookie("upass10",s2);
mycookie2.setMaxAge(24*60*60);
response.addCookie(mycookie2);
//out.println("done");
}



but if i direct it to the servlet it doesnt work wen i run it from my uni server:

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
try
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String s1 = req.getParameter("L");
String s2 = req.getParameter("P");
String x = req.getParameter("R");
Kookie mycookie1 = new Kookie("uname10",s1);
mycookie1.setMaxAge(24*60*60);
response.addCookie(mycookie1);

Kookie mycookie2 = new Kookie("upass10",s2);
mycookie2.setMaxAge(24*60*60);
response.addCookie(mycookie2);

rs=s.executeQuery("select * from Login3 where login='"+s1+"'and pass='"+s2+"'");
if(rs.next())
{
req.setAttribute("valid",s1);
req.setAttribute("valid1",s2);
req.setAttribute("valid2",x);
RequestDispatcher view = req.getRequestDispatcher("/jsp5.jsp");
view.forward(req, res);

}

}
catch(Exception g)
{
g.printStackTrace();
}




please help me out.
thank you,
Vikrant Pandit
Ranch Hand

Joined: Mar 27, 2006
Posts: 245
Hi

Didn't go thro ur entire problem, but I thought

we had "Cookie" in http and not "kookie"

VP


Vikrant Pandit
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Vivek Pandey:
Hi

Didn't go thro ur entire problem, but I thought

we had "Cookie" in http and not "kookie"

VP


The forum software doen't allow certain keywords containing the string "cookie".


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
suresh guru
Ranch Hand

Joined: Sep 07, 2002
Posts: 38
Hi,

Problem is with the RequestDispatcher. you are trying to forward a request after getting PrintWriter reference. retry by commenting out the PrintWriter statement
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

"suresh rg",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Re: Cookie problem
 
Similar Threads
Regarding Cookies
HFSJ book: Cookie example chapter 6 plz Help :(
need help for cookies problem
response.addCookie is not working
setting a cookie in jsp