⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basehttpservlet.java

📁 java+mapxtreme 开发校园房管系统
💻 JAVA
字号:
package com.oyc.mapxtreme.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oyc.wakeup.Session;
import com.oyc.wakeup.WakeupSessionFactory;

/**
 * HttpServlet基类: 其它HttpServlet应该继承此类
 * 
 * 当请求继承此类的servlet时,应提供一个请求调用的方法名,形如:
 * 
 * http://localhost/projectName/servletName?method=methodName
 * 
 * 子类servlet会将请求映射到具体的方法methodName上,如果不提供方法名,则执行默认的方法execute,
 * 如果url提供的方法名但找不到,那么抛出异常,子类servlet处理请求的方法形如:
 * 
 * public void methodName(HttpServletRequest request, HttpServletResponse response)
 * 		throws IOException, ServletException{}
 * 
 * 另外,继承此类的servlet不需要重写doGet,doPost方法
 * 
 * @author 三峡大学理学院 欧阳超
 *
 */
public class BaseHttpServlet extends HttpServlet {
	
	/**
	 * init servlet
	 */
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}

	/**
	 * 接收Get,Post请求,并将请求映射到具体的子类的相应方法上,如果请求的url不提供方法名,则执行默认方法
	 * 名execute,如果url提供的方法名但找不到,那么抛出异常
	 * 
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		//要调用的方法名
		String methodName=request.getParameter("method");
		//如果没有方法名,则执行默认方法
		if(methodName==null){
			methodName="execute";
		}
		
		//创建一个参数数组
		Object[] arg=new Object[]{request, response};
		
		//遍历子类servlet所有方法,找到请求的方法并执行它
		Method[] method=this.getClass().getDeclaredMethods();
		
		for(int i=0; i<method.length; i++){
			if(method[i].getName().equals(methodName)){
				try {
					method[i].invoke(this, arg);
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
				return ;
			}
		}
		
		//如果执行至此,说明没有找到请求的方法,抛出异常
		System.out.println("Error:there is no method "+methodName+" in the servlet you request !");
		throw new ServletException("there is no method "+methodName+" in the servlet you request !");
	}
	
	/**
	 * 取得数据库会话对象
	 * @return 数据库会话对象
	 * 
	 */
	public Session getWakeupSession(){
		Session sess=null;
		try{
			sess=WakeupSessionFactory.createSession();
		}catch(Exception e){
			e.printStackTrace();
		}
		return sess;
	}
	
	/**
	 * 重定向
	 * @param url
	 * @param response
	 * @throws IOException
	 */
	public void redirect(String url, HttpServletResponse response) throws IOException{
		response.sendRedirect(url);
	}
	
	/**
	 * 转发
	 * @param url
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void forward(String url, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		request.getRequestDispatcher(url).forward(request, response);
	}
	
	/**
	 * destroy the servlet
	 */
	public void destroy() {
		super.destroy();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -