/***********************************************login.html*****************************************/

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="/day07/LoginServlet" method="post">
        用户名:<input type="text" name="username"><br>
        密码:<input type="password" name="password"><br>
        <input type="submit" value="登录">;
    </form>
</body>
</html>

/*************************************LoginServlet***********************************************/

package 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.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String name = request.getParameter("username");
        String pwd = request.getParameter("password");
        
        List<User> list = Db.getAll();
        for(User user:list){
            if(user.getName().equals(name) && user.getPassword().equals(pwd)){
                //登录成功就是向session存一个登录标记
                request.getSession().setAttribute("user", user);//值是user对象
                //跳到首页
                response.sendRedirect(request.getContextPath()+"/index.jsp");
                return;
            }
        }
        
        
        out.print("用户名密码不正确");
        
    }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
class Db{
    private static List list = new ArrayList();
    static{
        list.add(new User("aaa","123"));
        list.add(new User("bbb","123"));
        list.add(new User("ccc","123"));
    }
    public static List getAll(){
        return list;
    }
}
/*****************************************************************index.jsp*******************************************************************8/

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    welcome:${user.name}<a href="/day07/login.html">登录</a><a href="/day07/LogoutServlet">退出登录</a>
</body>
</html>

/***********************************************LogoutServlet*************************************************/

package session;

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 {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        //先判断session是否为空,空就代表没有用户登录,这是就不做处理,继续跳回到首页
        if(session == null){
            response.sendRedirect("/day07/index.jsp");
            return;
        }
        //不为空就摧毁session跳回到首页
        session.removeAttribute("user");
        response.sendRedirect("/day07/index.jsp");
    }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

最新文章

  1. jQuery flickity 滑动触屏
  2. Kafka到Hdfs的数据Pipeline整理
  3. Java---Java的面试题(一)
  4. [AHOI2013]找硬币(搜索)
  5. Hadoop之TaskInputOutputContext类
  6. pip命令使用国内pypi镜像源加速在线安装
  7. HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋)
  8. DES 算法的 C++ 与 JAVA 互相加解密
  9. Ubuntu 安装 Courier New字体
  10. DJANGO,获取当前用户名,用户组名,用户组权限
  11. (转)java多线程的一篇好文
  12. GoF——抽象工厂模式
  13. Html 定位position
  14. setDefaultCloseOperation()参数得使用说明
  15. LINUX 笔记-iostat命令
  16. linux中硬链接与软链接
  17. PyCharm安装及使用
  18. 四、PyQt5布局管理(绝对&amp;相对、水平、垂直、格栅、表单)
  19. Android开发——使用Jword生成本地word文档
  20. Maven创建项目

热门文章

  1. T-SQL还有个内置方法NULLIF()
  2. 图片上传封装类【包括图片上传和缩略图上传】.NET
  3. [LeetCode] Search a 2D Matrix 二分搜索
  4. 移动电子商务:五个技术标准与Trustonic TEE解决方案【转】
  5. 【转】重装Ubuntu时如何保留/home分区中的数据
  6. Java I/O系统学习系列一:File和RandomAccessFile
  7. Android学习--RecyclerView
  8. 10.1综合强化刷题 Day1 morning
  9. 深入浅出 Cocoa 之 Core Data(4)- 使用绑定
  10. 【spring boot】8.spring boot的日志框架logback使用