zoukankan      html  css  js  c++  java
  • jsp课程笔记(二)

    在这里插入图片描述
    在这里插入图片描述

    1. response :响应对象 提供的方法: void addCookie( Cookie cookie );
      服务端向客户端增加cookie对象 void sendRedirect(String location ) throws
      IOException; :页面跳转的一种方式(重定向) void setContetType(String
      type):设置服务端响应的编码(设置服务端的contentType类型)

    2. 示例:登陆
      login.jsp -> check.jsp ->success.jsp
      login.jsp

    <%@ 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="uname"><br/>
    		密码:<input type="password" name="upwd"><br/>
    		<input type="submit" value="登陆"><br/>
    		
    	</form>
    </body>
    </html>
    

    check.jsp

    <%@ 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("uname");
    			String pwd = request.getParameter("upwd");
    			if(name.equals("zs") && pwd.equals("abc")){//假设 zs abc
    				response.sendRedirect("success.jsp") ;//页面跳转:重定向, 导致数据丢失
    				//页面跳转:请求转发, 可以获取到数据,并且 地址栏 没有改变(仍然保留 转发时的页面check.jsp)
    				//request.getRequestDispatcher("success.jsp").forward(  request,response);
    			}else{
    				//登陆失败
    				out.print("用户名或密码有误!") ;
    			}
    		
    		%>
    </body>
    </html>
    

    success.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    		登录成功!<br/>
    		欢迎您:
    		<%
    			  String name = request.getParameter("uname") ;
    				out.print(name) ;
     		%>
    </body>
    </html>
    

    重定向结果:
    在这里插入图片描述
    请求转发结果:
    在这里插入图片描述

    请求转发重定向
    地址栏是否改变不变(check.jsp)改变(success.jsp)
    是否保留第一次请求时的数据保留不保留
    请求的次数12
    跳转发生的位置服务端客户端发出的第二次跳转
    • 转发、重定向
      转发:
      张三(客户端) -> 【 服务窗口 A (服务端 ) -> 服务窗口B 】

    • 重定向:
      张三(客户端) -> 服务窗口 A (服务端 ) ->去找B

      张三(客户端) -> 服务窗口 B (服务端 ) ->结束

    2、

    • session(服务端)
    • Cookie(客户端,不是内置对象):Cookie是由 服务端生成的 ,再发送给客户端保存。
      相当于 本地缓存的作用: 客户端(hello.mp4,zs/abc)->服务端(hello.mp4;zs/abc)
      作用:提高访问服务端的效率,但是安全性较差。
    Cookie:	name=value   
    javax.servlet.http.Cookie
    public Cookie(String name,String value)
    String getName():获取name
    String getValue():获取value
    void setMaxAge(int expiry);最大有效期 (秒)
    
    • 服务端准备Cookie:

      response.addCookie(Cookie cookie)
      页面跳转(转发,重定向)
      客户端获取cookie: request.getCookies();

    • a.服务端增加cookie :response对象;客户端获取对象:request对象

    • b.不能直接获取某一个单独对象,只能一次性将 全部的cookie拿到

    cookie案例
    在webcontext下创建cookie文件夹

    response_addCookie.jsp

    <%@ 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("pwd","abc");
    		
    		response.addCookie(cookie1);
    		response.addCookie(cookie2);
    		
    		//页面跳转到客户端(转发或重定向跳过去)
    		response.sendRedirect("result.jsp");
    	%>
    </body>
    </html>
    

    result.jsp

    <%@ 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[] cookies = request.getCookies();
    	
    			for(Cookie cookie:cookies){
    				out.print(cookie.getName()+"--"+cookie.getValue()+"<br/>");
    			}
    	
    	%>
    </body>
    </html>
    

    访问:http://localhost:8080/01_jsp/cookie/responseaddCookie.jsp 跳转到 http://localhost:8080/01_jsp/cookie/result.jsp
    在这里插入图片描述
    通过F12可以发现 除了自己设置的Cookie对象外,还有一个name为 JSESSIONID的cookie
    JSESSIONID为cookie默认自带的
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    Remote Address: [::1]:8080 相当于 127.0.0.1:8080
    Status Code:状态码,以3开头的一般为重定向
    重定向、超链接、地址栏直接访问为get方式请求

  • 相关阅读:
    Spring事务的传播行为、隔离级别、回滚、只读和过期
    Spring 使用XML文件的方式配置事务
    SpringJDBCTemplate
    Spring事物管理
    【运维技术】Zookeeper单机以及集群搭建教程
    【运维技术】kafka三实例集群环境搭建及测试使用
    【运维技术】redis(一主两从三哨兵模式搭建)记录
    【运维技术】shell脚本实现线程挂掉,自动重启功能
    【运维技术】slc pm 启动不了,异常排除问题记录
    【知识总结】Java类初始化顺序说明
  • 原文地址:https://www.cnblogs.com/xdr630/p/15255111.html
Copyright © 2011-2022 走看看