jsp页面跳转链接 <a href="bookServlet?method=getBooks&pageNo=1"></a>
package com.servlet;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dao.BookDAO;
import com.daoimpl.BookDAOImpl;
import com.domain.Book;
import com.web.Page;
public class BookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//在doGet()方法中调用doPost()方法
doPost(request, response);
}
//一个servlet处理多个请求:可以通过在请求的时候加上参数,然后在servlet中获取请求的参数,再去调用对应的方法。达到一个servlet处理多个请求的目的
//这样一来不同的请求可以会用同一个servlet来处理,在servlet内部为不同的请求分别有不同的方法去处理。
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取对应的请求参数
String methodName = request.getParameter("method");
try {
// 利用反射获取方法
Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
//例如:getBooks方法为protected,而非public,故必须进行此操作
method.setAccessible(true);
// 执行相应的方法
method.invoke(this, request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
//处理查看书信息的请求
protected void getBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String idStr = request.getParameter("id");
int id = -1;
Book book = null;
try {
id = Integer.parseInt(idStr);
} catch (NumberFormatException e) {}
if(id > 0)
book = new BookDAOImpl<Book>().getOneBookInfo(id);
if(book == null){
response.sendRedirect(request.getContextPath() + "/error-1.jsp");
return;
}
request.setAttribute("book", book);
request.getRequestDispatcher("/WEB-INF/pages/book.jsp").forward(request, response);
}
protected void getBooks(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取请求参数
String pageNoStr = request.getParameter("pageNo");
String minPriceStr = request.getParameter("minPrice");
String maxPriceStr = request.getParameter("maxPrice");
//如果没有请求参数,设其为默认值
int pageNo = 1;
int minPrice= 0;
int maxPrice = Integer.MAX_VALUE;
//转型
try {
pageNo = Integer.parseInt(pageNoStr);
} catch (NumberFormatException e) {}
try {
minPrice = Integer.parseInt(minPriceStr);
} catch (NumberFormatException e) {}
try {
maxPrice = Integer.parseInt(maxPriceStr);
} catch (NumberFormatException e) {}
BookDAO<Book> bookDaoImpl=new BookDAOImpl<Book>();
int totalItemNumber=bookDaoImpl.getTotalNumber(minPrice, maxPrice);
List<Book> list=bookDaoImpl.getBooksList(minPrice, maxPrice, pageNo, 3);
Page<Book> page=new Page<Book>(pageNo);
page.setTotalItemNumber(totalItemNumber);
page.setList(list);
//把得到的 page 放入请求域里
request.setAttribute("bookpage", page);
//页面的转发
request.getRequestDispatcher("/WEB-INF/pages/books.jsp").forward(request, response);
}
}
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>servlet_page</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>BookServlet</display-name>
<servlet-name>BookServlet</servlet-name>
<servlet-class>com.servlet.BookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookServlet</servlet-name>
<url-pattern>/bookServlet</url-pattern>
</servlet-mapping>
</web-app>