Servlet学习之Response和Request
这个放在前面说,说一下请求转发和重定向的的异同
相同点:
不同点:
- 请求转发:url的地址不会发生变化
- 重定向:url的地址跳转到需要转发的页面
Response(重定向)
下边看一个具体的例子
<servlet>
<servlet-name>request</servlet-name>
<servlet-class>com.wu.response.RequestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>request</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<%--index.jsp页面--%>
<html>
<body>
<h2>Hello World!</h2>
<%--这里提交的路径需要找到项目路径--%>
<%--pageContext.request.contextPath 代表当前的项目--%>
<form action="${pageContext.request.contextPath}/login" method="post">
用户名:<input type="text" name="username:"><br>
密  码:<input type="password" name="password:" ><br>
<input type="submit" name="提  交">
</form>
</body>
</html>
<%--success.jsp页面--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Success</h1>
</body>
</html>
package com.wu.response;
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 RequestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendRedirect("/rsp/success.jsp");
}
}
启动之后进入到index.jsp页面,之后输入名字和密码之后点击提交按钮,此时页面就会跳转到success.jsp这个页面,而上边的url也会发生变化,此时的url地址是success.jsp的地址,而不是RequestServlet.java的地址。
Request(请求转发)
这个使用的方法是req.getRequestDispatcher("想要装发到的地址").forward(req,resp);
看一个具体的例子
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>com.zkw.request.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
</head>
<body>
<div>
<form action="${pageContext.request.contextPath}/login" method="post">
用户名:<input type="text" name="username"><br>
密 码:<input type="password" name="pwd"><br>
爱 好:
<input type="checkbox" name="hobbys" value="Python">Python
<input type="checkbox" name="hobbys" value="Java">Java
<input type="checkbox" name="hobbys" value="C">C
<input type="checkbox" name="hobbys" value="C++">C++
<br><br>
<input type="submit">
</for>
</div>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Success</title>
</head>
<body>
<h1>登陆成功了!!!!</h1>
</body>
</html>
package com.zkw.request;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String pwd = req.getParameter("pwd");
String[] hobbys = req.getParameterValues("hobbys");
System.out.println(username);
System.out.println(pwd);
System.out.println(Arrays.toString(hobbys));
req.getRequestDispatcher("/success.jsp").forward(req,resp);
}
}
运行之后先进入到index.jsp这个文件,再点击提交后页面跳转到success.jsp页面但是我们可以发现url还是LoginServlet.java的地址,而不是success.jsp的地址。
|