用jsp實現登錄,登錄成功則跳轉到登錄成功頁面,失敗則跳轉到失敗頁面


login.jsp

<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <div>
        <form action="dologin.jsp" method="post">
            <span>用戶名</span>
            <input type="text" name="username" />
            <span>密碼</span>
            <input type="text" name="password" />
            <input type="submit" value="登錄"/>
        </form>
    
    </div>
</body>
</html>

dologin.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<% String path=request.getContextPath(); String basePath=request.getScheme()+"://"+request.getServerName() +":"+request.getServerPort()+path+"/"; String username=""; String password=""; request.setCharacterEncoding("utf-8"); username=request.getParameter("username"); password=request.getParameter("password"); out.println(username); out.println(password); if("admin".equals(username) && "admin".equals(password)){ session.setAttribute("loginUsername", username); session.setAttribute("loginUser",username); request.getRequestDispatcher("login_success.jsp").forward(request,response); }else{ response.sendRedirect("login_failure.jsp"); } %>

login_success.jsp

<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <div> 歡迎<h3><%=session.getAttribute("loginUsername") %></h3>,登錄成功!!!! </div>
</body>
</html>

login_failure.jsp

<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <div> 登錄失敗,請檢查用戶名或者密碼!!! <a href="login.jsp">login</a>
    </div>
</body>
</html>


————————————————
版權聲明:本文為CSDN博主「吃瓜群眾歡樂多」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/sinat_36146776/article/details/60349599


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM