利用重定向实现两个页面的跳转(流程)
1. index.jsp
<html>
<body>
<h2>Hello World!</h2>
//pageContext.request.getContextPath()获取提交请求的当前地址,此处是当前地/login
<form action="${pageContext.request.getContextPath()}/login" method="get">
用户名:<input type="text" name="username"><br>
密 码:<input type="text" name="password"><br>
<input type="submit">
</form>
</body>
</html>
2.web.xml(在此处servlet中找到获取提交请求的当前地址,此处是当前地/login)
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.king.servlet.RequestTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
3.RequestTest (最后走到此处,在实现类中实现重定向)
package com.king.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 RequestTest 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(username+":"+password);
//重定向时候一定要注意,路径问题(404路径问题,500代码错误)
resp.sendRedirect("/response_war/success.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}