?? mainservlet.java
字號:
package org.impact.stars.control.web;import javax.servlet.ServletException;import javax.servlet.ServletContext;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpSession;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.PrintWriter;import java.io.OutputStreamWriter;import java.beans.Beans;import java.io.IOException;import java.util.HashMap;import java.util.Locale;import javax.naming.NamingException;import javax.naming.InitialContext;import org.impact.stars.util.JNDINames;import org.impact.stars.control.exceptions.SigninFailedException;import org.impact.stars.control.exceptions.GeneralFailureException;import org.impact.stars.util.Debug;import org.impact.stars.control.web.ModelManager;import org.impact.stars.control.web.AppModelManager;import org.impact.stars.control.web.UserWebImpl;import org.impact.stars.control.web.ScreenFlowXmlDAO;import org.impact.stars.control.web.RequestProcessor;import org.impact.stars.control.web.checker.CheckerManager;import org.impact.stars.util.WebKeys;import org.impact.stars.util.JSPUtil;public class MainServlet extends HttpServlet { private HashMap urlMappings; public void init() { Debug.println("MainServlet: Initializing"); String requestMappingsURL = null; try { requestMappingsURL = getServletContext().getResource("/WEB-INF/xml/requestmappings.xml").toString(); } catch (java.net.MalformedURLException ex) { Debug.println("ScreenFlowManager: initializing ScreenFlowManager malformed URL exception: " + ex); } urlMappings = ScreenFlowXmlDAO.loadRequestMappings(requestMappingsURL); getServletContext().setAttribute(WebKeys.URLMappingsKey, urlMappings); Debug.println("MainServlet: loaded urlMappings"); String serverType = null; try { InitialContext ic = new InitialContext(); serverType = (String)ic.lookup(JNDINames.SERVER_TYPE); getServletContext().setAttribute(WebKeys.ServerTypeKey, serverType); } catch (NamingException ex) { Debug.println("Server Type not specified in deployment descriptor: using default J2ee Security Adapter"); } getScreenFlowManager(); getRequestProcessor(); getAppModelManager(); getCheckerManager(); Debug.println("MainServlet: Initialization complete"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doGet(request, response); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String selectedURL = request.getPathInfo(); String client = request.getParameter("client"); // the current tomcat is resetting the outputstream so this is a workaround if ((selectedURL != null) && selectedURL.equals("/white")) return; HttpSession session = request.getSession(); ScreenFlowManager screenManager = null; ModelManager modelManager= (ModelManager)request.getSession().getAttribute(WebKeys.ModelManagerKey); if ( modelManager == null ) { try { modelManager = (ModelManager) Beans.instantiate(this.getClass().getClassLoader(), "org.impact.stars.control.web.ModelManager"); } catch (Exception exc) { throw new ServletException ("Cannot create bean of class ModelManager"); } session.setAttribute(WebKeys.ModelManagerKey, modelManager); modelManager.init(getServletContext(), session); } Debug.println("MainServlet: url " + selectedURL); // check if url is protected before processing request URLMapping current = getURLMapping(selectedURL); if ((current != null) && current.requiresSignin()) { UserWebImpl user = modelManager.getUserWebImpl(); if (user.isLoggedIn()) { doProcess(request, response); } else { String signinScreen = getScreenFlowManager().getSigninScreen(); session.setAttribute(WebKeys.CurrentScreen, signinScreen); session.setAttribute(WebKeys.SigninTargetURL, selectedURL); } } else { doProcess(request, response); } /** Default to the base language or the site. If a language is found in the session use that template. */ /* If client is not applet, select the next view page for it. * The client must specif URL as ?client=page */ Debug.println("checking client: "+client); if((client!= null)&&(client.equals("page"))) { Locale locale = JSPUtil.getLocale(request.getSession()); Debug.println("forwarding to webpage:" + getScreenFlowManager().getTemplate(locale)); getServletConfig().getServletContext().getRequestDispatcher(getScreenFlowManager().getTemplate(locale)).forward(request, response); } } private void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException { try { getRequestProcessor().processRequest(request, response); getScreenFlowManager().getNextScreen(request); } catch (Throwable ex) { String className = ex.getClass().getName(); String exceptionScreen = getScreenFlowManager().getExceptionScreen(className); Debug.println("MainServlet: target screen is: " + exceptionScreen); // put the exception in the request request.setAttribute("javax.servlet.jsp.jspException", ex); if (exceptionScreen != null) { request.getSession().setAttribute(WebKeys.CurrentScreen, exceptionScreen); } else { // send to general error screen Debug.println("MainServlet: unknown exception: " + className); throw new ServletException("MainServlet: unknown exception: " + className); } } } private RequestProcessor getRequestProcessor() { RequestProcessor rp = (RequestProcessor)getServletContext().getAttribute(WebKeys.RequestProcessorKey); if ( rp == null ) { Debug.println("MainServlet: initializing request processor"); rp = new RequestProcessor(); rp.init(getServletContext()); getServletContext().setAttribute(WebKeys.RequestProcessorKey, rp); } return rp; } private ScreenFlowManager getScreenFlowManager() { ScreenFlowManager screenManager = (ScreenFlowManager)getServletContext().getAttribute(WebKeys.ScreenManagerKey); if (screenManager == null ) { Debug.println("MainServlet: Loading screen flow definitions"); screenManager = new ScreenFlowManager(); screenManager.init(getServletContext()); getServletContext().setAttribute(WebKeys.ScreenManagerKey, screenManager); } return screenManager; } private AppModelManager getAppModelManager() { AppModelManager apmm = (AppModelManager)getServletContext().getAttribute(WebKeys.AppModelManagerKey); if (apmm == null ) { Debug.println("MainServlet: Loading App Manger"); apmm = new AppModelManager(); apmm.init(getServletContext()); getServletContext().setAttribute(WebKeys.AppModelManagerKey, apmm); } return apmm; } private CheckerManager getCheckerManager() { CheckerManager chm = (CheckerManager)getServletContext().getAttribute(WebKeys.CheckerManagerKey); if (chm == null ) { Debug.println("MainServlet: Loading CheckerManager"); chm = new CheckerManager(); chm.init(getRequestProcessor(), getServletContext()); getServletContext().setAttribute(WebKeys.CheckerManagerKey, chm); } return chm; } /** * The UrlMapping object contains information that will match * a url to a mapping object that contains information about * the current screen, the RequestHandler that is needed to * process a request, and the RequestHandler that is needed * to insure that the propper screen is displayed. */ private URLMapping getURLMapping(String urlPattern) { if ((urlMappings != null) && urlMappings.containsKey(urlPattern)) { return (URLMapping)urlMappings.get(urlPattern); } else { return null; } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -