登录的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>