使用Session完成用户登陆 利用Session实现验证码校验

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.Cookie;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import cn.csdn.dao.BookDao;

import cn.csdn.domain.Book;

public class BuyServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out= response.getWriter();

String bookid= request.getParameter("id");

BookDao bd= new BookDao();

Book book= bd.GetBook(bookid);

// 关闭浏览器后还可进行购买

HttpSession session =request.getSession();

Cookie cookie= new Cookie("JSESSION",session.getId());

cookie.setMaxAge(30*60);

cookie.setPath("/BookHistory");

response.addCookie(cookie);

List list = (List) session.getAttribute("cart");

if(list == null){

list = new ArrayList();

session.setAttribute("cart",list);

}

list.add(book);

// 禁用 cookie 后还可一购买

response.encodeRedirectURL("/BookHistory/servlet/CartDisplayServlet");

response.sendRedirect("url");

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

package cn.csdn.servlet;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import cn.csdn.domain.Book;

public class CartDiaplay extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out= response.getWriter();

// 获取 session 的资源

List<Book> list= (List<Book>) request.getSession().getAttribute("cart");

for(Book book:list){

out.print(book.getName()+"<br/>");

}

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}package cn.csdn.servlet;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import cn.csdn.dao.BookDao;

import cn.csdn.domain.Book;

import cn.csdn.domain.User;

public class IndexShoppingServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out= response.getWriter();

/*User user = session.getAttribute("user");

if(user!=null){

out.print(" 欢迎您 "+user.getUsername());

}*/

out.print(" 如下商品是: <br/>");

BookDao db= new BookDao();

List <Book> list = db.getAll();

for(Book book:list){

out.print(book.getName()+"<br/>");

}

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

package cn.csdn.servlet;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import cn.csdn.dao.UserDao;

import cn.csdn.domain.User;

public class LoginServlet extends HttpServlet {

public 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 username= request.getParameter("username");

String password= request.getParameter("password");

String checkcode=request.getParameter("checkcode");

System.out.println(checkcode);

String imgcode= (String) request.getSession().getAttribute("imgcode");

if(checkcode!=null && imgcode!=null &&checkcode.equals(imgcode)){

UserDao udao= new UserDao();

User user = udao.find(username, password);

if(user!=null){

request.getSession().setAttribute("user", user);

response.sendRedirect("/BookHistory/servlet/IndexShoppingServlet ");

}

out.print(" 用户名密码错误 !!!!!!");

}else{

out.print(" 验证密码错误!!!! ");

}

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

package cn.csdn.servlet;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

public class LogoutServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

HttpSession session= request.getSession( false );

if (session == null ){

response.sendRedirect( "/BookHistory/servlet/IndexServletShopping" );

return ;

}

session.removeAttribute( "user" );

response.sendRedirect( "/BookHistory/servlet/IndexServletShopping" );

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值