使用RequestDispatcher将Servlet转发到JSP导致500内部服务器错误

时间:2015-08-12 17:23:33

标签: java jsp tomcat servlets

知道为什么从servlet转发到JSP可能会导致错误500

这是代码的这一部分:

session.removeAttribute("cdoChosen");
    String nextJSP = "/EndLandingPage.jsp";
     System.out.println("Redirecting to the final landing page with results.");

    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
    dispatcher.forward(request,response);

我在许多其他servlets中使用此类转发而没有任何问题。应用程序的执行日志没有说什么,最后一行是这样的:

Redirecting to the final landing page with results.

您在上面的代码中看到的是System.out.println

的结果

我正在使用AWS的Tomcat v7和Ubuntu操作系统。

我想说唯一的区别是我没有将servlet分配给JSP。见web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>Visma_UploadInterface</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>servlets.LoginServlet</servlet-class>
</servlet>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/main/*</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>MainPageServlet</servlet-name>
        <servlet-class>servlets.MainPageServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MainPageServlet</servlet-name>
        <url-pattern>/mainPage/*</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>MappingServlet</servlet-name>
        <servlet-class>servlets.MappingServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MappingServlet</servlet-name>
        <url-pattern>/mapping/*</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>ListImportServlet</servlet-name>
        <servlet-class>servlets.ListImportServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ListImportServlet</servlet-name>
        <url-pattern>/listImport/*</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>RejectedRecordsServlet</servlet-name>
        <servlet-class>servlets.RejectedRecordsServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>RejectedRecordsServlet</servlet-name>
        <url-pattern>/rejectedRecords/*</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>UploadServlet</servlet-name>
        <servlet-class>servlets.UploadServlet</servlet-class>

    </servlet>



</web-app>

我的doPost()

protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        try{
            String fieldMapping = request.getParameter("fieldMapping");
            HttpSession session = request.getSession();
            String fileName = (String) session.getAttribute("fileName");
            EloquaListImportToCdo listImportClass = new EloquaListImportToCdo(fieldMapping, session, fileName);
            listImportClass.importList();
            String pathStr = (String) session.getAttribute("filePath");
            Path path = Paths.get(pathStr);
            try {
                Files.delete(path);
            } catch (NoSuchFileException x) {
                System.err.format("%s: no such" + " file or directory%n", path);
            } catch (DirectoryNotEmptyException x) {
                System.err.format("%s not empty%n", path);
            } catch (IOException x) {
                // File permission problems are caught here.
                System.err.println(x);
            }
            session.removeAttribute("cdoChosen");
            String nextJSP = "EndLandingPage.jsp";
            System.out.println("Redirecting to the final landing page with results.");

            RequestDispatcher dispatcher = request.getRequestDispatcher(nextJSP);
            dispatcher.forward(request,response);
        }catch (Exception ex){
            System.out.println(ex.toString());
        }
    }

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

RequestDispatcher dispatcher = 
        request.getRequestDispatcher("EndLandingPage.jsp");

如果使用相对路径,则必须使用HttpServletRequest.getRequestDispatcher()ServletContext.getRequestDispatcher()不允许这样做。

答案 1 :(得分:0)

失败的原因是,HTTP请求在长时间加载后已过期,这意味着我正在构建的PDF文件尚未创建。但是,Java程序仍在服务器上运行。

我唯一需要做的就是将其添加到tomcat的httpd.conf

Timeout 600
ProxyTimeout 600

如果您遇到同样的问题,请不要忘记重启整个服务器,而不仅仅是Java服务器。