……
/** * Front Controller. * * @author Rafael Steil * @version $Id: JForum.java,v 1.116 2007/10/10 04:54:20 rafaelsteil Exp $ */ public class JForum extends JForumBaseServlet {
/** * 每次请求(即以.page结尾的url),都被映射到该servlet的业务方法,<p> * 1.把一个servlet的url配置成*page,那么该servlet的作用本质上是和过滤器是一样的,即对每次请求进行拦截!<br> * 2. * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { Writer out = null; JForumContext forumContext = null; RequestContext request = null; ResponseContext response = null; String encoding = SystemGlobals.getValue(ConfigKeys.ENCODING); try { // Initializes the execution context JForumExecutionContext ex = JForumExecutionContext.get(); request = new WebRequestContext(req); response = new WebResponseContext(res); this.checkDatabaseStatus(); forumContext = new JForumContext(request.getContextPath(), SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION), request, response ); ex.setForumContext(forumContext); JForumExecutionContext.set(ex); // Setup stuff SimpleHash context = JForumExecutionContext.getTemplateContext(); ControllerUtils utils = new ControllerUtils(); utils.refreshSession(); context.put("logged", SessionFacade.isLogged()); // Process security data SecurityRepository.load(SessionFacade.getUserSession().getUserId()); utils.prepareTemplateContext(context, forumContext); String module = request.getModule(); // Gets the module class name String moduleClass = module != null ? ModulesRepository.getModuleClass(module) : null; if (moduleClass == null) { // Module not found, send 404 not found response response.sendError(HttpServletResponse.SC_NOT_FOUND); } else { boolean shouldBan = this.shouldBan(request.getRemoteAddr()); if (!shouldBan) { context.put("moduleName", module); context.put("action", request.getAction()); } else { moduleClass = ModulesRepository.getModuleClass("forums"); context.put("moduleName", "forums"); ((WebRequestContext)request).changeAction("banned"); } if (shouldBan && SystemGlobals.getBoolValue(ConfigKeys.BANLIST_SEND_403FORBIDDEN)) { response.sendError(HttpServletResponse.SC_FORBIDDEN); } else { context.put("language", I18n.getUserLanguage()); context.put("session", SessionFacade.getUserSession()); context.put("request", req); context.put("response", response); out = this.processCommand(out, request, response, encoding, context, moduleClass); } } } catch (Exception e) { this.handleException(out, response, encoding, e, request); } finally { this.handleFinally(out, forumContext, response); } }
说明:在这里,创建了封装请求对象。
(创建对象的时候,把请求参数添加到映射里)
public class WebRequestContext extends HttpServletRequestWrapper implements RequestContext { private Map query; //{module=forums, forum_id=1, action=show} /** * Default constructor. * * @param superRequest Original <code>HttpServletRequest</code> instance * @throws IOException */ public WebRequestContext(HttpServletRequest superRequest) throws IOException { super(superRequest); this.query = new HashMap(); boolean isMultipart = false; String requestType = superRequest.getMethod().toUpperCase(); String contextPath = superRequest.getContextPath(); String requestUri = this.extractRequestUri(superRequest.getRequestURI(), contextPath); String encoding = SystemGlobals.getValue(ConfigKeys.ENCODING); String servletExtension = SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION); boolean isPost = "POST".equals(requestType); boolean isGet = !isPost; boolean isQueryStringEmpty = (superRequest.getQueryString() == null || superRequest.getQueryString().length() == 0); if (isGet && isQueryStringEmpty && requestUri.endsWith(servletExtension)) { superRequest.setCharacterEncoding(encoding); this.parseFriendlyURL(requestUri, servletExtension); } else if (isPost) { isMultipart = ServletFileUpload.isMultipartContent(new ServletRequestContext(superRequest)); if (isMultipart) { this.handleMultipart(superRequest, encoding); } } if (!isMultipart) { boolean isAjax = "XMLHttpRequest".equals(superRequest.getHeader("X-Requested-With")); if (!isAjax) { superRequest.setCharacterEncoding(encoding); } else { // Ajax requests are *usually* sent using application/x-www-form-urlencoded; charset=UTF-8. // In JForum, we assume this as always true. superRequest.setCharacterEncoding("UTF-8"); } String containerEncoding = SystemGlobals.getValue(ConfigKeys.DEFAULT_CONTAINER_ENCODING); if (isPost) { containerEncoding = encoding; } for (Enumeration e = superRequest.getParameterNames(); e.hasMoreElements(); ) { String name = (String)e.nextElement(); String[] values = superRequest.getParameterValues(name); if (values != null && values.length > 1) { for (int i = 0; i < values.length; i++) { this.addParameter(name, new String(values[i].getBytes(containerEncoding), encoding)); } } else { this.addParameter(name, new String(superRequest.getParameter(name).getBytes(containerEncoding), encoding)); } } if (this.getModule() == null && this.getAction() == null) { int index = requestUri.indexOf('?'); if (index > -1) { requestUri = requestUri.substring(0, index); } this.parseFriendlyURL(requestUri, servletExtension); } } }
(具体的添加请求参数到映射的方法)
public void addParameter(String name, Object value) { if (!this.query.containsKey(name)) { this.query.put(name, value); } else { Object currentValue = this.getObjectParameter(name); List l; if (!(currentValue instanceof List)) { l = new ArrayList(); l.add(currentValue); } else { l = (List)currentValue; } l.add(value); this.query.put(name, l); } }