ServletContex&ServletRequest&FilterConfig

关于今天学习Filter的资料及总结:

ServletContext.getRequestDispatcher() vs ServletRequest.getRequestDispatcher()

 

why

getRequestDispatcher(String path) of the ServletRequest interface cannot extend outside the current servlet context

where as

getRequestDispatcher(String path) of the ServletContext can use the getContext(String uripath) method to obtain RequestDispatcher for resources in foreign contexts.

and how??

Please help

 

 

3 Answers

 

 

If you use absolute path ("/index.jsp"), there is no difference.

If you use relative path, you must use HttpServletRequest.getRequestDispatcher() . ServletContext.getRequestDispatcher() doesn't allow it.

For example, if you receive your request on http://example.com/myapp/subdir ,

    


RequestDispatcher
dispatcher 
=
request
.
getRequestDispatcher
(
"index.jsp);
dispatcher.forward( request, response );






Will forward the request to the page http://example.com/myapp/subdir/index.jsp .

In any case, you can't forward request to a resource outside of the context.

 

 

 

 

 

I would think that your first question is simply a matter of scope. The ServletContext is a much more broad scoped object (the whole servlet context) than a ServletRequest, which is simply a single request. You might look to the Servlet specification itself for more detailed information.

As to how, I am sorry but I will have to leave that for others to answer at this time.

 

 

 

Context is stored at the application level scope where as request is stored at page level i.e to say

Web Container brings up the applications one by one and run them inside its JVM. It stores a singleton object in its jvm where it registers anyobject that is put inside it.This singleton is shared across all applications running inside it as it is stored inside the JVM of the container itself.

However for requests, the container creates a request object that is filled with data from request and is passed along from one thread to the other (each thread is a new request that is coming to the server), also request is passed to the threads of same application.

 

 

 

 

ServletContext的作用是什么:

ServletContext.getContect(java.lang.String uripath)的作用是什么?

返回同一Server中指定的path对应的ServletContext对象,通过该对象可以实现与Server中的其他Context打交道。

uripath必须是以"/"开始(该路径的含义是相对于整个Servlet文档的根路径,而不是当前ServletContext的根路径)。

ServletContext.getRequestDispatcher(String url)和ServletRequest.getRequestDispatcher(String url)的区别是什么?为什么?

ServletContext.getRequestDispatcher(String url)中的url只能使用绝对路径;而ServletRequest.getRequestDispatcher(String url)中的url可以使用相对路径。

因为ServletRequest具有相对路径的概念;而ServletContext对象无次概念。

如何把请求转移到另外一个Web App中的某个地址?

ServletContext.getRequestDispatcher(String url)和ServletRequest.getRequestDispatcher(String url)只能把请求转移到同一个Web App中的地址。

如果需要把请求转移到另外一个Web App中的某个地址,可以按下面的做法:

1. 获得另外一个Web App的ServletConext对象(currentServletContext.getContext(uripath)).

2. 调用ServletContext.getRequestDispatcher(String url)方法。

 

 

 

 

翻译   javax.servlet.FilterConfig翻译 

========= START OF TOP NAVBAR ======= --> <!-- -->  
JavaTM 2 Platform
Ent. Ed. v1.4
<!-- ========= END OF TOP NAVBAR ========= --> <!-- ======== START OF CLASS DATA ======== -->

javax.servlet
Interface FilterConfig

public interface FilterConfig

A filter configuration object used by a servlet container to pass information to a filter during initialization. 一个过滤器配置对象,在初始化过程中由servlet容器传递信息给过滤器。

Since:
Servlet 2.3
See Also:
Filter

<!-- ======== NESTED CLASS SUMMARY ======== --><!-- =========== FIELD SUMMARY =========== --><!-- ======== CONSTRUCTOR SUMMARY ======== --><!-- ========== METHOD SUMMARY =========== --><!-- -->

Method Summary
  String getFilterName ()
          Returns the filter-name of this filter as defined in the deployment descriptor. 返回过滤器在部署描述符中定义的名称。
  String getInitParameter ( String  name)
          Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist. 返回命名的初始化参数的对应值,如果参数不存在返回null。
  Enumeration getInitParameterNames ()
          Returns the names of the filter's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the filter has no initialization parameters. 返回以String对象的Enumeration形式的过滤器初始化参数名称,如果过滤器不含初始化参数,返回一个空Enumeration。
  ServletContext getServletContext ()
          Returns a reference to the
ServletContext in which the caller is executing. 返回调用者正在执行的ServletContext的引用。
 

<!-- ============ FIELD DETAIL =========== --><!-- ========= CONSTRUCTOR DETAIL ======== --><!-- ============ METHOD DETAIL ========== --><!-- -->

Method Detail
<!-- -->

getFilterName

public 
String
 
getFilterName
()


Returns the filter-name of this filter as defined in the deployment descriptor. 返回过滤器在部署描述符中定义的名称。
<!-- -->

getServletContext

public 
ServletContext

 getServletContext
()


Returns a reference to the ServletContext in which the caller is executing. 返回调用者正在执行的ServletContext的引用。
Returns:
a ServletContext object, used by the caller to interact with its servlet container 由调用者和servlet容器交互的对象
See Also:
ServletContext
<!-- -->

getInitParameter

public 
String
 
getInitParameter 
(
String
 
name)


Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist. 返回命名的初始化参数的对应值,如果参数不存在返回null。
Parameters:
name - a String specifying the name of the initialization parameter 指定初始化参数名称的String
Returns:
a String containing the value of the initialization parameter 初始化参数的对应String值
<!-- -->

getInitParameterNames

public 
Enumeration

  getInitParameterNames
()


Returns the names of the filter's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the filter has no initialization parameters. 返回以String对象的Enumeration形式的过滤器初始化参数名称,如果过滤器不含初始化参数,返回一个空Enumeration。
Returns:
an Enumeration of String objects containing the names of the filter's initialization parameters 包含过滤器初始化参数的String对象的枚举
<!-- ========= END OF CLASS DATA ========= --> <!-- ======= START OF BOTTOM NAVBAR ====== --> <!-- -->
JavaTM 2 Platform
Ent. Ed. v1.4
<!-- ======== END OF BOTTOM NAVBAR ======= --> Submit a bug or feature

Copyright 2003 Sun Microsystems, Inc. All rights reserved.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ServletRequest是Java Servlet API中的一个接口,它是所有Servlet请求对象的超类。ServletRequest提供了一些方法,用于获取HTTP请求的属性、参数、输入流等信息。开发人员可以利用ServletRequest来处理用户的请求,并根据请求的不同参数和内容来生成响应结果。 除了HttpServletRequest之外,还有其他的子类实现了ServletRequest接口,例如: - javax.servlet.AsyncContext - javax.servlet.ServletRequestWrapper - javax.servlet.http.HttpServletRequest - javax.servlet.http.HttpServletRequestWrapper - javax.websocket.Session 以下是一些常用的ServletRequest的方法: - `getAttribute(String name)`:获取请求属性的值,参数name指定属性名。 - `getAttributeNames()`:获取所有请求属性名的枚举类型。 - `getCharacterEncoding()`:获取请求的字符编码。 - `getContentLength()`:获取请求体的长度。 - `getContentType()`:获取请求体的类型。 - `getInputStream()`:获取请求体的输入流。 - `getParameter(String name)`:获取请求参数的值,参数名由name指定。 - `getParameterMap()`:获取请求参数的Map对象,以参数名作为键,参数值作为值。 - `getProtocol()`:获取请求所使用的协议,如HTTP/1.1。 - `getReader()`:获取请求体的字符输入流。 - `getRemoteAddr()`:获取客户端的IP地址。 - `getScheme()`:获取请求所使用的协议类型,如http、https。 - `getServerName()`:获取服务器的名称。 - `getServerPort()`:获取服务器的端口号。 - `getLocale()`:获取客户端的语言环境。 - `isSecure()`:判断是否是安全传输,如HTTPS。 - `removeAttribute(String name)`:删除请求属性,参数name指定属性名。 - `setAttribute(String name, Object value)`:设置请求属性,参数name指定属性名,value指定属性值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值