servlet在每次重新加载时都会被调用,并用相同的数据重新填充页面

1zmg4dgp  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(282)

我正在做一个jsp+servlet书店项目,遇到了一个问题。我有一个displayItemservlet,其代码如下。

package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import connection.ConnectDB;
import model.Book;

/**
 * Servlet implementation class DisplayItems
 */
@WebServlet("/DisplayItems")
public class DisplayItems extends HttpServlet {
    private static final long serialVersionUID = 1L;
    ArrayList<Book> bookstore = new ArrayList<>();

    public DisplayItems() {
        super();
    }

    Cookie c = null;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();

        try {
            Connection con = ConnectDB.connect();

            String q = "select ISBN, title, year, price, first_name, last_name from books, authors\r\n"
                    + "where books.author_id = authors.author_id\r\n";
            PreparedStatement pst = con.prepareStatement(q);
            ResultSet rs = pst.executeQuery();

            try {
                c = new Cookie(request.getParameter("ItemId"), "0000000000001");
                response.addCookie(c);
                System.out.println("Product Added to Cart successfully!");  
            }
            catch(Exception e) {}

            while (rs.next() ) 
            {

                bookstore.add(new Book(rs.getString(1), rs.getString(2), rs.getInt(3), rs.getDouble(4), rs.getString(5), rs.getString(6)));  
            }
        }
        catch(Exception e)
        {   
            pw.print(e);
        }
        HttpSession session=request.getSession();  

            session.setAttribute("data", bookstore); 
            request.getRequestDispatcher("/bookstorePage/bookstore.jsp").forward(request, response);     
    }
}

我有一个bookstore.jsp文件,其中包含以下代码

<%@ page import="java.sql.*"%>
<%@page import="java.util.ArrayList"%> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@page import="model.Book"%> 
<%@page import="connection.ConnectDB"%> 

<html>
    <body>
          <table cellpadding = "20">
            <tr>
                    <td class = "header">ISBN</td>
                    <td class = "header">Title</td>
                    <td class = "header">Year</td>
                    <td class = "header">Price</td>
                    <td class = "header">Author</td>
            </tr>
            <%
        ArrayList<Book> bookstore = (ArrayList<Book>)session.getAttribute("data"); 

        if(bookstore != null){
        for(Book book: bookstore){%> 
            <tr> 
                <td class = "bookInfo"><%=book.getISBN()%></td> 
                <td class = "bookInfo"><%=book.getTitle()%></td> 
                <td class = "bookInfo" style = "text-align:center"><%=book.getYear()%></td> 
                <td class = "bookInfo" style = "text-align:center">$<%=book.getPrice()%></td> 
                <td class = "bookInfo"><%=book.getFirstName() + " "+ book.getLastName()%></td> 
                <td class = "bookInfo"><a href = 'DisplayItems?ItemId=<%=book.getISBN()%>'><strong>Add to Cart </strong></a></td>
            </tr> 
            <%} %>
        <% }%>  
   </table>
  </body>
</html>

我目前的困惑是,当页面加载时,它有一个带有servlet名称的url。因此,每当我刷新页面时,它都会重新运行servlet脚本并执行查询,然后将数据重新发送到bookstore.jsp。这样做的结果是,每次重新加载页面时,都会有相同的数据传入并添加到以前的数据中(在本例中,它会添加到图书列表中)。我需要你的帮助来阻止这一切的发生。
如果我的要求不清楚,请告诉我。如果我能得到一些指导就太好了。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题