单击“刷新Servlet页面”可显示“重复内容”

时间:2015-06-16 05:54:54

标签: java mysql servlets

我正在使用我的第一个servlet程序,并且我创建了一个页面来列出数据库中表的行。

每当我点击刷新按钮时,页面都会显示重复的结果。

如何克服它,有什么建议吗?

@WebServlet("/BookList")
public class BookList extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public BookList() {
        super();
        // TODO Auto-generated constructor stub
    }
    StringBuffer string = new StringBuffer();
    List<DemoBook>list=new ArrayList<DemoBook>();
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        HttpSession session=request.getSession();

        if(session.getAttribute("Admin")==null)
        {
            response.sendRedirect("AdminLoginServlet");
        }




        try {
            createTable(request, response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }}
    private void createTable(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
//      StringBuffer strr=new StringBuffer();
        String command = null;

         PrintWriter out=response.getWriter();
         System.out.println("hellowww");
         response.setContentType("text/html");
            System.out.println("begins");
            string.append("<html>");
            System.out.println("2nd html");
            string.append("<head>" );
            string.append("<Title>Book Collections</title><Script>");
            string.append("function editBook(obj) {"
                    + "document.listBook.command.value=\"editBook\";"
                    + "document.listBook.BookId.value=obj.name;"
                    + "document.listBook.submit();" + "}");


            string.append("function deleteBook(obj) {"
                    + "document.listBook.command.value=\"deleteBook\";"
                    + "document.listBook.BookId.value=obj.name;"
                    + "document.listBook.submit();" + "}");



            string.append("</Script>");
            string.append(" <style>");
            string.append("#one{" );
            string.append("background-image:url(\"./images/admini.jpg\");");
            string.append(" width: 99.6%;");
            string.append(" text-align:center;");
            string.append("color:white;" );
            string.append(" padding:50px;" ); 
            string.append("}");
            string.append("#two{" );
            string.append("background-color:#6495ED;" );
            string.append("width:100%;" );
            string.append(" height:23px;");
            string.append(" color:white;");
            string.append("padding:3px;");

            string.append("}" );
            string.append(" a:link {");
            string.append("color:blue; "  );
            string.append(" background-color:transparent;"   );
            string.append(" text-decoration:none;" ); 

            string.append("}");
            string.append("#four{" );
            string.append(" background-color:#A0522D;");
            string.append(" color:white;");
            string.append("width: 100%;");
            string.append(" padding:3px;");
            string.append("height:30px;");

            string.append(" text-align:center;");
            string.append("  font-size: 1.5em;");
            string.append("}");
            string.append("{ table, th, td {");
            string.append("border: 5px solid white;");
            string.append("border-collapse: collapse; border-width: 5px;");
            string.append("padding: 10px;");
            string.append("}" );
            string.append("h3");
            string.append("{" );
            string.append("color:white;");
            string.append("}" );

            string.append( "</style>");
            string.append( "<head>");
            string.append("<body>");

            string.append("<div id=\"one\" >");
            string.append( "<h2>Administration</h2>");
            string.append("</div>" );
            string.append("<div id=\"two\">");
            string.append("&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;");

            string.append("<a href=\"Home\"><b> Home</b></a>&nbsp; &nbsp;");
            string.append("<a href=\"AdminHome\"> <b>Add Book</b></a>&nbsp; &nbsp;");
            string.append("<a href=\"\"> <b>Order List</b></a>&nbsp; &nbsp;");
            string.append("<a href\"BookList\"> <b>Book List</b></a>&nbsp; &nbsp;");
            string.append("<a href=''> <b>Change Password</b></a>&nbsp; &nbsp;");
            string.append("<a href='demo.html'> <b>Logout</b></a>&nbsp; &nbsp;");
            string.append("</div>");

            string.append("<div id=\"four\"><b>List Books</b>");
            string.append("</div>");
command=request.getParameter("command");

            int bookId = 0;
            InBookData in1=new InBookData();

            List<DemoBook> bookList = in1.findAll();
            if (request.getParameter("BookId") != null
                    && request.getParameter("BookId") != "") {
                bookId = Integer.parseInt(request.getParameter("BookId"));

            }
            //System.out.println(request.getParameter("BookId"));

            if ("deleteBook".equals(command)) {

                System.out.println("clicked:::");
                InBookData in=new InBookData();
                in.delete(bookId);
                System.out.println(bookId);
                string.append(displayBookList(bookList));


            } 
//              else if ("editBook".equals(command)) {
//
//              DemoBook book = ServiceFactory.getInBookService().findBook(bookId);
////                book.setId(bookId);
////                string.append(createUpdateForm(book));
//          }
            else{

                string.append(displayBookList(bookList));
                System.out.println("first");

            }
            string.append("<input type=\"hidden\" name=\"command\"/>")
                    .append("<input type=\"hidden\" name=\"BookId\"/>")
                    .append("</form></body></html>");

            out.print(string);
            System.out.println("--------------------helloooooooooooooo");
            System.out.println(string.toString());
    }
    private String displayBookList(List<DemoBook> bookList) {
    System.out.println("displaybook");
    StringBuffer str=new StringBuffer();
    str.append("<form name=\"listBook\" action=\"\" method=\"post\" >");
            str.append( "<table align=\"center\">");
            str.append("<tr bgcolor=\"#6495ED\"><th > Book Name</th>");
            str.append("<th>ISBN</th>");
            str.append("<th>Publisher</th>");
            str.append("<th> Author</th>");
//           +"<th> Image</th>"
            str.append("<th>Edit</th>");
            str.append("<th>");
            str.append(" Delete");
            str.append("</th>" );
            str.append(" </tr>");

            for(DemoBook d:bookList)
            {
                System.out.println(d.getB_author()+"   "+d.getId());

            str.append("<tr>" );
            str.append( "<td>"+d.getB_name());
            str.append( "</td>");
            str.append( "<td>"+d.getIsbn());string.append("</td>");
            str.append(" <td>" +d.getB_publisher()+"</td>");
            str.append(" <td>"+d.getB_author()+"</td>");

                    str.append( "<td><input type=\"button\" name=" + d.getId()+ " onclick=\"editBook(this)\" value=Edit> </td>");
                    str.append( "<td><input type=\"button\" name=" + d.getId()+ " onclick=\"deleteBook(this)\" value=Delete></td>");

                    str.append("</tr>");
//                  string.append("<tr><td><input type=\"hidden\" name=\"BookId\" value="+d.getId()+"</td> </tr>");


            }
            return str.toString();
    }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
//      System.out.println("post");
    }

}

1 个答案:

答案 0 :(得分:0)

打印后删除字符串

string.delete(0,string.length());