`
i_am_birdman
  • 浏览: 281759 次
  • 性别: 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

    本篇文章将深入探讨`ServletRequest`接口,以及如何使用它来处理来自客户端的请求。 ### ServletRequest接口 `ServletRequest`接口代表了客户端发送到服务器的一个请求,它是`javax.servlet`包下的核心接口。这个...

    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 ...

    javaee-ServletRequest 类相关源代码解析

    本文将深入解析ServletRequest类的相关源代码,帮助你理解其内部工作机制。 首先,ServletRequest接口位于javax.servlet包下,它是所有请求对象(如HttpServletRequest)的基础。它定义了一系列方法,如...

    ServletRequest使用介绍.docx

    `ServletRequest`接口是Java Servlet API中用于处理客户端请求的主要接口,它为开发者提供了获取请求数据的各种方法。`HttpServletRequest`是`ServletRequest`的一个实现类,专门针对HTTP协议,提供了更具体的操作...

    聊天室程序下载

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

    java中需要用到的各种过滤器

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { if (encoding != null) { servletRequest.set...

    jsp和servlet操作mysql中文乱码问题的解决办法.docx

    public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter("encoding"); String value = filterConfig....

    Servlet过滤器大全

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { if (encoding != null) { servletRequest.set...

    Java+javabean

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

    六个有用的java过滤器

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)...

    servlet 过滤器大全

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { if (encoding != null) { servletRequest.set...

    day19_Filter&Listener教案1

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("filterDemo1 被执行了...."); ...

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

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

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

    public void init(FilterConfig filterConfig) throws ServletException {} @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ...

    java中文乱码处理.pdf

    public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter("encoding"); String value = filterConfig....

    好用的过滤器,信不信由你

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)...

    中文乱码问题处理方法.docx

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

    Java几个过滤器学习技巧

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest...

    java中处理乱码的文件

    ServletRequest request = (ServletRequest) servletRequest; try { request.setCharacterEncoding(targetEncoding); filterChain.doFilter(servletRequest, servletResponse); } catch (Exception ex) { ex....

Global site tag (gtag.js) - Google Analytics