Search code examples
servletswebsphere-8requestdispatcher

getRequestDispatcher(.).forward(req,res) throws java.io.FileNotFoundException


I have upgraded my Servlet from 2.4 to 3.0 and deployed my application on Websphere 8.5.5.8. Application Server starts properly. When I try to access my home.jsp page in browser it throws:

Controller Main Error OG1000SRVE0190E: File not found: /servlet/com.platform7.affina.operations.servlet.ValidateLoginUser

When I try to debug, code hits my Main Controller Servlet (which is in same package) but inside Controller servlet class I am calling:

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

Which throws:

FileNotFoundException for Servlet/com.platform7.affina.operations.servlet.ValidateLoginUser.

But ValidateLoginUser is in the same package and classes folder location!

Folder structure:

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

ControllerMain.class and ValidateLoginUser.class are in same servlet package.

my Web.xml file:

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

So when I access my URL: it hits ControllerMain.class but inside this class I am calling another servlet which is not part of web.xml but is located in same package of ControllerMain.class.

When I print realpath: this.getServletContext().getRealPath("/"));

I get:

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

I tried using getNamedDispatcher(..) too but throws: null.

Same code works fine on Websphere 7 and even works on Websphere 8.5.5.5


Solution

  • Due to security reasons the default setting for com.ibm.ws.webcontainer.disallowServeServletsByClassname property has been changed.

    Please Note:This APAR has changed the default value of the WebContainer custom property com.ibm.ws.webcontainer.disallowServeServletsByClassname from false to true so that no security threat could occur. Prior to this change, it was up to the developer to remember to change the custom property to true before deploying into production.

    Property Name: com.ibm.ws.webcontainer.disallowServeServletsByClassname Description: If set to true, disallows the use of serveServletsByClassnameEnabled at the application server level, overriding any setting of serveServletsByClassnameEnabled at the application level. This property affects all applications. Values: true(default)/false

    You will need to add that custom property to the Web Container and set it to false for serving servlets by class name.

    But as BalusC suggested, you should add your servlet to web.xml in the form:

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

    and change that forward to:

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

    And do the same with your other class from the same package.