新闻资讯
看你所看,想你所想

ServletRequest

ServletRequest

ServletRequest

ServletRequest由Servlet容器来管理,当客户请求到来时,容器创建一个ServletRequest对象,封装请求数据,同时创建一个ServletResponse对象,封装回响数据。这两个对象将被容器作为service()方法的参数传递给Servlet,Servlet利用ServletRequest对象获取客户端发来的请求数据,利用ServletResponse对象传送回响数据。

基本介绍

  • 外文名:ServletRequest
  • 类型:ServletRequest接口
  • 定义:在javax.servlet包中
  • 方法:移除请求中名字为name的属性

定义

interface ServletRequest
在javax.servlet包中。

方法

1、getAttribute
public Object getAttribute(String name)
返回以name为名字的属性的值。如果该属性不存在,这个方法将返回null。
2、getAttributeNames
public Enumeration getAttributeNames()
返回请求中所有可用的属性的名字。如果在请求中没有属性,这个方法将返回一个空的枚举集合。
3、removeAttribute
public void removeAttribute(String name)
移除请求中名字为name的属性。
4.setAttribute
public void setAttribute(String name,Object o)
在请求中保存名字为name的属性。如果第二个参数o为null,那幺相当于调用removeAttribute(name)。
5、getCharacterEncoding
public String getCharacterEncoding()
返回请求正文使用的字元编码的名字。如果请求没有指定字元编码,这个方法将返回null。
6、getContentLength
public int getContentLength()
以位元组为单位,返回请求正文的长度。如果长度不可知,这个方法将返回-1。
7、getContentType
public String getContentType()
返回请求正文的MIME类型。如果类型不可知,这个方法将返回null。
8、getInputStream
public ServletInputStream getInputStream()
返回一个输入流,使用该输入流以二进制方式读取请求正文的内容。javax.servlet.ServletInputStream是一个抽象类,继承自InputStream。
9、getLocalAddr
public String getLocalAddr()
返回接收到请求的网路接口的IP位址,这个方法是在Servlet 2.4规範中新增的方法。
10、getLocalPort
public int getLocalPort()
返回接收到请求的网路接口的IP连线埠号,这个方法是在Servlet 2.4规範中新增的方法。
11、getLocalName
public String getLocalName()
返回接收到请求的IP接口的主机名,这个方法是在Servlet 2.4规範中新增的方法。
12、getParameter
public String getParameter(String name)
返回请求中name参数的值。如果name参数有多个值,那幺这个方法将返回值列表中的第一个值。如果在请求中没有找到这个参数,这个方法将返回null。
13、getParameterNames
public Enumeration getParameterNames()
返回请求中包含的所有的参数的名字。如果请求中没有参数,这个方法将返回一个空的枚举集合。
14、getParameterValues
public String[ ] getParameterValues(String name)
返回请求中name参数所有的值。如果这个参数在请求中并不存在,这个方法将返回null。
15、getReader
public BufferedReader getReader() throws IOException
返回BufferedReader对象,以位元组数据方式读取请求正文。
16、getRemoteHost
public java.lang.String getRemoteHost()
返回传送请求的客户端或最后一个代理伺服器的完整限定名。
17、getRemotePort
public int getRemotePort()
返回传送请求的客户端或者最后一个代理伺服器的IP源连线埠, 这个方法是在Servlet 2.4规範中新增的方法。
18、 getRequestDispatcher
public RequestDispatcher getRequestDispatcher(String path)
返回RequestDispatcher对象,作为path所定位的资源的封装。
19、getServerName
public String getServerName()
返回请求传送到的伺服器的主机名。
20、getServerPort
public int getServerPort()
返回请求传送到的伺服器的连线埠号。
21、setCharacterEncoding
public void setCharacterEncoding(String env)throws UnsupportedEncodingException
覆盖在请求正文中所使用的字元编码的名字。
ServletRequest 和 HttpServletRequest区别
servletRequest是接口,httpServletRequest是实现,但是有些方法是httpServletRequest独有的,比如getSession().。
HttpServletRequest接口是继承自ServletRequest接口的。增加了和HTTP相关的一些方法。而所谓request(在JSP中使用的)其实只是规範中的一个名称而已。它当然是一个对象,但并不是SUN提供的,这是由各个不同的Servlet提供商编写的,SUN只是规定这个类要实现HttpServletRequest接口,并且规定了各个方法的用途,但具体是什幺类是由各个提供商自己决定的。

转载请注明出处海之美文 » ServletRequest

相关推荐

    声明:此文信息来源于网络,登载此文只为提供信息参考,并不用于任何商业目的。如有侵权,请及时联系我们:ailianmeng11@163.com