菜鸡学jsp(2)

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值