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

📄 commonservlet.java

📁 在线模拟选课系统
💻 JAVA
字号:
/*
 * This product includes software developed by the
 * Apache Software Foundation (http://www.apache.org/).
 */
package ch07.servlet;

import java.io.*;
import java.util.*;
import java.lang.reflect.*;

import javax.servlet.*;
import javax.servlet.http.*;

import org.jdom.input.SAXBuilder;
import org.apache.commons.jxpath.JXPathContext;

import ch07.global.*;
/**
 * 针对所有页面的共同父类Servlet
 * 执行一些共通异常处理的功能
 * @author ShenYK
 * @version 1.0
 */
public class CommonServlet extends HttpServlet
{
    //保存各页面Id对应的action类的对象
    private Hashtable hPageHandler = new Hashtable();
    
    //配置文件的存放位置
    private JXPathContext configContext = null;
    
    public void init()
    {
        //取得配置文件,并获得其中的dom元素
        String filePath = getInitParameter("configXML");
        String fileRealPath = getServletContext().getRealPath(filePath);
        
        //尝试建立配置文件的DOM
        try
        {
            org.jdom.input.SAXBuilder  builder = new SAXBuilder();
            org.jdom.Document pDoc = builder.build(fileRealPath );
            
            configContext = JXPathContext.newContext(pDoc);
            
            GlobalObjectProvider.init( configContext );
        }
        catch(Exception e)
        {
            System.out.println("Servlet初始化失败!");
        }
    }
    
    //每一种动作第一次执行的时候,初始化对应的类
    public void doPost ( HttpServletRequest request, 
                          HttpServletResponse response )
        throws ServletException, IOException 
    {
        //设置提交表单的中文编码
        request.setCharacterEncoding("GBK");
        
        HttpSession mySession = request.getSession(true);
        
        //得到用户输入信息
        String sPageId = request.getParameter("pageId");
        String sActionId = request.getParameter("actionId");
        
        if ( sPageId == null || sPageId.equals("")
           || sActionId == null || sActionId.equals("") )
        {
            //非法进入页面,跳转到首页
            mySession.invalidate();
            response.sendRedirect("../login.jsp");
            return;
        }
        
        //如果非法进入页面(登录页面除外)
        if ( !sPageId.equals("S001") 
           && mySession.getAttribute("loginUser") == null )
        {
            //非法进入页面,跳转到首页
            mySession.invalidate();
            response.sendRedirect("../login.jsp");
            return;
        }
        
        try
        {
            //根据pageId获得处理对象,如果没有则创建一个对象
            Object oActionObject = hPageHandler.get( sPageId );
            if ( oActionObject == null )
            {
                //根据配置文件创建一个新对象
                String sClassName = (String)configContext.getValue(
                        "ch07-config/page[@id='"+sPageId+"']/@className");
                
                oActionObject = Class.forName( sClassName ).newInstance();
                hPageHandler.put( sPageId, oActionObject);
            }
            //取得方法名
            String sMethodName = (String)configContext.getValue(
                    "ch07-config/page[@id='"+sPageId+"']/action[@id='"+sActionId+"']/@methodName");
            
            //生成对应的参数,并调用对应对象的对应方法
            //inputData是根据传入的参数做成的
            Hashtable inputData = new Hashtable();
            Enumeration params = request.getParameterNames();
            while( params.hasMoreElements())
            {
                String sParaName = (String)params.nextElement();
                inputData.put( sParaName, request.getParameter(sParaName) );
            }
            //outputData是下一个页面的值域,在此只是被初始化
            Hashtable outputData = new Hashtable();
            
            //生成参数列表
            Class[] paraType = { Class.forName("java.util.Hashtable"),
                                 Class.forName("java.util.Hashtable"),
                                 Class.forName("javax.servlet.http.HttpSession") };
            Object[] paraObj = { inputData, outputData, mySession };
            
            //生成Method对象
            Method invokeMethod = oActionObject.getClass().getMethod( sMethodName, paraType );
            
            //调用方法
            invokeMethod.invoke( oActionObject, paraObj );
            
            //根据outputData的结果决定下一个页面
            String sNextPageId = (String)outputData.get("pageId");
            String sRealPagePath = (String)configContext.getValue(
                        "ch07-config/page[@id='"+sNextPageId+"']/@path");
            //设置下一个页面的值域
            mySession.setAttribute( sNextPageId, outputData );
            response.sendRedirect( sRealPagePath );
            return;
        }
        catch(Exception e)
        {
            //页面处理出错,跳转到首页
            e.printStackTrace();
            mySession.invalidate();
            response.sendRedirect("../login.jsp");
            return;
        }
    }
    
    public void doGet ( HttpServletRequest request, 
            HttpServletResponse response )
        throws ServletException, IOException 
    {
        doPost( request, response );
    }
}

⌨️ 快捷键说明

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