10 JSTL 核心标签库标签实战

需求:模拟登录
如果登录了,首页显示登录用户名
如果没有登录,提示用户登录

Paste_Image.png

User.java

public class User {
    private int id;
    private String name;
    private String password;
    
    public User(int id,String name,String password){
        this.id = id;
        this.name = name;
        this.password = password;
    }
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
        
}

LoginServlet

public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");
        
        String password = req.getParameter("password");
        
        resp.setContentType("text/html;charset=utf-8");
        if (name.equals("丁昌江")){
            if (password.equals("1111")){
                HttpSession session = req.getSession();
                session.setAttribute("name",name);
                //这里是把post过来的请求转发到UserListServlet
                req.getRequestDispatcher("/UserListServlet").forward(req, resp);//
            }else{
                resp.getWriter().write("密码错误");
            }
        }else{
            resp.getWriter().write("用户名错误");
        }
    } 
    
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

}

UserListServlet

public class UserListServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<User>list = new ArrayList<User> ();
        list.add(new User(1,"a","a-1"));
        list.add(new User(1,"a","a-1"));
        list.add(new User(1,"a","a-1"));
        
        request.setAttribute("userList",list);
        request.getRequestDispatcher("/userList.jsp").forward(request, response);
    }
}

login.jsp

<body>
    <form action="${pageContext.request.contextPath}/LoginServlet" method="post">
    用户名:<input type="text" name="name"/><br/>
    密码:<input type="password" name="password"/><br/>
    <button type="submit">登录</button>
    </form>
</body>

userList.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
<style type="text/css">
        table{
            margin: 0px auto;
            border: 1px solid #223344;
            text-align:center;
        }
        
        table td,th{
            border: 1px solid #112233;
            text-align: center;
        }
</style>
</head>
<body>
    
    <c:choose>
        <c:when test="${empty sessionScope.name}">
    请先<a href="${pageContext.request.contextPath}/login.jsp">登录</a><br/>
        </c:when>
        
        <c:otherwise>
    欢迎${sessionScope.name}回来
        </c:otherwise>
    </c:choose>
    
    <table class="table table-bordered">
        <caption>员工信息统计</caption>
        <tr>
            <th>编号</th>
            <th>姓名</th>
            <th>密码</th>
        </tr>
        
        <c:forEach items="${userList}" var="user">
            <tr>
                <td>${user.id }</td>
                <td>${user.name }</td>
                <td>${user.password }</td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>
login.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容