dcsimg
Can anyone solve the bug in this cookie servlet. I am held up for the last four days.
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Adi2000_Sharma
Posted On:   Saturday, June 19, 2004 02:12 AM

Problem :- no cookies are added.Loop is going all rite though.Control has been transffered to this servlet from a jsp page on click of Submit button.Browser settings ->ok. Parameters been retrieved are B001,B002.....no of records. they r all checkboxes.If any of them are not null means it is a selected item.So finally data to be added in cookie could be B001;B002;B007 etc. import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import java.sql.*; public class Cookieadd extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse res) throws S   More>>

Problem :- no cookies are added.Loop is going all rite though.Control has been transffered to this servlet from a
jsp page on click of Submit button.Browser settings ->ok.
Parameters been retrieved are B001,B002.....no of records.
they r all checkboxes.If any of them are not null means it
is a selected item.So finally data to be added in cookie
could be B001;B002;B007 etc.

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

import java.sql.*;

public class Cookieadd extends HttpServlet

{

public void doPost(HttpServletRequest req,
HttpServletResponse res)

throws ServletException,IOException

{

PrintWriter pw=res.getWriter();

try{

Connection con=null;

int ctr=0;

boolean cookieFound=false;

Cookie myCookie=null;

Cookie[] cookieset =req.getCookies();

res.setContentType("text/html");

String com=new String (" ");

String rt=new String(" ");

String app=new String (" ");

String st1="B00";

String re= new String(" ");

pw.println(cookieset.length);

pw.println(" ");

pw.println(" ");

for (int i=0;i

{

if (cookieset[i].getName().equals("Choice"))

{

cookieFound=true;

myCookie=cookieset[i];

}

}

if (cookieFound==true) {

pw.println("Hi ! user");

}

if (cookieFound==false) {

pw.println("Welcome to your first shopping");

}


Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection
("jdbc:odbc:MyData","","");

Statement st=con.createStatement();

ResultSet rs=st.executeQuery("select * from
books_for_sale");

ctr=0;

while (rs.next()) {

com=st1+Integer.toString(ctr);

rt=req.getParameter(com);

if (rt !=null) {

app=app+com+";";

}

ctr=ctr+1;

}

rs.close();


if ((app !=null) && (cookieFound==true)) {

re=myCookie.getValue();

re=re+app;

myCookie.setValue(re);

int age=60*60*24*30;

myCookie.setMaxAge(age);

res.addCookie(myCookie);

cookieFound=false;

}

else if ((cookieFound==false) && (app != null))

{

pw.println(app);

myCookie = new Cookie("Choice",app);

int age=60*60*24*30;

myCookie.setMaxAge(age);

res.addCookie(myCookie);

pw.println("A cookie named 'Choice' was added ");

}


pw.println(" ");

pw.println(" ");


}

catch(Exception d)

{

pw.println("Some exception occured :" +d);

}

}

}

   <<Less
About | Sitemap | Contact