Request & response

For the request and response:

    request -----> HttpServletRequest

    response -----> HttpServletResponse

Every request send by browser will create a new request object, when the response ends, the response object will be destroyed.

And the request and response object are created by the server.

The function of request and response:

  request: handle the http request, get the http request information.

  response: handle the http response, set the http response information.

The response structure:

Redirect:

  We can use response object to redirect the resource:

    1. set the status code : response.setStatus(302);

    2. set the response header : response.setHeader("location","/index.jsp");

But in actual developing:

  response.sendRedirect(String location);

Set the header and control the browser to jump to the specific page in some time:

  response.setHeader("refresh","5;url=/day8_1/refresh.html");

  But in actual developing, we seldom use the server to control the jump in some time, usually accomplish in browser, we can do it  via javascript:

  <head>

    <meta http-equiv="refresh" content="5;url=/day8_1/index.jsp">

    <script type="text/javascript">

      var time = 5;

      function change(){

        var span = getElementById("s");

        span.innerHTML = time;

        time--;

        setTimeout("change()",1000);

      }

    </sctipt>

  </head>

  <body >

    The page will jump to the other page in <span id="s"></span> second.

  </body>

We usually forbid the cache in jsp, but if you want to forbid it in server, you can use the code:

  response.setHeader("pragma","no-cache");

  response.setHeader("cache-control","no-cache");

  response.setDateHeader("expires",-1);

The body part in response is what we see in the browser. And if we want to handle the response body content, we should use response object to get the output stream object.

  PrintWriter getWriter();

  ServletOutputStream getOutputStream();

  When we use getWriter() and when we use getOutputStream()?

    If you want to get the original file and don‘t do any modification, we can use getOutputStream();

    If you wan to manipulate your defined information, we can use getWriter().

  The PrintWriter has two features:

    1. it could auto-refresh.

    2. it could print the information in original type.

Notice:

  1.When you get the output stream via response, the PrintWriter and ServletOutputStream can‘t use both at the same time, you can only use one of them.

  2.When you use the PrinWirter or ServletOutputStream, close the stream is not necessary, the server will close the stream automatically.

The encoding of response:

  1. response.setContentType(String mimeType);   -----> it can not only set the response‘s encoding but also set the browser‘s text encoding

  2. response.setHeader("Content-Type","text/html;charset=utf-8");   -----> it has the same function of the setContentType()

  3. response.setEncoding("utf-8");   -----> it can only set the response‘s encoding, can‘t set the browser‘s encoding, to avoid encoding problems, we should change the setting of browser.

HttpServletRequest:

  Request object could get the http request infomation.

  What‘s the difference of post and get request method?

    1. post could submit big data and the get request could only submit the data less than 1kb

    2. the data submited by post could not be displayed on the browser, while the data submited by get will be displayed on the browser.

    3. the post request‘s paramter located in the request body, while the get request‘s parameter located in the resource path.

时间: 2024-10-12 19:26:15

Request & response的相关文章

【报文】理解HTTP协议的Request/Response(请求响应)模型

[报文]理解HTTP协议的Request/Response(请求响应)模型 系列目录 [简介]"请求/响应"模型 http://www.cnblogs.com/engraver-lxw/p/7550514.html [原理]理解HTTP协议的Request/Response(请求响应)模型 http://www.cnblogs.com/engraver-lxw/p/7550691.html [报文]理解HTTP协议的Request/Response(请求响应)模型--当前 http:/

request \response 总结

request&response request 1.获得信息的方法     1> 获得请求首行信息的方法         *getMethod         *getContextPath ==> /项目名         *getServletPath ==> /XXXServlet         getRequestURI ==> /项目名/XXXServlet         getSchema==> HTTP     2>获得请求头的方法     

chain.doFilter(request,response)含义

过滤器的生命周期一般都要经过下面三个阶段: 初始化 当容器第一次加载该过滤器时,init() 方法将被调用.该类在这个方法中包含了一个指向 Filter Config 对象的引用.我们的过滤器实际上并不需要这样做,因为其中没有使用初始化信息,这里只是出于演示的目的. 过滤 过滤器的大多数时间都消耗在这里.doFilter方法被容器调用,同时传入分别指向这个请求/响应链中的 Servlet Request.Servlet Response 和 Filter Chain 对象的引用.然后过滤器就有机

request&amp;response

HttpServletRequest&HttpServletResponse. web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象和代表响应的response对象.如果要获取客户机提交的数据,只需要request对象,要想客户机输出数据,只需要response对象. 一.Response    1.Resonse的继承结构:            ServletResponse--HttpServletResponse    2.Response

request response 区别

request:有关于客户端请求的信息,都可以由它来取得,例如请求标头.请求方法.请求参数.使用者IP等等信息.response:有关于对客户端请求之响应,可以利用它来设定一些要响应的讯息,例如标题信息.响应状态码等. response: 属于重定向请求: 其地址栏的URL会改变: 会向服务器发送两次请求: sendRedirect用法:       response.sendRedirect("跳转页面路径或需要执行的操作路径");       不保持request中设置属性,也就是

过滤器中的chain.doFilter(request,response)

先来说说过滤器filter的作用,它就相当于公路上的一个关卡,对要通过的拦截下来进行相关操作或放行. dofilter作用[request -> filter1 -> filter2 ->filter3 -> .... -> request resource.] 先写一个简单的没有filter的登录例子,当用户名和密码都是admin时,能跳转到success页面,否则到fail页面. 1.eclipse建立一个web project ,结果目录如下 其中,jsp很简单.在lo

//可以不保存在session中, 并且前面我保存在request,这里session也可以获取 chain.doFilter(request, response); //只有登录名不为空时放行,防止直接登录 成功的页面

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httprequest = (HttpServletRequest)request; HttpServletResponse httpresponse = (HttpServletResponse)res

UDP 網路程式設計 (2) --- request / response

在 Oracle 網站上,有個簡單的 UDP 程式範例 --- Lesson: All About Datagrams,這個程式和前一個程式基本上是差不多的,差別在於,它是由 client 先送個 request 給 server 端,server 端接收到後,回應訊息給 client 端,下面是 Oracle 的範例程式. 1 package idv.steven; 2 3 import java.io.*; 4 import java.net.*; 5 import java.util.*;

servlet下的request&amp;&amp;response

request的方法 *获取请求方式: request.getMethod(); * 获取ip地址的方法 request.getRemoteAddr(); * 获得用户清气的路径; * request.getRequestURL().tostring();    --- url * request.getRequestURI()                --- uri *获得工程名; * request.getContextPath(); *在接受数据前设置编码 * post请求 * re

【转】对Django框架架构和Request/Response处理流程的分析

本文转载于疯狂的蚂蚁. 一. 处理过程的核心概念 如下图所示django的总览图,整体上把握以下django的组成: 核心在于中间件middleware,django所有的请求.返回都由中间件来完成. 中间件,就是处理HTTP的request和response的,类似插件,比如有Request中间件.view中间件.response中间件.exception中间件等,Middleware都需要在 "project/settings.py" 中 MIDDLEWARE_CLASSES 的定