1.在默认页面编写登录选项页面
<%--
Created by IntelliJ IDEA.
User: 小m
Date: 2023/2/13
Time: 10:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>淘宝网</title>
</head>
<body>
<h2>欢迎学习WEB!</h2>
<a href="Login.jsp">去登陆</a><br>
<a href="Zhuce.jsp">去注册</a>
</body>
</html>
登陆页面
<%--
Created by IntelliJ IDEA.
User: 小m
Date: 2023/2/14
Time: 10:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆页面</title>
</head>
<body>
<h2>欢迎学习WEB!</h2>
<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>
注册页面
<%--
Created by IntelliJ IDEA.
User: 小m
Date: 2023/2/14
Time: 10:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册页面</title>
</head>
<body>
<h2>欢迎学习WEB!</h2>
<form action="zhuce" method="post">
账号:<input type="text" name="urse" value=""> <br>
密码:<input type="password" name="psw" value=""> <br>
<input type="submit" value="注册">
</form>
</body>
</html>
2.配置处理前端请求的web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 配置servlet类 -->
<servlet>
<!-- 起别名 -->
<servlet-name>login</servlet-name>
<!-- servlet类所在的位置:类的全类名就是 包名.类名 -->
<servlet-class>com.man.servlet.Login</servlet-class>
</servlet>
<!-- Servlet类的映射:Servlet用来处理哪个请求 -->
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>zhuce</servlet-name>
<servlet-class>com.man.servlet.Zhuce</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>zhuce</servlet-name>
<url-pattern>/zhuce</url-pattern>
</servlet-mapping>
</web-app>
3.登陆页面判断登录信息
package com.man.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
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...");
// 三个常用的获取地址请求
// String uri = request.getRequestURI();
// String contextPath = request.getContextPath();
// String servletPath = request.getServletPath();
//
// System.out.println(uri);
// System.out.println(contextPath);
// System.out.println(servletPath);
System.out.println("===================================================");
// 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("登陆失败");
}
}
}
注册页面
package com.man.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Zhuce 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...");
// // 三个常用的获取地址请求
// String uri = request.getRequestURI();
// String contextPath = request.getContextPath();
// String servletPath = request.getServletPath();
//
// System.out.println(uri); //包含项目名和资源路径
// System.out.println(contextPath); //项目名称
// System.out.println(servletPath); //资源路径
System.out.println("===================================================");
// 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("登陆失败");
}
}
}
完整页面如下