学习——JavaWeb06:response,cookie

学习——JavaWeb06:response,请求转发重定向

Ø  Response响应对象

1、        提供的方法:

void addCookie(Cookie cookie);服务器向客户端增加cookie对象

void sendRedirect(String location) throws IOException:重定向

void setConterType(String type):设置服务端响应的编码(设置服务端的content类型)

2、        重定向和请求转发的区别

1、 请求转发:getRequestDispatcher(“b.jsp”).forward(request,response);请求转发的方式跳转页面 A->B

2、 重定向:void sendRedirect(String location) throws IOException:

  • 地址栏是否改变:转发不变定向变;
  • 是否保留第一次请求时的数据:转发保留定向丢;
  • 请求的次数:转发一次定向俩;
  • 效率:转发很高定向慢;

     

2.Cookie

1、Cookie(客户端,不是内置对象):是由服务端产生的,再发送给客户端保存。

相当于 本地缓存的作用: 客户端——>服务端

 

作用:可以提高访问服务端的效率,但是安全性较差。

Cookie:key-value

Javax.servlet.http.Cookie

Public Cookie (String name,String value);

String getName();

String getValue();

Void setMaxAge(int expiry);最大有效期(秒)

 

服务端发送给客户端:

Response.addCookie(Cookie cookie)

页面跳转(转发,重定向)

客户端获取cookie:response对象,getCookies();客户端必须一次全部拿到,不能单独获取一个cookie;

通过F12可以发现除了自己设置的cookie对象外,还有一个 key 为 JSESSIONID的cookie

代码

 

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

   <form action="check.jsp" method="post">

      用户名:<input type="text" name="name"/><br>

      密码:<input type="password" name="password"/><br>

      <input type="submit" name="登录" value="登录"/><br>

   </form>

</body>

</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

   <%

      request.setCharacterEncoding("utf-8");

     

      String name = request.getParameter("name");

      String pwd = request.getParameter("password");

     

      if(name.equals("zs")&&pwd.equals("123")){

         //response.sendRedirect("success.jsp");//导致数据丢失;

        

         //可以获取数据,而且地址栏没有改变(仍然保持转发时的页面:check.jsp)

         request.getRequestDispatcher("success.jsp").forward(request, response);

      }

      else{

         out.println("登录失败!");

      }

   %>

</body>

</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

   登陆成功:<br>

   欢迎您:

   <%

      String name = request.getParameter("name");

      out.println(name);

   %>

</body>

</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

   <%

      //服务端

      Cookie cookie1 = new Cookie("name","zs");

      Cookie cookie2 = new Cookie("password","123");

     

      response.addCookie(cookie1);

      response.addCookie(cookie2);

     

      //通过重定向或者请求转将 发页面跳转到客户端

      response.sendRedirect("request.jsp");

   %>

</body>

</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

   <%

      //尝试获取cookie

      Cookie[] cookies = request.getCookies();

  

      for(Cookie cookie:cookies){

         out.print(cookie.getName()+"-----"+cookie.getValue()+"<br/>");

         out.print("--------------------------<br/>");

      }

   %>

</body>

</html>

posted on 2018-11-01 10:14  一念心然  阅读(1018)  评论(0编辑  收藏  举报

导航