Cookie code.Urgent help needed !
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Adi2000_Sharma
Posted On:   Wednesday, June 16, 2004 09:39 AM

Hi experts, I am using j2sdkee1.2.1 and jdk1.3 OS-Windows 2000 Server. Developing a shopping cart program. At this stage,when i open browser window and call a jsp page,it prompts me for adding a cookie(due to customized settings). On viewing the details the name of Cookie is JSESSIONID, which is a default cookie that is added. As i navigate through pages and reach a page where i have stated a logic to add cookie,i find that - No cookie get added however it should. - req.getCookies() only gives JSESSIONID cookie. - 1 out of 200 times it gives success to prompt me to add cookie that i want. - On again opening the browser window    More>>

Hi experts,

I am using j2sdkee1.2.1 and jdk1.3 OS-Windows 2000 Server.
Developing a shopping cart program.

At this stage,when i open browser window and call a jsp
page,it prompts me for adding a cookie(due to customized settings).

On viewing the details the name of Cookie is JSESSIONID, which is a default cookie that is added.
As i navigate through pages and reach a page where i

have stated a logic to add cookie,i find that

- No cookie get added however it should.

- req.getCookies() only gives JSESSIONID cookie.

- 1 out of 200 times it gives success to prompt me

to add cookie that i want.

- On again opening the browser window at that stage,

it refuses to find the same cookie while max age is

1 year.

Earlier i was using JSP,i converted the code to

servlet and results are same !.

I am willing to add shopping cart items.At this stage i

know all items selected by user and i want to put all of
them (only ids) in a cookie.Ids go as B000 B001 B002 ...
Here's the code




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

{

Connection con=null;

int ctr=0;

boolean cookieFound=false;

Cookie c=null;

String com=new String (" ");

String rt=new String(" ");

String st1="B00";

PrintWriter pw=res.getWriter();

Cookie[] cookieset =req.getCookies();

pw.println(cookieset.length);

for (int i=0;i

{

if (cookieset[i].getName() !=null) {

pw.println(cookieset[i].getName());

}

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

{

cookieFound=true;

c=cookieset[i];

}

}

if (cookieFound==true) {

pw.println("Hi ! user");

}

if (cookieFound==false) {

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

}

try

{

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) && (cookieFound==true)) {

res.setContentType("text/html");

c.setValue(com);

c.setMaxAge(3600*24*30);

res.addCookie(c);

}

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

{

c = new Cookie("Booksjargon",com);

c.setMaxAge(3600*24*30);

res.addCookie(c);

pw.println("A Cookie was added to your system");

}


ctr=ctr+1;

}

}

catch(Exception d)
{

pw.println("Cannot display the record" +d);

}

}
}

   <<Less
About | Sitemap | Contact