【问题标题】:getRequestDispatcher(.).forward(req,res) throws java.io.FileNotFoundExceptiongetRequestDispatcher(.).forward(req,res) 抛出 java.io.FileNotFoundException
【发布时间】:2016-02-25 10:06:02
【问题描述】:

我已将我的 Servlet 从 2.4 升级到 3.0,并在 Websphere 8.5.5.8 上部署了我的应用程序。应用程序服务器正常启动。 当我尝试在浏览器中访问我的home.jsp 页面时,它会抛出:

控制器主要错误 OG1000SRVE0190E:找不到文件:/servlet/com.platform7.affina.operations.servlet.ValidateLoginUser

当我尝试调试时,代码会碰到我的主控制器 Servlet(它在同一个包中),但在我调用的控制器 servlet 类中:

this.getServletContext().getRequestDispatcher("Servlet/com.platform7.affina.operations.servlet.ValidateLoginUser").forward(request, response);

哪个抛出:

Servlet/com.platform7.affina.operations.servlet.ValidateLoginUser 的 FileNotFoundException。

ValidateLoginUser 位于同一个包和类文件夹位置!

文件夹结构:

\NEED4SPEEDCell02\operations_1.ear\OperationsWeb.war\WEB-INF\classes\com\platform7\affina\operations\servlet

ControllerMain.classValidateLoginUser.class 在同一个 servlet 包中。

我的Web.xml 文件:

<servlet>
    <servlet-name>servletMain</servlet-name>
    <servlet-class>com.platform7.affina.operations.servlet.ControllerMain</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>servletMain</servlet-name>
    <url-pattern>/controllerMain</url-pattern>
</servlet-mapping>

所以当我访问我的 URL 时:它点击 ControllerMain.class 但在这个类中我正在调用另一个 servlet,它不是 web.xml 的一部分,但位于 ControllerMain.class 的同一包中。

当我打印真实路径时:this.getServletContext().getRealPath("/"));

我明白了:

C:\WebSphere858\AppServer\profiles\AppSrv01\installedApps\NEED4SPEEDCell02\operations_1.ear\OperationsWeb.war

我也尝试使用getNamedDispatcher(..),但抛出:null

相同的代码在 Websphere 7 上运行良好,甚至在 Websphere 8.5.5.5 上运行

【问题讨论】:

    标签: servlets websphere-8 requestdispatcher


    【解决方案1】:

    由于security reasonscom.ibm.ws.webcontainer.disallowServeServletsByClassname 属性的默认设置已更改。

    请注意:此 APAR 已更改 WebContainer 自定义属性 com.ibm.ws.webcontainer.disallowServeServletsByClassnamefalsetrue,这样就不会发生安全威胁。在此更改之前,它 由开发人员决定将自定义属性更改为 在部署到生产环境之前为 true。

    物业名称: com.ibm.ws.webcontainer.disallowServeServletsByClassname 描述: 如果设置为 true,则不允许使用 serveServletsByClassnameEnabled 在应用程序服务器级别,覆盖任何设置 应用程序级别的serveServletsByClassnameEnabled。这 属性影响所有应用程序。值:真(默认)/假

    您需要将该自定义属性添加到 Web 容器并将其设置为 false 以便按类名提供 servlet。

    但正如 BalusC 建议的那样,您应该将您的 servlet 添加到web.xml,格式为:

    <servlet>
        <servlet-name>servletMain</servlet-name>
        <servlet-class>com.platform7.affina.operations.servlet.ValidateLoginUser</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>servletMain</servlet-name>
        <url-pattern>/validateLoginUser</url-pattern>
    </servlet-mapping>
    

    并将其更改为:

    this.getServletContext().getRequestDispatcher("/validateLoginUser").forward(request, response);
    

    对同一个包中的其他类做同样的事情。

    【讨论】:

    • Brilliant Gas,很抱歉回复晚了,因为在您建议的更改之后需要大量部署时间。所以想在评论之前先尝试。非常感谢您的解决方案。它结合了您的解决方案和 BalusC 两种解决方案。我必须相应地更改我的所有 servlet 调用模式 getServletContext().getRequestDispatcher("...") 和 web.xml 。再次感谢。
    • 很高兴加热它。如果将 servlet 添加到 web.xml,并将调度程序更改为使用映射名称,则不再需要设置该属性并在 ibm-web-ext.xml 中设置 serveServletsByClassnameEnabled
    【解决方案2】:

    您似乎依赖于已知存在重大安全漏洞的旧版InvokerServlet。这在 Tomcat 5 和克隆 (WebSphere 4) 中已被弃用,并在 Tomcat 7 和克隆 (WebSphere 6) 中被删除。

    你不应该再使用它了。只需将 servlet 映射到正常的 URL 模式并调用它。假设 servlet 通过 servlet 类上的 @WebServlet("/validateLoginUser") 注释映射到 /validateLoginUser 的 URL 模式,或者通过 servlet 上的 web.xml 映射中的 &lt;url-pattern&gt;/validateLoginUser&lt;/url-pattern&gt; 映射,那么您可以在其上获取请求调度程序,如下所示:

    request.getRequestDispatcher("/validateLoginUser");
    

    或者,只需将共享代码重构为带有方法的普通 Java 类,然后以通常的 Java 方式调用它。如今,将共享验证逻辑紧密耦合在 servlet 中有点奇怪。

    另见:

    【讨论】:

    • 感谢您的友好帮助 Balusc。问题是,我的公司不想做重大的代码更改。否则,我将最好删除所有这些旧的 web.xml 代码遗留模式并删除紧密耦合并继续使用注释。我们需要支持需要 Servlet 3 的 websphere 8.5.5.8。原始代码是使用 Servlet 2.4 返回的。相同的代码适用于 Websphere 7,甚至适用于 Websphere 8.5.5.5,但它开始抱怨 8.5.5.8 :(
    • 感谢 BalusC,您是对的,您的解决方案也有效。
    【解决方案3】:

    为了使上述升级正常工作,我做了一些其他更改,如下所示以供将来参考。

    主要是,我必须更改 websphere 的绑定文件。 以前,我有两个绑定 ibm-web-bnd.xmiibm-web-ext.xmi

    ibm-web-bnd.xmi

    <?xml version="1.0" encoding="UTF-8"?>
    <com.ibm.ejs.models.base.bindings.webappbnd:WebAppBinding xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:com.ibm.ejs.models.base.bindings.webappbnd="webappbnd.xmi" xmi:id="WebAppBinding_1226331113121" virtualHostName="default_host">
      <webapp href="WEB-INF/web.xml#WebApp"/>
        <resRefBindings xmi:id="ResourceRefBinding_1226331113121" jndiName="AffinaDataSource_pma">
          <bindingResourceRef href="WEB-INF/web.xml#ResourceRef_AffinaDataSource_pma"/>
        </resRefBindings>
    </com.ibm.ejs.models.base.bindings.webappbnd:WebAppBinding>
    

    ibm-web-ext.xmi

    <?xml version="1.0" encoding="UTF-8"?>
    <com.ibm.ejs.models.base.extensions.webappext:WebAppExtension 
        xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" 
        xmlns:com.ibm.ejs.models.base.extensions.webappext="webappext.xmi" 
        xmi:id="WebAppExtension_1226331113121"
        serveServletsByClassnameEnabled="true">
      <webApp href="WEB-INF/web.xml#WebApp"/>
      <jspAttributes xmi:id="JSPAttribute_1226331113121" name="reloadEnabled" value="true"/>
      <jspAttributes xmi:id="JSPAttribute_1226331113122" name="reloadInterval" value="10"/>
    </com.ibm.ejs.models.base.extensions.webappext:WebAppExtension>
    

    因此,根据 servlet3 和 Websphere 8.5.5.8,我将上述两个 .xmi 文件替换为 ibm-web-bnd.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-bnd xmlns="http://websphere.ibm.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd" version="1.0">
    
      <virtual-host name="default_host"/>
      <resource-ref name="AffinaDataSourceAlias_pma" binding-name="AffinaDataSource_pma"/>
    
    </web-bnd>
    

    然后在 Websphere 8.5.5.8 上安装应用程序时,它会抛出 outofmemmory 错误,因此要修复我在 wsadmin.bat 中将最大内存参数从 256m 更改为 512m

    C:\WebSphere858\AppServer\bin\wsadmin.bat

    set PERFJAVAOPTION=-Xms256m -Xmx512m -Xquickstart
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      • 2012-12-29
      • 2022-12-11
      • 2019-10-27
      • 2012-11-20
      相关资源
      最近更新 更多