Help reading from a cookie and processing infomation from it

i have a servlet which lets a user register, when they register a cookie is set depending on which level they choose, the cookies are:

int userLevel = Integer.parseInt(level);

if(userLevel == 1){

Cookie cookie =new Cookie("trialUser", request.getParameter("userName"));

cookie.setMaxAge(86400*14);// secs a day times 14

response.addCookie(cookie);

}

elseif( userLevel == 2){

Cookie cookie =new Cookie("level_1", request.getParameter("userName"));

cookie.setMaxAge(86400*365);// lasts a year

response.addCookie(cookie);

}

elseif( userLevel == 3){

Cookie cookie =new Cookie("level_2", request.getParameter("userName"));

cookie.setMaxAge(86400*365);// lasts a year

response.addCookie(cookie);

}

else{

Cookie cookie =new Cookie("level_3", request.getParameter("userName"));

cookie.setMaxAge(86400*365);// lasts a year

response.addCookie(cookie);

}

I then have a servlet which takes the items that a user has picked and updates my database with the details. My problem is how can i use the cookie value in the servlet below so that when a user gets an item the servlet knows which level there on and can decided if they have made a valid choice?

Each level as different things attached to it trial user lets users get 3 items over 14 days

level 2 lets them get 1 item level 3 2 items level 4 3 items. So i need to check the level then need a way to see if there over the limit if so the servlet will not update and they will get a message.

package coreservlets;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.sql.*;

publicclass addRentalextends HttpServlet{

publicvoid doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

HttpSession session = request.getSession();

String username = (String)session.getAttribute("username");

if (username ==null){

out.println(

"<BODY BGCOLOR=\"#a00e4e\">\n" );

out.println("You are not logged in you may not get items");

}else{

Connection conn =null;

try{

Class.forName("com.mysql.jdbc.Driver").newInstance();

}catch(Exception e){

System.out.println(e);

}

try{

conn = DriverManager.getConnection

("jdbc:MYURL");

}

catch(SQLException se){

System.out.println(se);

}

try{

String recordingIDStr = request.getParameter("recordingid");// this id comes from a form

int recordingID = Integer.parseInt(recordingIDStr);// convert to string to alow to update to table

int userid = 0;// THIS WAS A TEST WILL NEED TO BE A STRING AND GET USERNAME FROM COOKIE

int priority = 4;// need a way to work this out

String insertSQL ="insert into video_rental(customer_id, recording_id, Priortiy) values ( "+

userid +"," + recordingID +"," + priority +")";

System.out.println("insert statement " + insertSQL);

Statement stmt = conn.createStatement();

int rowsAffected = stmt.executeUpdate(insertSQL);

String title ="Added rental : "+ recordingID;

out.println(ServletUtilities.headWithTitle(title) +

"<BODY BGCOLOR=\"#a00e4e\">\n" +

"<center></center>\n" +

"<H1 ALIGN=\"CENTER\">" + title +"</H1>\n");

out.println("Added rental");

out.println("</BODY></HTML>");

stmt.close();

conn.close();

}catch(SQLException se){

System.out.println(se);

}

}

}

}

Any help will be fantastic

[6673 byte] By [ajrobsona] at [2007-11-26 14:26:32]
# 1
anyone? i need help with this just a something to get me started would help!Message was edited by: ajrobson
ajrobsona at 2007-7-8 2:19:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hi,

I would suggest that you in corporate the cookie code into the main program (addrental) so that as soon as the user is validated a session-id is created using the cookie and the user-level is determined by the the validating code.Then the control finally goes to the data processing part where connection to the database is established and other tasks can be performed.

Thanks.

Mudita at 2007-7-8 2:19:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
thanks for the reply, can anybody help me get started on this as i do not know where to begin.
ajrobsona at 2007-7-8 2:19:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...