一个servlet处理多个请求 (利用反射获取方法)

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>

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sinat_36710456

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值