`
i_am_birdman
  • 浏览: 274504 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

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.

分享到:
评论

相关推荐

    javaWEB总结(6):ServletRequest

    javaWEB总结(6):ServletRequest测试代码

    ServletRequest过滤程序

    提供了ServletRequest过滤程序,重新构造对象内容,并有效规避request.getParameter()、request.getInputStream()冲突的问题,同时提供了对跨站脚本攻击XSS和SQL注入的过滤程序。

    购物车源码

    public void doFilter ServletRequest sRequest ServletResponse sResponse FilterChain chain throws IOException ServletException { HttpServletRequest request HttpServletRequest sRequest; String ...

    聊天室程序下载

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (encoding != null) { request.setCharacterEncoding(encoding); ...

    Java+javabean

    The method setAttribute String Object in the type ServletRequest is not applicable for the arguments String int

    ServletRequest使用介绍.docx

    HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求中的所有信息都封装在这个对象中,开发人员通过这个对象的方法,可以获得客户这些信息;

    超全面javaweb教程28天第9天 7 Servlet相关类之ServletRequest和Servletresponse对象的简单介绍

    超全面javaweb教程28天第9天_7_Servlet相关类之ServletRequest和Servletresponse对象

    Machinetool Java项目

    public void init(FilterConfig filterConfig) throws ServletException{ System.out.println("-------------------初始化过滤器--------------------"); } @Override public void doFilter(ServletRequest ...

    javaee-ServletRequest 类相关源代码解析

    NULL 博文链接:https://sunfish.iteye.com/blog/1485374

    c3p0工具包(jdbc)

    public void init(FilterConfig filterConfig) throws ServletException { } private String formatHTML(String str) { if(str==null){ return null; } str = str.replaceAll(", "&lt;sc"); str = str....

    filter-源代码.rar

    doFilter(ServletRequest,ServletResponse,FilterCha):代表filter执行过滤的核心方法,如果某资源在已经被配置到这个filter进行过滤的话,那么每次访问这个资源都会执行doFilter方法 destory():代表是filter销毁...

    Filter_Listener相信代码使用

    import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; /...

    java servlet 监听器

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { MyRequest myRequest = null; HttpServletRequest request2 = ...

    完美解决跨域问题和静态资源冲突的demo(png图片)

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) res; response.setHeader...

    Servlet过滤器使用

    a、init(FilterConfig): 这是Servlet过滤器的初始化方法,Servlet容器创建Servlet过滤器实例后将调用这个方法。在这个方法中可以读取web.xml文件中Servlet过滤器的初始化参数。 b、doFilter(ServletRequest,...

    java web 修改request携带的参数信息

    一个非常棒的程序,用于修改request携带的参数信息。使用了filter等技术。

    Jsp中解决session过期跳转到登陆页面并跳出iframe框架的方法

    当session过期后可以用过滤器来设置重定向页面 代码如下:public class ActionFilter extends ...}public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filter

    java sql注入l

    08 import javax.servlet.FilterConfig; 09 import javax.servlet.ServletException; 10 import javax.servlet.ServletRequest; 11 import javax.servlet.ServletResponse; 12 import javax.servlet....

    JavaWeb核心之Servlet-源代码

    JavaWeb核心之Servlet。...2)service(ServletRequest request,ServletResponse response) 何时执行:每次请求都会执行 ServletRequest :代表请求 认为ServletRequest 内部封装的是 http请求的信息

    JAVA J2EE 类库文档

    FilterConfig GenericServlet HttpServlet HttpServletRequest HttpServletRequestWrapper HttpServletResponse HttpServletResponseWrapper HttpSession HttpSessionActivationListener ...

Global site tag (gtag.js) - Google Analytics