How to Redirect to a unique jsp (using jsp id) file by using servlet



  • I am going to crate a forum by using jsp and servlets. first of all i created a forum layout and i passed relevant question data to that layout when someone click a question. when you go to the ComapnyForum.jsp page you are able to post your answer. after you click the post button, form action goes to the PostAnswer servlet and it will send data to the database. what i want is, after you click the post button, reload the page ans show the answer that enterd before .i use

    <a href="CompanyForum.jsp?id=<%=rs.getString("id")%>" >
    

    in my jsp page and it worked very well. but when i used

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

    in my PostAnswer servlet, it is not working. please show me how to do this

    here is my 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>
    

    this is my 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);
        }
    }
    


  • @akashmanujaya founded!!!

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

  • Linux Help

    @akashmanujaya fatta bro