登陆
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
//PrintWriter out = response.getWriter();
String name = request.getParameter("name");
String pass = request.getParameter("password");
System.out.println("读取表格成功");
JavaBSession peo = new JavaBSession();
peo.setName(name);
peo.setPassword(pass);
JavaBSession peo1 = new JavaBSession();
Thelogin loginThelogin = new Thelogin();
peo1 = loginThelogin.loginjc(peo);
String s = URLEncoder.encode(name, "GBK");
ServletContext context = this.getServletContext();
context.setAttribute("username", s);
if(peo1!=null){
System.out.println("登陆成功");
String use = request.getSession().getId()+s;
System.out.println("登陆session:"+use);
Cookie cookie[] = request.getCookies();
String time = null;
for(int i=0;i<cookie.length&&cookie!=null;i++){
if(use.equals(cookie[i].getName()))
time = cookie[i].getValue();
}
if(time!=null){
System.out.println(name+"上次登陆时间:"+time);
}
else {
System.out.println("第一次登陆");
}
String timesString = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
Cookie cookie2 = new Cookie(use, timesString);
System.out.println("本次登录时间"+timesString);
response.addCookie(cookie2);
System.out.println("增加cookie成功");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
else{
System.out.println("登陆失败");
request.getRequestDispatcher("MyLog.jsp").forward(request, response);
}
}
注销
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String use = (String) context.getAttribute("username");
String id=request.getSession().getId();
request.getSession().removeAttribute(id+use);
request.getRequestDispatcher("MyLog.jsp").forward(request, response);
System.out.println("退出成功");
}
购物(servlet)查找数据库里面商品消息的类没放,类名Gwspjq
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String [] itemStrings;
String iterName ;
double sum = 0;
double s = 0;
Gwspjq jc = new Gwspjq();
ServletContext context = this.getServletContext();
String usesString = (String) context.getAttribute("username");
HttpSession session = request.getSession();
String id=request.getSession().getId();
String use = id+usesString;
@SuppressWarnings("unchecked")
ArrayList<String> cartArrayList = (ArrayList<String>)session.getAttribute(use);
if(cartArrayList==null){
cartArrayList = new ArrayList<String>();
session.setAttribute(use, cartArrayList);
}
System.out.println("购物进行中"+use);
itemStrings = request.getParameterValues("shangp");
Cookie cookie[] = request.getCookies();
for(int i=0;i<cookie.length&&cookie!=null;i++){
if(use.equals(cookie[i].getName())){
System.out.println("是同一用户");
if(itemStrings != null){
System.out.println(itemStrings[0]+itemStrings[1]+itemStrings[2]);
System.out.println(itemStrings.length);
for(int k = 0;k<itemStrings.length;k++){
iterName = itemStrings[k];
System.out.println("3");
cartArrayList.add(iterName);
System.out.println("4");
s = jc.Gwspjqa(iterName);
System.out.println("5");
sum+=s;
out.println(iterName+" "+s);
System.out.println("6");
System.out.println(iterName+" "+s);
}
}
//输出
System.out.println("7");
out.println("总价 "+sum);
}
}
}
备注:因为问题主要出现在登陆时,后面的结算是没有问题的。
发现重启服务器后sessionID改变,目前解决办法为增加一个javaBean类,存放cookie的value,有时间,本次sessionID,上次sessionID。具体实现很简单,就不阐述了(我的代码能顺利运行的前提是服务器不死)。有一种技术可以让服务器死后还能用原来的session,大体思想就是我那个解决办法,但是不这样实现(要序列化,反序列化)。
碰到的问题
- 多个servlet数据共享问题,在我定义自己的sessionID的时候用到,需要调用唯一用户名(使用servletcontext 的this.getservletcontext() 定义一个context,然后使用setAttribute() 设置,使用get调用)
- 用户名是中文名,但是cookie里不允许存中文字符串(使用URLEncoder.encode(name, "GBK")编码,dn是译码,注意:直接编码已经out了,需要写编码格式)
- 代码里的system.out是用来调试的,我没删
- 上面代码没有自动注销,一定要手动注销,否则,就不对(有待完善)。
- 如何判断同一用户(之前问老师,她说把session的ID放到cookie里保存,但是这个只是可以单用户,也就是一个浏览器只允许一个人买东西,但是实际情况不是这样的,你也可以注销掉自己的号然后登陆别人的,所以我重新第一个自己的sessionID = request.getsession().getid()+name)这样就可以保证自己的sessionID是独一无二的,然后保存到cookie,在结算的时候遍历所有的cookie找到与结算页面sessionID相同的那个(name定义成共享,只有登录的时候才能初始化name,所以就可以判断是同一人了,这里就要到了GBK编码)然后再把选的物品保存到arraylist中)
总之代码不是很完美,只是解决掉了最主要的难题,剩下的就是小问题了。
(如果有什么问题请评论,后续会不断完善)
不拘小节和细节决定成败这两句话,我觉得够我们感悟一辈子