设置一个简单的表单:
<html>
<body>
<h2>Hello World!</h2>
<form action="${pageContext.request.contextPath}/login" method="get">
用户名:<input type="text" name="username"> <br>
密码:<input type="password" name="password"><br>
<input type="submit">
</form>
</body>
</html>
注意重定向的路径,否则可能报404,路径中的r1是我的Tomcat的部署名
public class FileServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println("进入了这个请求");
resp.sendRedirect("/r1/success.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<servlet>
<servlet-name>d1</servlet-name>
<servlet-class>com.gao.servlet.FileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>d1</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
创建success,jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>重定向成功了!</h1>
</body>
</html>