1.第一步用html先写出简易的用户登录注册页面:
<html>
<head>
<title>第一个web文件</title>
</head>
<body>
<h2>欢迎学习web!</h2>
<a href="Login.jsp">去登录</a><br>
<a href="Zhuce.jsp">去注册</a>
</body>
</html>
![](https://img-blog.csdnimg.cn/img_convert/675558ad919b7cd4ff8119ed6f278029.png)
2.用HTML写出简易的的第二个登录注册页面
<html>
<head>
<title>登录</title>
</head>
<body>
<h1>登录</h1>
<form action="login" method="post">
账号:<input type="text" name="urse" value=""><br>
密码:<input type="password" name="psw" value=""><br>
<input type="submit" value="登录">
</form>
</body>
</html>
![](https://img-blog.csdnimg.cn/img_convert/7482c47e1ede3ae9cc9821fc822a7f3e.png)
3.每一个页面都对应的有一个class文件,在class文件里面用http方法实现“假登录”,例(”注册“同下):
public class Login extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Login-doGet...");
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Login-doPost...");
// 1.从请求中获取用户提交的参数(数据)
request.setCharacterEncoding("utf-8"); // 设置获取数据的格式
String urse = request.getParameter("urse"); //根据表单的name属性获取用户输入的账号密码
String psw = request.getParameter("psw");
System.out.println(urse);
System.out.println(psw);
/*
2.根据用户提交的用户名和密码---去数据库执行查询
select * from users where urse=? and psw=?
假登录*/
// 3.判断成功要干什么? 判断失败要干什么?---做出响应
response.setCharacterEncoding("utf-8"); //设置响应的编码格式
response.setContentType("text/html:charset=UTF-8"); //以什么样的格式(文本/网页)响应
if (urse.equals("张三")&&psw.equals("111")){
// 登陆成功
response.getWriter().write("登陆成功");
}else {
// 登陆失败
response.getWriter().write("登陆失败");
}
}
}
4.发送请求到浏览器,根据用户提交的密码判断用户名和密码是否正确,做出响应:登陆成功或者登陆失败
![](https://img-blog.csdnimg.cn/img_convert/e96d046f29f3edaa13b43a52e712f2b5.png)