实现类
public class RequestRedirect 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
resp.sendRedirect("/Response_war/success.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
修改index.jsp
<html>
<body>
<h2>Hello World!</h2>
<%--这里提交的路径 需要找到项目的路径
${pageContext.request.contextPath}代表当前的项目
--%>
<form action="${pageContext.request.contextPath}/req" method="get">
用户名: <input type="text" name="username"><br>
密码: <input type="password" name="password"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
添加success.jsp页面 可供重定向
在web.xml中注册Servlet
<!-- 注册 请求重定向的Servlet-->
<servlet>
<servlet-name>req</servlet-name>
<servlet-class>com.csnz.servlet.RequestRedirect</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>req</servlet-name>
<url-pattern>/req</url-pattern>
</servlet-mapping>
如果重定向的时候这样子写
resp.sendRedirect("/success.jsp");
结果...
面试题: