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

📄 actionservlet.java

📁 java开发购物车+网上书城
💻 JAVA
字号:
package com.xaccp.aj3q8073.controller;

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

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

import com.xaccp.aj3q8073.vo.AdminInfoVo;

public class ActionServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ActionServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * 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 {
		this.doProcessor(request, response);
	
	}

	/**
	 * 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.doProcessor(request, response);
		
	}
	
	public void doProcessor(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
//		AdminInfoVo name= (AdminInfoVo) request.getSession().getAttribute("name");
		
		//得到form表单提交来的映射
		String servletPath=request.getServletPath();
		if(servletPath.startsWith("/")){
			servletPath=servletPath.substring(1);
		}
		if(servletPath.endsWith(".do")){
			servletPath=servletPath.substring(0,servletPath.length()-3);
		}
//		if(name==null && !servletPath.equals("login")){
//			response.sendRedirect(request.getContextPath()+"/background/adminlogin.jsp");
//			return;
//		}
		//因为在启动服务器时已经加载了配置文件(放在内存中)所以这里就可以不用在每次请求来时
		//再加载,这样就提高了效率
		ActionMapping mapping=ActionConfig.getActionMapping(servletPath);
		if(mapping==null){
			response.sendError(404,"映射错误!");
			return;
		}
		//用类的对象生成类的描述
		Class c=mapping.getAction().getClass();
		//指定方法类型
		Class[] types=new Class[]{HttpServletRequest.class,HttpServletResponse.class};
		try {
			//确定调用哪个方法并且设置类型
			Method method=c.getMethod(mapping.getMethodName(), types);
			//调用指定类中的方法并且传参
			Object resultSet= method.invoke(mapping.getAction(), new Object[]{request,response});
			//上面的反回结果是Object所以需要转化为我们需要的ActionForword
			ActionForward af=(ActionForward) resultSet;
			if(af!=null && af.getUrl()!=null && af.getUrl().length()!=0){
				//从返回结果中确定是以哪种跳转方式跳转到哪个url
				if(af.isRedirect){
					response.sendRedirect(request.getContextPath()+af.getUrl());
				}else{
					request.getRequestDispatcher(af.getUrl()).forward(request, response);
				}
			}
			
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		//获取配置文件信息
		String configPath= this.getInitParameter("config");
		//获取服务器根目录
		String realPath=this.getServletContext().getRealPath(".");
		System.out.println("系统2已经初始话!!!");
		//传递配置文件的具体路径
		ActionConfig.init(realPath+configPath);
	}

}

⌨️ 快捷键说明

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