applicationservlet.java

来自「100多M的J2EE培训内容」· Java 代码 · 共 83 行

JAVA
83
字号
package bible.servlets;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;public class ApplicationServlet extends HttpServlet {  private static final int FIRST_PAGE = 1;  private static final int LAST_PAGE = 5;  public void service(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {    ServletContext ctx = this.getServletContext();    HttpSession session = request.getSession();    String userName = request.getRemoteUser();    Hashtable sessionInfo = null;    if (session.getAttribute("sessionInfo") == null) {      // New user. Create a Hashtable to track their      // session state.      sessionInfo = new Hashtable();      sessionInfo.put("userName", userName);      sessionInfo.put("pageNumber", new Integer(FIRST_PAGE));      session.setAttribute("sessionInfo", sessionInfo);      // See if the new user is already logged in by checking      // for their sessionInfo in the servlet context.      if (ctx.getAttribute("session_" + userName) == null) {        // Not already logged in. Save their sessionInfo to        // the servletContext and continue.        ctx.setAttribute("session_" + userName, sessionInfo);      } else {        // Already logged in elsewhere. Invalidate this session        // and kick this user out.        session.invalidate();        response.sendRedirect(response.encodeURL("/BibleServlets/Home.html"));        return;      }    } else {      // Returning user. Retrieve their state from the session.      sessionInfo = (Hashtable) session.getAttribute("sessionInfo");      Integer pageNumber = (Integer)sessionInfo.get("pageNumber");      int nextPage = pageNumber.intValue();      pageNumber = null;      // Read the query string to determine the user's desired      // action. Update their state in the session accordingly.      if (request.getParameter("action").equals("next")) {        // Go to the next page. Wrap around to the        // first page after passing the last page.        nextPage++;        if (nextPage > LAST_PAGE) {          nextPage = FIRST_PAGE;        }        sessionInfo.put("pageNumber", new Integer(nextPage));        session.setAttribute("sessionInfo", sessionInfo);        ctx.setAttribute("session_" + userName, sessionInfo);      } else if (request.getParameter("action").equals("previous")) {        // Go to the previous page. Wrap around to the last page        // after passing the first page.        nextPage--;        if (nextPage < FIRST_PAGE) {          nextPage = LAST_PAGE;        }        sessionInfo.put("pageNumber", new Integer(nextPage));        session.setAttribute("sessionInfo", sessionInfo);        ctx.setAttribute("session_" + userName, sessionInfo);      } else if (request.getParameter("action").equals("logoff")) {        // User wants to log off. Invalidate their session,        // remove their session info from the servlet context,        // redirect them to the home page, and then quit.        session.invalidate();        ctx.removeAttribute("session_" + userName);        response.sendRedirect(response.encodeURL("/BibleServlets/Home.html"));        return;      }    }    // Forward all non-logoff requests to the PageServlet.    request.getRequestDispatcher("PageServlet").forward(request, response);  }}

⌨️ 快捷键说明

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