user7693832
user7693832

Reputation: 6849

Why I can not get the jsp thought its path?

I use struts in my Java EE project:

In my loading.jsp if I use the below src, I will get 404 error:

<IFRAME src="${pageContext.request.contextPath}/WEB-INF/page/menu/alermDevice.jsp" name="dev" id="dev" frameBorder="0" width="500" scrolling="auto" height="400">
</IFRAME>

But if I use below src:

<IFRAME src="elecMenuAction_alermDevice.do" name="dev" id="dev" frameBorder="0" width="500" scrolling="auto" height="400">
</IFRAME>

I will get the correct information.

This is my struts.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

    <constant name="struts.devMode" value="true"></constant>
    <constant name="struts.ui.theme" value="simple"></constant>
    <constant name="struts.action.extension" value="do"></constant>

    <package name="system" namespace="/system" extends="struts-default">
        <action name="elecTextAction_*" class="elecTextAction" method="{1}">
            <result name="save">/system/textAdd.jsp</result>
        </action>
        <action name="elecMenuAction_*" class="elecMenuAction" method="{1}">
            <result name="menuHome">/WEB-INF/page/menu/home.jsp</result>
            <result name="title">/WEB-INF/page/menu/title.jsp</result>
            <result name="left">/WEB-INF/page/menu/left.jsp</result>
            <result name="change">/WEB-INF/page/menu/change.jsp</result>
            <result name="loading">/WEB-INF/page/menu/loading.jsp</result>
            <result name="logout" type="redirect">index.jsp</result>
            <result name="alermStation">/WEB-INF/page/menu/alermStation.jsp</result>
            <result name="alermDevice">/WEB-INF/page/menu/alermDevice.jsp</result>
        </action>
    </package>
</struts>

Why I use the path can not access the JSP? only use the action I can get it yet?

Upvotes: 1

Views: 285

Answers (1)

Roman C
Roman C

Reputation: 1

The web server cannot get resources from and below WEB-INF folder. When action is invoked it returns a response as an execution of the result. It's used a result type dispatcher which is used by default to forward request to the specified URL (the requested JSP page).

Dispatcher Result

Includes or forwards to a view (usually a jsp). Behind the scenes Struts will use a RequestDispatcher, where the target servlet/JSP receives the same request/response objects as the original servlet/JSP. Therefore, you can pass data between them using request.setAttribute() - the Struts action is available.

There are three possible ways the result can be executed:

  • If we are in the scope of a JSP (a PageContext is available), PageContext's PageContext#include(String) method is called.

  • If there is no PageContext and we're not in any sort of include (there is no "javax.servlet.include.servlet_path" in the request attributes), then a call to RequestDispatcher#forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse) is made.

  • Otherwise, RequestDispatcher#include(javax.servlet.ServletRequest, javax.servlet.ServletResponse) is called.


When servlet dispatcher is invoked it has not such restriction and can return resource with the same response that was originally requested.

Upvotes: 1

Related Questions