我在JSP页面上有一个表。点击按钮,我想对它进行排序。我应该怎么做?结果表会在其他jsp页面上还是我可以更改当前表? 那是我的servlet,但我不明白,排序后如何返回结果。它 谢谢!
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/home")
public class HomeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
String[] name = {"x1","x2","x3","x4","x5","x6","x7","x8","x9","x10"};
int[] price = {7600,8500,11700,15000,6000,10200,6400,5800,8700,10400};
int[] memory = {4,4,12,16,2,10,3,1,6,8};
int[] camera = {10,8,10,15,6,9,5,4,8,12};
public HomeServlet() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int temp = 0;
List<Product> products = new ArrayList<Product>();
while(temp!=price.length-1){
Product pr = new Product(name[temp], camera[temp], memory[temp], price[temp]);
temp++;
products.add(pr);
}
request.setAttribute("productList", products);
RequestDispatcher rd = request.getServletContext().getRequestDispatcher("/WEB-INF/views/home.jsp");
rd.forward(request, response);
if (request.getParameter("button1") != null) {
Sorts.function1(price);// in this function I sort table by a certain field
} else if (request.getParameter("button2") != null) {
Sorts.function1(memory);
}
}
//???? WHAT I SHOULD DO HERE?
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}