response:响应对象
提供的方法:
void Cookie(Cookie cookie)服务端向客户端增加cookie对象
void sendRedirect(String location)throws IOEXception;页面跳转的一种方式
void setContentType(String type)设置服务器响应时候的编码
实例:
login。jsp->check.jsp->success.jsp
请求转发 重定向
地址栏是否改变 不改变 变化
是否保留第一次 保留 不保留
请求时的数据
请求的次数 1 2
跳转发生的位置 客户端 服务端
login.jsp
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2020/6/21
Time: 18:08
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action = "check.jsp" method="post">
用户名:<input type="text"name="uname"><br/>
密码:<input type="password"name="upwd"><br/>
用户名:<input type="submit"value="登录"><br/>
</form>
</body>
</html>
check.jsp
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2020/6/21
Time: 18:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String name=request.getParameter("uname");
String pwd = request.getParameter("upwd");
if (name.equals("zs")&&pwd.equals("abc"))
{
//response.sendRedirect("success.jsp");//d导致数据丢失
//页面跳转,请求转发,可以获取到数据,并且地址栏没有改变
request.getRequestDispatcher("success.jsp").forward(request,response);
}
else{
out.print("1111");}
%>
</body>
</html>
success.jsp
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2020/6/21
Time: 18:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
登陆成功,欢迎恁
<%
String name = request.getParameter("uname");
out.print(name);
%>
</body>
</html>