如何通过使用servlet重定向到唯一的jsp(使用jsp id)文件

时间:2019-03-20 09:19:57

标签: java mysql jsp servlets

我将使用jsp和servlet创建一个论坛。首先,我创建了一个论坛布局,当有人单击问题时,我将相关的问题数据传递给该布局。当您转到ComapnyForum.jsp页时,便可以发布答案。单击post按钮后,表单操作将转到PostAnswer Servlet,它将把数据发送到数据库。我想要的是,单击发布按钮后,重新加载页面并显示之前输入的答案

我的jsp页面中的

我的用户<a href="CompanyForum.jsp?id=<%=rs.getString("id")%>" >运作良好。但是当我使用

RequestDispatcher requestDispatcher = request.getRequestDispatcher("CompanyDashboard.jsp?id=<%=rs.getString("id")%>");
requestDispatcher.forward(request, response);

在我的PostAnswer servlet中,它不起作用。请告诉我该怎么做

这是我的 CompanyForum.jsp

 <%
               java.sql.Connection connection = null;


                try
                {
                    int id = Integer.valueOf(request.getParameter("id"));
                    connection = Connector.ConnectDb();
                    PreparedStatement pst = connection.prepareStatement(" SELECT * FROM question where id = '"+id+"' ");
                    ResultSet rs = pst.executeQuery();
                    while(rs.next())
                    {
                        String title=rs.getString("title");
                        String tags = rs.getString("tags");
                        String question = rs.getString("question");
                        int qid = rs.getInt("id");
                        request.setAttribute("tags", tags);
                        request.setAttribute("title", title);
                        request.setAttribute("question", question);
                        session.setAttribute("qid", qid);
                    }
                } 

                catch (SQLException ex) {

                }
            %>
            <form class="margin-top-40" action="PostAnswer" method="post">
            <div class="form-group">
                <input type="text" placeholder="Place your title here" disabled style="color: rgba(1, 203, 225, 0.8); font-size: 20px; font-weight: bold" class="form-control" style="font-weight: bold;font-size: 20px;"  name="title" value='${title}'>
            </div>
              <div class="form-group">
                  <label style="color: #ffffff">Tags</label>
                  <input type="text" placeholder="Place your tags here" disabled class="form-control"  name="keywords" value='${tags}' style="color: rgba(1, 203, 225, 0.8);">
                  <input type="hidden" display="none" name="qid" value='${qid}' style="color: rgba(1, 203, 225, 0.8);">

            </div>
            <div class="form-group">
              <label style="color: #ffffff;">Question Detials</label>
              <textarea cols="12" rows="12" placeholder="Post Your Question Details Here....." name="message" class="form-control" style="color: #ffffff;" disabled=""> ${question} </textarea>
            </div>
             <div class="form-group">
              <label style="color: #ffffff">Image</label>
              <input class="input--style-4" type="file" name="image">
            </div> 


              <div class="form-group">
              <label style="color: #ffffff">Answer</label>
              <textarea cols="12" rows="12" placeholder="Post Your Answer Here....."  name="comment" class="form-control"></textarea>
            </div>
            <button class="btn btn-primary pull-right" value="submit" type="submit"> Post </button> 
           <!-- <button class="btn btn-primary pull-right" value="reset">Reset</button>-->
           <br>
            <br>
            <br>
            <br>
            <hr style="border: 2px solid rgba(1, 203, 225, 0.8);">

           <h2>Answers</h2>

           <table align="left" cellpadding="10" cellspacing="10" border="0">

          <%                  
                try
                {
                    int id = Integer.valueOf(request.getParameter("id"));
                    connection = Connector.ConnectDb();
                    PreparedStatement pst = connection.prepareStatement(" SELECT * FROM comment where q_id = '"+id+"' order by id ");
                    ResultSet rs = pst.executeQuery();
                    while(rs.next())
                    {
            %>

            <tr>

                <td><ul><li><%= rs.getString("comment") %></li></ul></td>
            </tr>
            <% 
            }

            } 
            catch (Exception e) {
            e.printStackTrace();
            }
            %>
            </table>



          </form>

这是我的 PostAnswer.java

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

    String comment = request.getParameter("comment");
    PrintWriter out = response.getWriter();
    int id = Integer.parseInt(request.getParameter("qid"));

    try
    {
        connection = Connector.ConnectDb();
        PreparedStatement pst = connection.prepareStatement("INSERT INTO comment (comment,q_id) values (?,?)");
        pst.setString(1, comment);
        pst.setInt(2, id);

        int rs = pst.executeUpdate();

        if(rs>0)
        {
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("CompanyDashboard.jsp?id=<%=rs.getString("id")%>");
            requestDispatcher.forward(request, response);

        }
    } 

    catch (SQLException ex) {
        Logger.getLogger(PostAnswer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

更新 我用

RequestDispatcher requestDispatcher = request.getRequestDispatcher("CompanyForum.jsp?id=" + id);
requestDispatcher.forward(request, response);

它醒了!!

如果还有其他方法,请告诉我

0 个答案:

没有答案