ApplicationServlet.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:3k
源码类别:
Java编程
开发平台:
Java
- 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);
- }
- }