File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Re: Cookie problem

 
chintan doshi
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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

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

VP
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
suresh guru
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic