📄 vendorlistservlet.java
字号:
package com.yiboit.cfss.vendor;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class VendorListServlet extends HttpServlet {
public static final int PAGE_LINES = 5;
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int curpage;
int maxpage = 0;
String strCurPage = request.getParameter("curpage");
String strMaxPage = request.getParameter("maxpage");
String cmd = request.getParameter("cmd");
if (strCurPage == null) {
curpage = 1;
} else {
curpage = Integer.parseInt(strCurPage);
}
if (strMaxPage != null) {
maxpage = Integer.parseInt(strMaxPage);
}
HttpSession session = request.getSession();
try {
if (cmd == null) {
VendorDAO dao = new VendorDAO();
List<VendorBean> vendorList = dao.getVendorList();
session.setAttribute("vendorList", vendorList);
maxpage = vendorList.size() / PAGE_LINES;
if (vendorList.size() % PAGE_LINES != 0) {
maxpage ++;
}
} else if (" next ".equals(cmd)) {
if (curpage < maxpage) {
curpage++;
}
} else if (" previous ".equals(cmd)) {
if (curpage > 1) {
curpage--;
}
}
int start = PAGE_LINES * (curpage - 1);
int end = start + PAGE_LINES - 1;
request.setAttribute("start", start);
request.setAttribute("end", end);
request.setAttribute("curpage", curpage);
request.setAttribute("maxpage", maxpage);
request.getRequestDispatcher("vendorList.jsp").forward(request,
response);
} catch (SQLException e) {
e.printStackTrace();
response.sendRedirect(request.getContextPath() + "/error.jsp");
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -