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

