Hello All
i'm new to jsp developing and i'm working on a site contain news page and allows user comments . i already do it but having 2 problems
first that i have to refresh after submit comment to show comment
second that every time i refresh the comment is inserted again in DB
so please help

<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@page contentType="text/html"  import="java.util.*, java.lang.*, java.io.*, java.sql.*, oscai.conn.OscaiConnection"%> <!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <Title> News Page</Title> </head> <% System.out.print(request.getParameter("doSubmit")); %> <body class="home" id="home"> <div id="container"> <div id="wrapper"> <jsp:include page="header.jsp" /> <table border="0"> <tr> <td> <div id="content-article"> <%

                                try {
                                    String createString = "select distinct News.NewsID as 'newsid' ,News.Title as 'title',  News.NewsDate as 'newsdate', News.NewsBody as 'newsbody', NewsImages.ImagePath as 'Image' "
                                            + "from News left join NewsImages on (NewsImages.NewsID = News.NewsID ) where News.NewsID =" + request.getParameter("newsid") + " and NewsImages.IsMain ";

                                    OscaiConnection O = new OscaiConnection();
                                    Connection con = O.createConnection();
                                    ResultSet results = O.QueryType(con, createString, 0);

                                    results.beforeFirst();
                                    while (results.next()) {
                            %> <div class="article"> <img  src="<%=results.getString("Image")%>"> <h1><a style="color:#507F19;text-decoration:none;background-color:transparent"> <%=results.getString("title")%> </a></h1> <p class="timestamp"><%=results.getString("newsdate")%></p> <p><%=results.getString("newsbody")%><br/> </p> </div> <%
                                }/*end of while loop*/

                                O.closeConnection(con);
                            } catch (Exception e) {
                                //System.err.print("ClassNotFoundException: "); 
                                //System.err.println(e.getMessage());
%><div class="erralert">Error1: <%=e%></div> <%  }%> </div> <!-- end of content-article div --> <div id="readerComments"> <form  id="addComment" action="news.jsp?newsid=<%=request.getParameter("newsid")%>" method="POST"> <table width="100%"   align="right"> <%
                                    try {
                                        String createString = "select News.NewsID as 'newsid' ,NewsReaderComments.Comments as 'comments',  NewsReaderComments.CommentDate as 'commentdate' "
                                                + "from News left join NewsReaderComments on (NewsReaderComments.NewsID = News.NewsID ) where News.NewsID =" + request.getParameter("newsid");

                                        OscaiConnection O = new OscaiConnection();
                                        Connection con = O.createConnection();
                                        ResultSet results = O.QueryType(con, createString, 0);

                                        while (results.next()) {
                                %> <tr> <td> <h4> <b><%=(results.getString("commentdate") == null ? "" : results.getString("commentdate"))%> </b> </h4> <br/> <h3><%=(results.getString("comments") == null) ? "" : results.getString("comments")%></h3> </td> </tr> <%
                                    }/*end of while loop*/

                                    O.closeConnection(con);
                                } catch (Exception e) {
                                    //System.err.print("ClassNotFoundException: "); 
                                    //System.err.println(e.getMessage());
%><div class="erralert">Error2: <%=e%></div> <%  }%> <tr> <input type="text"  aligh="right" name="comments" width="600px"> <input type="hidden" name="doSubmit" value="" > <input type="button" value="submit"  > </tr> </table></form> </div> <% 
                                try {
                                    String vComment = null;
                                    vComment = request.getParameter("comments");
                                    String createString = "insert into NewsReaderComments(NewsID, UserID, Comments) values ('" + request.getParameter("newsid") + "','" + 1 + "','" + vComment + "')";
                                    OscaiConnection O = new OscaiConnection();
                                    Connection con = O.createConnection();
                                    if (vComment != null) {
                                        ResultSet results = O.QueryType(con, createString, 1);
                                    }

                                    O.closeConnection(con);
                                } catch (Exception e) {
                                    //System.err.print("ClassNotFoundException: "); 
                                    //System.err.println(e.getMessage());
%><div class="erralert">Error3: <%=e%></div> <%  }
                            %> <div> </div> </td> </tr> <tr><td colspan="2"><jsp:include page="footer.jsp"  /></td></tr> </table> </div> <!-- end of div wrapper--> </div> <!-- end of div container--> </body> 

code like this is one of the main reasons I usually recommend to start moving every single line of your business logic out of your jsp files (which are really only supposed to implement the ui of the application) and into servlets.

This code is an ureadable mess, so I can imagine you are having quite some trouble debugging it.

first that i have to refresh after submit comment to show comment
second that every time i refresh the comment is inserted again in DB

So, either you have an endless loop, or this isn't doing what you need it to do.
Anyway, use servlets. That way, you can "forward" to a page instead of refreshing it. Should be able to solve your problems.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.