requestDispatcher.forward forward不起作用,但response.sendRedirect确实有效。有谁知道为什么?

时间:2015-08-12 20:18:55

标签: jsp servlets redirect forward

我已经构建了一个jsp页面来动态显示内容以允许评论。 jsp页面可以激活PostComment servlet以在数据库中插入数据。 然后,servlet重定向回找到的jsp页面,并显示内容和表单,但不显示先前的注释。 我可以通过使用response.sendRedirect获得正确的功能,但我很困惑 为什么请求调度不起作用。我把相关的servlet和jsp放在下面。 我已经删除了我认为不相关的代码,但如果需要,可以乐意添加更多代码。这三个组成部分如下。

sumarize。如果我打电话给A,一切正常。抓取内容,提出表单,并显示先前的注释(servlet B)。 如果用户输入注释,A将调用servlet C,它将重定向回A. 如果C使用Response.sendRedirect返回A,它可以工作,如果C使用requestDispatcher.forward返回A,A函数但不包括C的结果

一个。 MAIN viewarticle.jsp(显示一些内容(5-7)提供添加注释的表单(9-16)显示之前的注释(18-20)如果直接调用则工作正常

<html><head><TITLE>Article</TITLE><link rel="stylesheet" href="../default.css"></head>
    <body class="main">
        <h1>Article:  <%= request.getParameter("article") %></h1>
        <% String fname = request.getParameter("article"); %><% String fullname = "/dswva/articles/"+fname+".jsp"; %><% String relname = "../articles/"+fname+".jsp"; %>

        <jsp:include page = '<%= relname %>' flush="true">
        <jsp:param name="bull" value="shit"/>
        </jsp:include>

        <h3>To make a comment, enter it below?</h3>
        <form name="PostComm" method="post" action="/dswva/servlet/PostComm">
        <input type="hidden" name='article' value='<%= fname %>'>
        <input type="hidden" name='fullname' value='<%= fullname %>'>
        <input type="hidden" name="user" value= <%= request.getRemoteUser() %>>
        <p><textarea name="comment" rows="9" cols="70"></textarea></p>
        <p>    <input type="submit" value="Submit My Comment" name="submit">
        <input type="reset" value="Clear Form" name="reset"> </p>
        </form>
        <h2>Comments made previously:</h2>
        <jsp:include page="/servlet/ViewComments" flush="true" >
        <jsp:param name="fullname" value='<%= fullname %>'/>
        </jsp:include>
    </body>
</html>

B中。用于显示内容评论的Servlet - 按宣传方式工作。

public void doGet(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {
    DatabaseConnector con = new DatabaseConnector(DatabaseConnector.getDatabaseType());
    ResultSet rs = null;
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    try {
        StringBuffer SQL = new StringBuffer("SELECT ID,User,Date_Posted,Comment FROM ArtComment ");
        SQL.append("WHERE Article = '" + request.getParameter("fullname") + "' ORDER BY Date_Posted DESC");
        rs = con.selectSQL_RS(SQL.toString());
        if (rs != null) {
            while (rs.next()) {
                String userString = rs.getString("User");
                String dateString = new SimpleDateFormat("MM/dd/yyyy").format(rs.getObject("Date_Posted"));
                String CommentString = rs.getString("comment");
                out.println("<p>On " + dateString + ", " + userString + " said:</p>");
                out.println("<blockquote>" + CommentString + "</blockquote>");
            }
        } else {
            out.println("<h4>No Comments have been made to date on this article.</h4>");
        }
    } catch (SQLException e) {
        out.println("SQLException caught in viewcomments: " + e.getMessage());
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
        }
        // ...
    }
}

℃。 Servlet发表评论并回顾viewarticle.jsp - 在发布新评论后,servlet被重定向(第17-18行)到正确运行的JSP页面,除了它不包含先前的评论。如果我使用Response.sendRedirect(第16行),它按预期工作

public void doGet(HttpServletRequest request, HttpServletResponse response)        throws ServletException, IOException {
    DatabaseConnector con1 = new DatabaseConnector(DatabaseConnector.getDatabaseType());
    try {
        con1 = new DatabaseConnector(DatabaseConnector.getDatabaseType());
        String madeBy = request.getRemoteUser();
        if (madeBy == null) {
            madeBy = request.getParameter("user")+"?";
        }
        String lComment = request.getParameter("comment");
        String lFullname = request.getParameter("fullname");
        String lArticle = request.getParameter("article");
        int display = 0;

        String SQL = "INSERT INTO artcomment (user,Comment, Article) VALUES (?,?,?)";
        con1.freeformSQL(SQL, new Object[]{madeBy, lComment, lFullname});
        con1.CleanUp();
        response.sendRedirect("/dswva/articles/viewArticle.jsp?article="+lArticle);
        // RequestDispatcher rd = getServletContext().getRequestDispatcher("/articles/viewArticle.jsp?article=" + lArticle);
        //rd.forward(request, response);// Create the header for the resource info

    } finally {
    con1.CleanUp();
    }
}

0 个答案:

没有答案