10 JSTL 核心標(biāo)簽庫(kù)標(biāo)簽實(shí)戰(zhàn)

需求:模擬登錄
如果登錄了,首頁(yè)顯示登錄用戶名
如果沒有登錄,提示用戶登錄

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過(guò)來(lái)的請(qǐng)求轉(zhuǎn)發(fā)到UserListServlet
                req.getRequestDispatcher("/UserListServlet").forward(req, resp);//
            }else{
                resp.getWriter().write("密碼錯(cuò)誤");
            }
        }else{
            resp.getWriter().write("用戶名錯(cuò)誤");
        }
    } 
    
    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" >
<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}">
    請(qǐng)先<a href="${pageContext.request.contextPath}/login.jsp">登錄</a><br/>
        </c:when>
        
        <c:otherwise>
    歡迎${sessionScope.name}回來(lái)
        </c:otherwise>
    </c:choose>
    
    <table class="table table-bordered">
        <caption>員工信息統(tǒng)計(jì)</caption>
        <tr>
            <th>編號(hào)</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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容