?? controllerservlet.java
字號:
package org.sunxin.guestbook.controller;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.dom4j.*;
import org.dom4j.io.*;
import org.sunxin.guestbook.beans.*;
public class ControllerServlet extends HttpServlet
{
private ServletContext sc;
private static final String CONFIG_FILE = "/WEB-INF/guestbook-config.xml";
private HashMap hm=new HashMap();
public void init() throws ServletException
{
//得到ServletContext對象。
sc=getServletContext();
SAXReader saxReader = new SAXReader();
InputStream is=sc.getResourceAsStream(CONFIG_FILE);
try
{
Document doc = saxReader.read(is);
Element root = doc.getRootElement();
Element eltMapping=root.element("processor-mappings");
List childrenList=eltMapping.elements("processor");
Iterator it=childrenList.iterator();
while(it.hasNext())
{
Element elt = (Element)it.next();
hm.put(elt.attributeValue("path"),elt.attributeValue("type"));
}
}
catch(DocumentException de)
{
System.err.println(de.toString());
throw new UnavailableException("系統設置錯誤!");
}
GuestbookDB gstDB=new GuestbookDB();
sc.setAttribute("gstdb",gstDB);
}
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException
{
req.setCharacterEncoding("GB2312");
String uri=req.getRequestURI();
String ctxPath=req.getContextPath();
uri=uri.substring(ctxPath.length());
String className=(String)hm.get(uri);
if(null!=className)
{
Processor processor=null;
try
{
ClassLoader classLoader = Thread.currentThread().
getContextClassLoader();
if (classLoader == null)
{
classLoader = ControllerServlet.class.getClassLoader();
}
Class pClass = classLoader.loadClass(className);
processor = (Processor) pClass.newInstance();
}
catch (ClassNotFoundException ex)
{
System.err.println(ex.toString());
}
catch (InstantiationException ie)
{
System.err.println(ie.toString());
}
catch (IllegalAccessException iae)
{
System.err.println(iae.toString());
}
try
{
if(null!=processor)
{
//對請求進行處理,創建頁面XML文檔。
processor.perform(req, resp, sc);
}
else
{
//如果創建處理請求的Processor實例失敗,
//則拋出“請求頁面不存在”的異常信息。
ServletException se = new ServletException("您所請求的頁面不存在!");
ExceptionHandler(req, resp, sc, se);
}
}
catch (Exception e)
{
ExceptionHandler(req, resp, sc, e);
}
}
else
{
//如果用戶訪問的頁面URI沒有在guestbook-config.xml中配置,
//則拋出“請求頁面不存在”的異常信息。
ServletException se=new ServletException("您所請求的頁面不存在!");
ExceptionHandler(req,resp,sc,se);
}
//將請求轉發(forward)到XsltServlet,
//XsltServlet負責產生HTML頁面并發送到客戶端的。
req.getRequestDispatcher("/xsltservlet").forward(req, resp);
}
private void ExceptionHandler(HttpServletRequest req,
HttpServletResponse resp,
ServletContext sc,
Exception e)
{
//將異常對象保存到請求對象中。
req.setAttribute("javax.servlet.error.exception",e);
//將客戶端請求的URI保存到請求對象中。
req.setAttribute("javax.servlet.error.request_uri",req.getRequestURI());
//ErrorProcessor實例負責產生錯誤頁面的XML文檔。
new ErrorProcessor().perform(req,resp,sc);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -