登錄的Servlet程式
/**
* @Author Li Weitong
* @Date 2020/11/12 14:03
*/
public class LoginServlet extends HttpServlet {
private UserService userService = new UserServiceImpl();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*
1.獲取輸入的用戶名和用戶密碼引數
2.呼叫服務層登錄方法,
如果未查詢到回傳空值
跳轉到當前頁面
否則
跳轉到登錄成功頁面
*/
String username = req.getParameter("username");
String password = req.getParameter("password");
User loginUser = userService.login(new User(null, username, password, null));
if (loginUser != null) {
req.getRequestDispatcher("pages/user/login_success.html").forward(req,resp);
}else {
req.getRequestDispatcher("pages/user/login.html").forward(req,resp);
// 在頁面提示用戶名或密碼有誤
}
}
}
當完成基本功能之后,如果我登錄不成功,跳轉到當前的頁面后想讓服務器回傳資料提示用戶,用到JSP技術,
登錄頁面 login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登錄頁面</title>
<link rel="stylesheet" href="../../static/css/style.css">
<script></script>
</head>
<body>
<div id="login_header">
<!-- <img class="logo_img" alt="無法顯示" src="" alt="">-->
</div>
<div class="login_banner">
<div id="l_content">
<span>歡迎登錄</span>
</div>
<div id="content">
<div class="login_form">
<div class="login_box">
<div class="tit">
<h1>卡賓會員</h1>
<a href="regist.html">立即注冊</a>
</div>
<div class="msg_cont">
<b></b>
<span class="errorMsg">請輸入用戶名和密碼</span>
</div>
<div class="form">
<form action="http://localhost:8080/loginServlet" method="post">
<label>用戶名稱:</label>
<input type="text" name="username" class="itxt">
<br>
<br>
<label>用戶密碼:</label>
<input type="password" name="password" class="itxt">
<br>
<br>
<input id="sub_btn" type="submit" value="登錄">
</form>
</div>
</div>
</div>
</div>
</div>
<div id="bottom">
<span>
程式彤卡賓商城.Copyright ©2020.11.12
</span>
</div>
</body>
</html>
登錄成功跳轉頁面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注冊成功頁面</title>
<link rel="stylesheet" href="../../static/css/style.css">
</head>
<body>
<div class="bottom">
</div>
<div id="main">
<h1>登錄成功!<a href="">跳轉到?</a></h1>
</div>
<div id="bottom">
<span>卡賓商城.Copyright ©2020.11.12</span>
</div>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/218611.html
標籤:其他
上一篇:作業系統—銀行家演算法(一個大魚吃小魚的游戲,簡單易懂)
下一篇:面試必備-Java集合框架
