zoukankan      html  css  js  c++  java
  • 【读书笔记】之《head first Servlet and Jsp》 第四章 请求与响应

    本章大纲

    1.什么是servlet?

    2.Servlet的任务是什么?

    3.http方法

    4.GET请求和POST请求的区别

    5.怎么确定浏览器发送的是get请求还是post请求

    6 HttpServletRequest接口

    7 HttpServletResponse接口

    8 Servlet的生命周期

    9 Servlet的继承关系

    10 Servlet生命周期的三个重要时刻

    11 ServletConfig和ServletContext

    12 HttpServletRequest中关于port的三个方法

    13 servlet什么时候被初始化,什么时候被销毁?

    14 HttpServletResponse的三个header()方法

    15 转发和重定向

    1.什么是servlet?

    Servlet是一种独立于平台和协议的服务器端的技术,可以生成动态的web页面。

    2.Servlet的任务是什么?

    得到一个用户的请求,再发回一个响应。

    3.http方法

    一共有8种:get,post,put,delete,head,trace,options,connect

    备注:servlet API中没有处理doConnect()的机制,所以HttpServlet里没有doConnect().

    GET请求和POST请求的区别

    • 从传输数据量大小来说,get请求传输数据小,post请求无限制;
    • 从安全性来说,get请求把参数写在请求行,不安全,post请求安全;
    • 从书签来说,get请求可以建立书签,post请求不可以建立书签;
    • 从幂等来说,get请求是幂等的,它不会修改服务器上的任何内容;post请求不是幂等的,可能修改服务器资源的请求;

    怎么确定浏览器发送的是get请求还是post请求

      get请求:

    • 简单的超链接往往意味着get请求
      <a href="www.baidu.com">百度</a> 
    • form表单的method方法指定get请求或者默认为get请求
      <form method="POST" action="selectBee.do">
          <input type="submit"/>
      </form>
      

        

      post请求:

    • form表单的method方法指定post请求
      <form method="POST" action="selectBee.do">
          <input type="submit"/>
      </form>

       如果想让servlet同时支持GET和POST请求,怎么做?

    public void doPost() throws Exception{
         doGet(req,resp);  
    }
    public void doPost() throws Exception{
         doGet(req,resp);  
    }

     6 HttpServletRequest接口

    • 从请求中获取HTML表单参数:req.getParameter():String   req.getParameterValues():String[]
    • 获取HTTP请求首部信息:req.getHeader(String ):String
    • 从请求中获取cookie:req.getCookies():cookie[ ]
    • 从请求中获取session:req.getSession();
    • 获取http方法:req.getMethod();
    • 获取输入流:req.getInputStream()

     7 HttpServletResponse接口

    • 编写代码设置http响应首部:resp.addHeader(String name,String value):void
    • 设置响应的内容类型:resp.setContentType(String ):void
    • 为响应获得一个文本流:resp.getWriter():PrintWriter
    • 为响应获得一个二进制流:resp.getOutputStream():ServletOutputStream
    • 把一个http请求重定向到另一个url:resp.encodeRedirectURL(String url):String
    • 向响应增加cookie:resp.addCookie(Cookie ):void

    8 Servlet的生命周期

    servlet类加载    servlet实例化    init()    service()    destroy()

    9 Servlet的继承关系

    Servlet(interface) <------ GenericServlet <------ HttpServlet <------ MyServlet

    10 Servlet生命周期的三个重要时刻

    • init() : Servlet实例创建之后,并在servlet能为客户请求提供服务之前,容器会在servlet实例上调用init();
    • service() : 第一个用户请求到来时,容器会开始创建(或找到)一个线程,并调用servlet的service()方法;
    • doGet()或doPost() : service()方法根据请求的http方法来调用doGet()或doPost()。

    11 ServletConfig和ServletContext

    见第五章

     12 HttpServletRequest中关于port的三个方法

    • getServerPort():得到发送请求的客户的端口号
    • getLocalPort():得到发送的端口号
    • getServerPort():得到最后发送的端口号

    13 servlet什么时候被初始化,什么时候被销毁?

    servlet被初始化:容器要加载类,调用servlet的无参构造函数,并调用servlet的init()方法,从而初始化servlet

    servlet被销毁:关闭服务器,调用servlet的destroy方法销毁servlet。

    14 HttpServletResponse的三个header()方法

    • response.setHeader("foo","bar");如果没有增加,有的话修改
    • response.addHeader("foo","bar");增加一个首部值
    • response.setIntHeader("foo",42);确定首部值是一个整数

    15 转发和重定向

    重定向:之前的是http://www.wickedlysmart.com/myApp/cool/bar.do

    • url不以斜线开头 sendRedirect("foo/stuff.html") 则跳转到http://www.wickedlysmart.com/myApp/cool/foo/stuff.html
    • url以斜线开头 sendRedirect("/foo/stuff.html") 则跳转到http://www.wickedlysmart.com/foo/stuff.html

    注意:sendRedirect()取一个String,而不是URL对象,比如sendRedirect(new URL("http://www.oreilly.com"));就是错误的。

    转发:

    RequestDispatcher view = request.getRequestDispatcher("result.jsp");
    view.forward(req,resp);

        

    Ride the wave as long as it will take you.
  • 相关阅读:
    权限管理系统(五):RBAC新解,基于资源的权限管理
    HTTP报文头Accept和Content-Type总结
    Spring Security教程(七):RememberMe功能
    Spring Security教程(六):自定义过滤器进行认证处理
    权限管理系统(三):自定义开发一套权限管理系统
    Spring Security教程(五):自定义过滤器从数据库从获取资源信息
    Spring Security教程(八):用户认证流程源码详解
    Spring Security教程(四):自定义登录页
    Spring Security教程(三):自定义表结构
    golang 做了个mutex与atomic性能测试
  • 原文地址:https://www.cnblogs.com/jianpanaq/p/7201168.html
Copyright © 2011-2022 走看看