portalservlet.java

来自「JAVA Servlet2.3外文书籍源码」· Java 代码 · 共 88 行

JAVA
88
字号
package personalPortal;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

/**
 * Title:        Professional Java Servlet Programming - Chapter 2
 * Description:  Application Servlet that processes all requests made
 *               to the application.  No HTML is directly output from
 *               this page, but from HTMLPortal object and other resources.
 * Copyright:    Copyright (c) 2001
 * Company:
 * @author Andrew Harbourne-Thomas
 * @version 1.0
 */
public class PortalServlet extends GenericServlet implements DataMapping {

  /** static variable to keep a count of application access */
  private static int requestCount = 0;

  /**
   * map to store name of request and location of resource
   *  (servlet/HTML page)
   */
  private Map resourceMap = null;

  /**
   * initialises the map object with the resource data
   */
  public void init() {
    resourceMap = new HashMap();
    for (int i = 0; i < RESOURCE_DATA.length; i++) {
      resourceMap.put(RESOURCE_DATA[i][NAME], RESOURCE_DATA[i][RESOURCE]);
    }
  }

  /**
   * Here the logic for processing all requests is managed
   *
   * @param request The object containing the client request
   * @param response The object used to send the response back
   */
  public void service(ServletRequest request, ServletResponse response)
    throws ServletException, IOException {
    requestCount++;
    ServletContext context = getServletConfig().getServletContext();

    //the basic parameters to interpret the request
    String pageViewType = request.getParameter(PORTAL_VIEW_PARAM_NAME);
    String pageRequested = request.getParameter(PAGE_REQUESTED_NAME);

    //in case they are not set (eg clients first access),
    //we set them to default to the News page, normal view.
    if (pageViewType == null) {
      pageViewType = PORTAL_VIEW_PARAM;
    }
    if (pageRequested == null) {
      pageRequested = NEWS_NAME;
    }

    //create the object to output the basic html page
    HTMLPortal htmlPortal = new HTMLPortal(pageRequested, pageViewType);
    String requestedResource = (String)resourceMap.get(pageRequested);

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    //print the title and menu of the page
    out.println(htmlPortal.getOpenPage());

    RequestDispatcher dispatcher = context.getRequestDispatcher(requestedResource);
    if (dispatcher != null) {
      //pass the count and date variables as attributes
      request.setAttribute(REQUEST_COUNT, new Integer(requestCount).toString());
      dispatcher.include(request, response);
    }
    else {
      //this should only happen during development
      //or if a resource is removed
      out.println(htmlPortal.getError("Request Resource not found"));
    }

    //close off the html page, then close the output object
    out.println(htmlPortal.getClosePage());
    out.close();
  }
}

⌨️ 快捷键说明

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