1,先写一个页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>页面</title>
</head>
<body>
<h2>页面</h2>
<a href="denglu.jsp">登录</a>
<a href="zhuce.jsp">注册</a>
</body>
</html>
如图所示:
2,再写一个点击登录跳转
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
</head>
<body>
<h2>登录</h2>
<form method="post" action="denglu">
账号:<input name="user" type="text" value=""><br>
密码:<input name="pwd" type="password" value=""><br>
<input type="submit" value="登录">
</form>
</body>
</html>
如图所示:
3,再写一个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-name>denglu</servlet-name>
<servlet-class>com.w.Servle.denglu</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>denglu</servlet-name>
<url-pattern>/denglu</url-pattern>
</servlet-mapping>
</web-app>
4,最后创建一个java类用来继承HttpServlet
package com.w.Servle;
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 denglu extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("utf-8");
String contextPath=req.getParameter("user");
String pwd=req.getParameter("pwd");
System.out.println(contextPath);
System.out.println(pwd);
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=UTF-8");
if(contextPath.equals("11111")&&pwd.equals("22222")){
resp.getWriter().write("登录成功");
}else {
resp.getWriter().write("登录失败");
}
}
}
如图所示: