`
sakakokiya
  • 浏览: 516078 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

国外的一些struts面试题(2)

阅读更多
Question: What is RequestProcessor and RequestDispatcher?
Answer:  The controller is responsible for intercepting and translating user input into actions to be performed by the model. The controller is responsible for selecting the next view based on user input and the outcome of model operations. The Controller receives the request from the browser, invoke a business operation and coordinating the view to return to the client.
The controller is implemented by a java servlet, this servlet is centralized point of control for the web application. In struts framework the controller responsibilities are implemented by several different components like
The ActionServlet Class
The RequestProcessor Class
The Action Class
The ActionServlet extends the javax.servlet.http.httpServlet class. The ActionServlet class is not abstract and therefore can be used as a concrete controller by your application.
The controller is implemented by the ActionServlet class. All incoming requests are mapped to the central controller in the deployment descriptor as follows.
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
</servlet>
All request URIs with the pattern *.do are mapped to this servlet in the deployment descriptor as follows.
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
<url-pattern>*.do</url-pattern>
A request URI that matches this pattern will have the following form.
http://www.my_site_name.com/mycontext/actionName.do
The preceding mapping is called extension mapping, however, you can also specify path mapping where a pattern ends with /* as shown below.
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/do/*</url-pattern>
<url-pattern>*.do</url-pattern>
A request URI that matches this pattern will have the following form.
http://www.my_site_name.com/mycontext/do/action_Name
The class org.apache.struts.action.requestProcessor process the request from the controller. You can sublass the RequestProcessor with your own version and modify how the request is processed.
Once the controller receives a client request, it delegates the handling of the request to a helper class. This helper knows how to execute the business operation associated with the requested action. In the Struts framework this helper class is descended of org.apache.struts.action.Action class. It acts as a bridge between a client-side user action and business operation. The Action class decouples the client request from the business model. This decoupling allows for more than one-to-one mapping between the user request and an action. The Action class also can perform other functions such as authorization, logging before invoking business operation. the Struts Action class contains several methods, but most important method is the execute() method.
public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest request, HttpServletResponse response)    throws Exception;
The execute() method is called by the controller when a request is received from a client. The controller creates an instance of the Action class if one doesn’t already exist. The strut framework will create only a single instance of each Action class in your application.
Action are mapped in the struts configuration file and this configuration is loaded into memory at startup and made available to the framework at runtime. Each Action element is represented in memory by an instance of the org.apache.struts.action.ActionMapping class . The ActionMapping object contains a path attribute that is matched against a portion of the URI of the incoming request.
<action>
path= “/somerequest”
type=”com.somepackage.someAction”
scope=”request”
name=”someForm”
validate=”true”
input=”somejsp.jsp”
<forward name=”Success” path=”/action/xys” redirect=”true”/>
<forward name=”Failure” path=”/somejsp.jsp” redirect=”true”/>
</action>
Once this is done the controller should determine which view to return to the client. The execute method signature in Action class has a return type org.apache.struts.action.ActionForward class. The ActionForward class represents a destination to which the controller may send control once an action has completed. Instead of specifying an actual JSP page in the code, you can declaratively associate as action forward through out the application. The action forward are specified in the configuration file.
<action>
path= “/somerequest”
type=”com.somepackage.someAction”
scope=”request”
name=”someForm”
validate=”true”
input=”somejsp.jsp”
<forward name=”Success” path=”/action/xys” redirect=”true”/>
<forward name=”Failure” path=”/somejsp.jsp” redirect=”true”/>
</action>
The action forward mappings also can be specified in a global section, independent of any specific action mapping.
<global-forwards>
<forward name=”Success” path=”/action/somejsp.jsp” />
<forward name=”Failure” path=”/someotherjsp.jsp” />
</global-forwards>
public interface RequestDispatcher
Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server. The servlet container creates the RequestDispatcher object, which is used as a wrapper around a server resource located at a particular path or given by a particular name.
This interface is intended to wrap servlets, but a servlet container can create RequestDispatcher objects to wrap any type of resource.
getRequestDispatcher
public RequestDispatcher getRequestDispatcher(java.lang.String path)
Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.
The pathname must begin with a “/” and is interpreted as relative to the current context root. Use getContext to obtain a RequestDispatcher for resources in foreign contexts. This method returns null if the ServletContext cannot return a RequestDispatcher.
Parameters:
path - a String specifying the pathname to the resource
Returns:
a RequestDispatcher object that acts as a wrapper for the resource at the specified path
See Also:
RequestDispatcher, getContext(java.lang.String)
getNamedDispatcher
public RequestDispatcher getNamedDispatcher(java.lang.String name)
Returns a RequestDispatcher object that acts as a wrapper for the named servlet.
Servlets (and JSP pages also) may be given names via server administration or via a web application deployment descriptor. A servlet instance can determine its name using ServletConfig.getServletName().
This method returns null if the ServletContext cannot return a RequestDispatcher for any reason.
Parameters:
name - a String specifying the name of a servlet to wrap
Returns:
a RequestDispatcher object that acts as a wrapper for the named servlet
See Also:
RequestDispatcher, getContext(java.lang.String), ServletConfig.getServletName()
分享到:
评论

相关推荐

    struts2面试题

    Struts2 面试题 Struts2 是基于 Java 语言的 Web 应用程序框架,继承自 WebWork 框架。Struts2 的主要特点是使用 FilterDispatcher 作为核心控制器,将请求分发到相应的 Action 中。下面是 Struts2 面试题中涉及到...

    struts2面试题(个人四处搜集,吐血推荐).

    标题中提到的是“struts2面试题”,所以本文的知识点将围绕Struts2框架的面试常见问题展开,包括基础知识点、常用类和工作流程等。 首先,我们需要了解Struts2框架的基础概念。Struts2是一个用于创建企业级Java Web...

    struts面试题详解

    在深入探讨Struts面试题之前,我们首先理解Struts1的基本架构和工作原理。 **1. Struts1框架的核心组件:** - **ActionServlet**:这是Struts1的核心控制器,它是一个实现了Servlet接口的类。当用户发起HTTP请求时...

    Struts2面试题及答案

    ### Struts2面试题及答案解析 #### 一、Struts2框架执行流程与核心组件 **题目背景:** 在Struts2框架中,FilterDispatcher是核心组件之一,它负责处理用户请求,并调用相应的Action进行业务逻辑处理。相较于...

    JAVA笔试面试资料JDBC HTTP、JSP、Servlet、Struts面试题汇总资料.zip

    JAVA笔试面试资料JDBC HTTP、JSP、Servlet、Struts面试题汇总资料: 2014年最新Java笔试题及答案.docx 225道Java面试题 学会了Java面试随你问.docx Ant和Maven的作用是什么?...遇到的一些Java面试题回顾.docx

    Struts2基础面试题

    ### Struts2基础面试题详解 #### 1. Struts2的核心控制器及其性质 - **Struts1的核心控制器**:在Struts1框架中,核心控制器是`ActionServlet`,这是一个实现了`HttpServlet`接口的类,因此本质上是一个Servlet。 ...

    struts2面试题(本人面试题)

    ### Struts1与Struts2的关键区别及其工作流程详解 #### Struts1关键组件与工作流程 **核心组件:** - **ActionServlet:** Struts1的核心控制器,它负责接收HTTP请求,并将其转发到相应的Action。 - **...

    Struts2 框架面试题.CHM

    持续更新ing Struts2 框架面试题汇总。 Struts2 基于 MVC 架构,框架结构清晰,使用 OGNL,提供了全局范围、包范围和Action范围的国际化资源文件管理实现。

    struts1面试题

    以上就是关于Struts1面试题的一些核心知识点,包括其初始化流程、处理用户请求的步骤、数据验证机制以及配置文件中的元素详解。这些内容涵盖了Struts1的基本架构和工作原理,对于理解和解答相关面试问题非常关键。

    Struts2面试题

    ### Struts2面试题知识点详解 #### 一、Struts2的工作流程 1. **请求接收**:所有的请求首先会被`StrutsPrepareAndExecuteFilter`接收。 2. **请求识别**:`StrutsPrepareAndExecuteFilter`判断这个请求是否属于...

    struts2框架面试题

    #### 二、Struts2框架中的绳子问题(武汉晨安科技笔试题) 题目描述了一个经典的面试问题:“如何通过燃烧两条不均匀的绳子来计算出15分钟的时间?”解答此题的关键在于理解绳子燃烧的速度不是均匀的这一前提。具体...

    2014 java servet jdbc struts struts2面试题

    【Struts2面试题】 1. **Struts2架构**:基于MVC模式,了解Action、Result、Interceptor等核心组件。 2. **Struts2拦截器**:自定义拦截器,理解默认拦截器栈及其作用。 3. **Struts2配置**:struts.xml文件,...

    大量的Struts面试题集合

    以下是一些Struts面试题及相关的知识点解析: 1. **什么是Struts框架?** Struts是一个开源的MVC框架,它基于Model-View-Controller设计模式,旨在提高Java Web应用的可维护性和可扩展性。它提供了请求处理、页面...

    struts2框架面试题及答案02

    根据给定的信息,我们将深入探讨Struts2框架中的一些核心概念,包括OGNL表达式的使用、ValueStack的理解及其数据存取方法,以及拦截器的生命周期等。 ### OGNL表达式及其三要素 #### OGNL简介 OGNL(Object-Graph ...

    最全的IT公司面试题集 CHM版的

    外企面试,软件测试面试题,Python面试题,Oracle面试题,MySql面试题,Web开发面试题,Unix面试题,程序员面试,网络技术面试题,网络安全面试题,Linux面试题,Hibernate面试题,Spring面试题,SQL Server面试题,Struts面试题,...

    struts基础面试笔试题及其答案

    Struts 是一个开源的Java Web框架,由Apache软件基金会维护,主要用于构建企业级...以上是关于Struts基础的一些常见面试题和答案,深入理解和掌握这些概念对于Java Web开发者来说非常重要,尤其是在处理企业级应用时。

    spring、Struts2、hibernate常见面试题

    Spring、Struts2、Hibernate面试题详解 本文将详细解释 Spring、Struts2、Hibernate 面试题中的知识点,涵盖了 MVC 模式、Struts1.2 和 Struts2.0 的区别、单例模式、Servlet 依赖、可测试性、项目中使用 SSH 的...

Global site tag (gtag.js) - Google Analytics