request中的那些方法到底是干什么的?
最近做Java Web项目,在.jsp页面和servlet之间request和response还是有些混淆,查阅了一些资料,总结如下,方便以后使用:
首先,servlet接口是最基本的,提供的五个方法也不多说了,关键是了解清楚什么是request,什么事response。
| void | destroy() Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. |
| ServletConfig | getServletConfig() Returns a ServletConfig object, which contains initialization and startup parameters for this servlet. |
| java.lang.String | getServletInfo() Returns information about the servlet, such as author, version, and copyright. |
| void | init(ServletConfig config) Called by the servlet container to indicate to a servlet that the servlet is being placed into service. |
| void | service(ServletRequest req, ServletResponse res) Called by the servlet container to allow the servlet to respond to a request. |
简单来说:
request对象是服务器对浏览器请求的封装,而response是服务器对服务器响应的封装。
request用来取出请求信息,而response则用来添加要返回给浏览器的信息。
从常用的途径来说:
request 是请求的载体,它附带着,请求的参数,ip,cookie,session,表单,字符编码,或者上传文件。你可以通过request去获得你所需要的信息。
response是响应的载体,你可以设置,表头,报文,响应文件类型,字符编码,response,自带一个输出流,当你需要将一个页面或者一个文件传到客户端时,你可以通过这个流来进行操作。
说到这里,我们不得不提一下request中最常用的几个方法(可能是我项目经验少,用到的不多),
| java.lang.Object | getAttribute(java.lang.String name) Returns the value of the named attribute as an Object, or null if no attribute of the given name exists. |
| java.lang.String | getParameter(java.lang.String name) Returns the value of a request parameter as a String, or null if the parameter does not exist. |
| void | setAttribute(java.lang.String name, java.lang.Object o) Stores an attribute in this request. |
很奇怪,有getAttributes()和setAttributes(),有getParameter()却没有setParameter()?那什么时候用getAttributes()什么时候用getParameter()呢?
1. 好,首先来看getParameter(),getParameter()用来去post或者get提交表单时候,获得表单里的数据,或者是url上的参数,
什么是表单里的数据呢?看这段代码:
<form action="HelloFormServlet" method="GET">
网址名:<input type="text" name="name">
<br />
网址:<input type="text" name="url" />
<input type="submit" value="提交" />
</form>
HTML 表单和提交按钮传递两个值。我们将使用HelloFormServlet 来处理输入
在Servlet中:
String name =new String(request.getParameter("name");
String url = new String(request.getParameter("url"));
那什么是url上的参数呢?比如这样的:
<a href="/ControlCenterServlet?NewTopic.jsp&BoardID=1">××</a>
获取方式也是通过getParameter():
request.getParameter("BoardID");
request.getParameter()方法传递的数据,会从Web客户端传到Web服务器端,代表HTTP请求数据。request.getParameter()方法返回String类型的数据。
2.而setAttributes()和getAttribute()发生在,当两个Web组件之间为转发关系时,转发目标组件通过setAttributes(),getAttribute()方法来和转发源组件共享request范围内的数据。
假定a.jsp和b.jsp之间为转发关系
a.jsp希望向b.jsp传递当前的用户名字,如何传递这一数据呢?先在a.jsp中调用setAttribute()方法:
<%
String username=request.getParameter("username");
request.setAttribute("username",username);
%>
<!-- 转发到b.jsp -->
<jsp:forward page="b.jsp" />
在b.jsp中通过getAttribute()方法获得用户名字:
<% String username=(String)request.getAttribute("username"); %>
username: <%= username %>
request.setAttribute()和getAttribute()方法传递的数据只会存在于Web容器内部,在具有转发关系的Web组件之间共享。这两个方法能够设置Object类型的共享数据。
这篇博客将上述三个方法概括为:
http://blog.csdn.net/zkflame/article/details/51096836
request.getParameter()取得是通过容器的实现来取得通过类似post,get等方式传入的数据。
request.setAttribute()和getAttribute()只是在web容器内部流转,仅仅是请求处理阶段。
getAttribute是返回对象,getParameter返回字符串
总的来说:
request.getAttribute()方法返回reques,sessiont范围内存在的对象
request.getParameter()方法是获取http提交过来的数据
request中的那些方法到底是干什么的?的更多相关文章
- Request中的各种方法
前言 Request中方法众多,对于Java Web程序员来说,种种方法都会在工作中常常用到.Request由于不是JDK的一部分,这些方法的用法也没有专门的API可以查,所以在工作中遇到Reques ...
- spinner中的onNothingSelected方法到底什么时候调用?
这个东东大家在开发中可能不太能用到,所以总是容易被忽略,由于工作原因,我最近琢磨了一下onNothingSelected方法的调用时机问题,其实很简单,只要我们稍微看一下源码就明白了: /** * C ...
- java.lang.String中的replace方法到底替换了一个还是全部替换了。
你没有看错我说的就是那个最常用的java.lang.String,String可以说在Java中使用量最广泛的类了. 但是我却发现我弄错了他的一个API(也可以说是两个API),这个API是关于字符串 ...
- nginx 自动忽略request中header name包含下划线参数的解决方法
使用nginx过程中遇到了个问题,就是request中的header name中如果包含下划线会自动忽略掉,导致服务器接收不到该字段的内容,以下为解决方法: nginx默认request的header ...
- Java--获取request中所有参数的方法
我们通常用到request获取某个参数的方法: String value=request.getparameter("key"); 如果想要获取request中所有的参数呢? re ...
- Spring中获取request的几种方法,及其线程安全性分析
前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...
- laravel中请求用例$request可用的一些方法小结
laravel中$request可用的一些方法小结 1,请求方法的获取 $method = $request->method(); 2,检测请求方法 $res = $request->is ...
- [No000016E]Spring 中获取 request 的几种方法,及其线程安全性分析
前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...
- Spring中获取request的几种方法,及其线程安全性分析(山东数漫江湖)
前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...
随机推荐
- 查看SQL Server中的锁表及解锁
有时候系统很慢,有可能是SQL Server数据库中某些表被锁定 --查看被锁表(需查多几次,有些临时锁很快会自动解锁): SELECT request_session_id AS spid, OBJ ...
- pycharm乱码
1.文件编码类型 2.查看IDE编码类型
- Oracle 内置函数
三. 字符函数(可用于字面字符或数据库列) 1,字符串截取 select substr('abcdef',1,3) from dual 2,查找子串位置 select instr('abcfdgfdh ...
- R语言基础命令与安装
1. R的安装过程 1.1.首先附上清华线路的下载链接Windows版3.3.1 1.2. 选择安装路径 1.3. 注意根据自己的计算机位数选择,如我的是64位,便选择64位安装. 1.4. 其他默认 ...
- iOS沙盒(sandbox)机制及获取沙盒路径
一.每个iOS应用SDK都被限制在沙盒中,沙盒相当于一个加了仅主人可见权限的文件夹,苹果对沙盒有以下几条限制. (1).应用程序可以在自己的沙盒里运作,但是不能访问任何其他应用程序的沙盒. (2).应 ...
- dojo事件绑定
Dojo如何动态绑定事件的小demo <html> <title>事件绑定测试test</title> <head></head> < ...
- SPFieldLookupValue class
using System; using Microsoft.SharePoint; namespace ConsoleApp { class Program { static void Main(st ...
- java:数据库操作JDBC
JDBC详解:https://www.cnblogs.com/erbing/p/5805727.html JDBC存储过程,事务管理,数据库连接池,jdbc的封装框架:https://www.cnbl ...
- nodejs+MQTT协议实现远程主机控制
摘抄自百度:MQTT(MessageQueuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,有可能成为物联网的重要组成部分. 所谓物联网,就是“万物互 ...
- leetcode: 哈希——two-sum,3sum,4sum
1). two-sum Given an array of integers, find two numbers such that they add up to a specific target ...