Java 单击刷新Servlet页面显示重复内容
我使用的是我的第一个servlet程序,我创建了一个页面来列出数据库中表的行 每当我单击“刷新”按钮时,页面都会显示重复的结果 我如何克服它,有什么建议吗Java 单击刷新Servlet页面显示重复内容,java,mysql,servlets,Java,Mysql,Servlets,我使用的是我的第一个servlet程序,我创建了一个页面来列出数据库中表的行 每当我单击“刷新”按钮时,页面都会显示重复的结果 我如何克服它,有什么建议吗 @WebServlet("/BookList") public class BookList extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet()
@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(" ");
string.append("<a href=\"Home\"><b> Home</b></a> ");
string.append("<a href=\"AdminHome\"> <b>Add Book</b></a> ");
string.append("<a href=\"\"> <b>Order List</b></a> ");
string.append("<a href\"BookList\"> <b>Book List</b></a> ");
string.append("<a href=''> <b>Change Password</b></a> ");
string.append("<a href='demo.html'> <b>Logout</b></a> ");
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");
}
}
@WebServlet(“/BookList”)
公共类BookList扩展了HttpServlet{
私有静态最终长serialVersionUID=1L;
/**
*@参见HttpServlet#HttpServlet()
*/
公共书目(){
超级();
//TODO自动生成的构造函数存根
}
StringBuffer字符串=新的StringBuffer();
Listlist=新的ArrayList();
/**
*@参见HttpServlet#doGet(HttpServletRequest请求,HttpServletResponse响应)
*/
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
//TODO自动生成的方法存根
HttpSession session=request.getSession();
if(session.getAttribute(“Admin”)==null)
{
sendRedirect(“AdminLoginServlet”);
}
试一试{
createTable(请求、响应);
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}}
私有void createTable(HttpServletRequest请求,
HttpServletResponse)引发异常{
//StringBuffer strr=新的StringBuffer();
字符串命令=null;
PrintWriter out=response.getWriter();
System.out.println(“hellowww”);
response.setContentType(“text/html”);
System.out.println(“开始”);
字符串。追加(“”);
System.out.println(“第二个html”);
字符串。追加(“”);
string.append(“藏书”);
append(“函数editBook(obj){”
+“document.listBook.command.value=\“editBook\”
+“document.listBook.BookId.value=obj.name;”
+“document.listBook.submit();”+“}”);
append(“函数deleteBook(obj){”
+“document.listBook.command.value=\”deleteBook\“;”
+“document.listBook.BookId.value=obj.name;”
+“document.listBook.submit();”+“}”);
字符串。追加(“”);
字符串。追加(“”);
追加(“#one{”);
append(“背景图像:url(\”/images/admini.jpg\”);”;
追加(“宽度:99.6%;”);
追加(“文本对齐:居中;”);
追加(“颜色:白色;”);
append(“padding:50px;”);
追加(“}”);
追加(“#two{”);
追加(“背景色:#6495ED;”);
追加(“宽度:100%;”);
追加(“高度:23px;”);
追加(“颜色:白色;”);
append(“padding:3px;”);
追加(“}”);
append(“a:link{”);
追加(“颜色:蓝色;”);
追加(“背景色:透明;”);
追加(“文本装饰:无;”;
追加(“}”);
追加(“#四{”);
追加(“背景色:#A0522D;”);
追加(“颜色:白色;”);
追加(“宽度:100%;”);
append(“padding:3px;”);
追加(“高度:30px;”);
追加(“文本对齐:居中;”);
字符串。追加(“字体大小:1.5em;”);
追加(“}”);
append(“{table,th,td{”);
追加(“边框:5px纯白;”);
append(“边框折叠:折叠;边框宽度:5px;”);
追加(“padding:10px;”);
追加(“}”);
字符串。追加(“h3”);
追加(“{”);
追加(“颜色:白色;”);
追加(“}”);
字符串。追加(“”);
字符串。追加(“”);
字符串。追加(“”);
字符串。追加(“”);
字符串。追加(“管理”);
字符串。追加(“”);
字符串。追加(“”);
字符串。追加(“”);
字符串。追加(“”);
字符串。追加(“”);
字符串。追加(“”);
字符串。追加(“”);
字符串。追加(“”);
字符串。追加(“”);
字符串。追加(“”);
string.append(“列表书”);
字符串。追加(“”);
command=request.getParameter(“command”);
int bookId=0;
InBookData in1=新的InBookData();
List 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(命令)){
System.out.println(“单击:”;
InBookData in=新的InBookData();
in.删除(bookId);
System.out.println(bookId);
追加(displayBookList(bookList));
}
//else if(“editBook”.equals(命令)){
//
//DemoBook book=ServiceFactory.getInBookService().findBook(bookId);
////book.setId(bookId);
////append(createUpdateForm(book));
// }
否则{
追加(displayBookList(bookList));
System.out.println(“第一”);
}
string.append(“”)
.附加(“”)
.附加(“”);
输出。打印(字符串);
System.out.println(“--------------hellooooooo”);
System.out.println(字符串
string.delete(0,string.length());