如何将来自Servlet的调用转发到Jsp文件

时间:2013-08-01 14:00:40

标签: java jsp servlets java-ee-7

我很抱歉改变了我的整个概念。

我想将呼叫从Servlet转发到Jsp。转发时间我会传递参数。

Sevlet计划。

 protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    //Creating object to FunctionNames class for calling their functions 
        try {
            FunctionNames FunctionNamesObject=new FunctionNames();
            //calling Method from class object
            List<File> finalListNames=FunctionNamesObject.ListOfFileNames(getServletContext().getRealPath("/copy"),".txt");
            for (int fileIndex = 0; fileIndex < finalListNames.size(); fileIndex++) {
                //Copy and pasting file from SourcePath to destination Path
                FunctionNamesObject.FileMoving(
                                            finalListNames.get(fileIndex),
                                            getServletContext().getRealPath("/Rod"),
                                            "TMapInput.txt"
                                            );
                //.exe file process will be start from here.
                FunctionNamesObject.ExeternalFileProcessing(getServletContext().getRealPath("/Rod"),"ThMapInfratab1-2.exe","TMapInput.txt");
                //Later Rods output files moving from one directory to another function will be added here
                request.setAttribute("Name","ABD");
                request.setAttribute("Password", "DEF");
                RequestDispatcher rd=request.getRequestDispatcher("/JSPFiles/JspTesting.jsp");
                rd.forward(request, response ) ;
            }
        }
        catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Jsp代码是:

<%
   out.print(request.getAttribute("Name"));
   out.print(request.getAttribute("Password"));
 %>

如果你观察我的代码,RequestDispatcher代码在for循环中,意味着对于每次迭代,我都希望使用参数转发到Jsp文件。

在第一次迭代中,我收到来自Jsp文件的响应,但它在第二次迭代时没有工作。我收到以下错误。

  HTTP Status 500 - Cannot forward after response has been committed
  java.lang.IllegalStateException: Cannot forward after response has been committed
PackageName.ClassName.service(ClassName.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

我该如何解决这个问题。

由于

0 个答案:

没有答案