如何在单个Servlet中进行双向前进?

时间:2016-05-24 16:30:46

标签: jsp redirect servlets illegalstateexception forward

我使用Servlet和JSP创建了一个Web应用程序。我的首页看起来像这样:http://i.stack.imgur.com/UCF8t.png

我有两个JSP文件,我想根据点击重定向我的网络应用程序。如果用户单击“删除”,则我的Web应用程序会将其重定向到FrontPage.jsp,当用户单击“创建”时,Web应用程序应将其重定向到index.jsp。我已经引用了这个链接Double forward in one servlet并试图使用条件语句,它将转发到适当的资源,具体取决于正确的请求。但是当用户输入集合名称并单击“创建”时,我收到一条错误消息:

java.lang.IllegalStateException: Cannot forward after response has been committed

这是我的servlet:

     @WebServlet(description = "My first collection servlet", urlPatterns = {
    "/CollectionPath" })
public class Collection extends HttpServlet {
  private static final long serialVersionUID = 1L;

  protected void doPost(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("text/html");

    String deleteFileName = request.getParameter("filename");
    System.out.println("you clicked on " + deleteFileName);
    File f = new File(
        "C:/Apps/eclipse-jee-mars-2-win32-x86_64/eclipse/" + deleteFileName);
    if (f.delete()) {
      System.out.println(f + "is deleted");

    } else {
      System.out.println(f + "is not deleteds");
    }


    String CollectionName = request.getParameter("myCollectionName");
    request.setAttribute("collectionName", CollectionName);

    if (CollectionName == null) {
      request.getRequestDispatcher("FrontPage.jsp").forward(request, response);
    }

    else if (CollectionName != null) {
      request.getRequestDispatcher("index.jsp").forward(request, response);
    }

    String Pattern = request.getParameter("Pattern");
    String NotPattern = request.getParameter("NotPattern");
    String CollectionNameValue = request.getParameter("CollectionNameValue");
    File file = new File(CollectionNameValue + ".xml");
    file.createNewFile();
    FileWriter writer = new FileWriter(file);
    System.out.println(file.getAbsolutePath());
    writer.write("<?xml version=\"1.0\"?><collection><includePatterns>"
        + Pattern + "</includePatterns><doNotIncludePatterns>" + NotPattern
        + "</doNotIncludePatterns></collection>");
    writer.flush();
    writer.close();

  }

}

如果用户点击“删除”,我就可以重定向到下一页,但是当用户点击“创建”时无法重定向。 这是我的FrontPage.jsp

<%@page import="java.io.File"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="ISO-8859-1">

</head>

<body>

    A collection is a subset of the complete index. For example, create a
    marketing collection or an engineering collection to support searches
    only in the marketing or engineering pages in your index. You specify
    the contents of the collection using URL patterns. Create as many
    collections as you need.
    <br>
    <br>
    <b>Current Collections</b>

    <table width="100%" border="1">

        <%
          File folder = new File("C:/Apps/eclipse-jee-mars-2-win32-x86_64/eclipse");
          File[] listOfFiles = folder.listFiles();
          for (int i = 0; i < listOfFiles.length; i++) {
        %>
        <tr>
            <%
              if (listOfFiles[i].isFile()) {
            %>
            <td><%=listOfFiles[i].getName()%></td>
            <td>
                <form method="post" action='CollectionPath'>
                    <input type="submit" value="Delete"
                        onclick="return confirm('Are you sure you want to proceed?')">
                    <input type="hidden" value="<%=listOfFiles[i].getName()%>"
                        name="filename" />
                </form>
            </td>
            <%
              }
            %>
        </tr>
        <%
          }
        %>
    </table>
    <br>

    <title>Create New Collection</title>
    <h> <b>Create New Collection</b></h>
    <br> Collection Name:
    <textarea name="myCollectionName" cols="10" rows="1"></textarea>
    <br>
    <br>
    <form method="post" action='CollectionPath'>
        <input type="submit" value="Create"
            style="color: white; background: blue" />
    </form>
</body>
</html>

这是我的index.jsp

<%@page import="java.io.File"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="ISO-8859-1">

</head>

<body>

    A collection is a subset of the complete index. For example, create a
    marketing collection or an engineering collection to support searches
    only in the marketing or engineering pages in your index. You specify
    the contents of the collection using URL patterns. Create as many
    collections as you need.
    <br>
    <br>
    <b>Current Collections</b>

    <table width="100%" border="1">

        <%
          File folder = new File("C:/Apps/eclipse-jee-mars-2-win32-x86_64/eclipse");
          File[] listOfFiles = folder.listFiles();
          for (int i = 0; i < listOfFiles.length; i++) {
        %>
        <tr>
            <%
              if (listOfFiles[i].isFile()) {
            %>
            <td><%=listOfFiles[i].getName()%></td>
            <td>
                <form method="post" action='CollectionPath'>
                    <input type="submit" value="Delete"
                        onclick="return confirm('Are you sure you want to proceed?')">
                    <input type="hidden" value="<%=listOfFiles[i].getName()%>"
                        name="filename" />
                </form>
            </td>
            <%
              }
            %>
        </tr>
        <%
          }
        %>
    </table>
    <br>

    <title>Create New Collection</title>
    <h> <b>Create New Collection</b></h>
    <br> Collection Name:
    <textarea name="myCollectionName" cols="10" rows="1"></textarea>
    <br>
    <br>
    <form method="post" action='CollectionPath'>
        <input type="submit" value="Create"
            style="color: white; background: blue" />
    </form>
</body>
</html>

我是Servlet和JSP的新手。我可能会遗漏一些明显的东西。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

如果您查看代码,那么您在转发后就会在回复中写一些内容。

停止这样做。或者只是你可以尝试在前进后添加一个回报。

首先处理结果,然后最后处理。