AdminServlet.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:2k
源码类别:
Java编程
开发平台:
Java
- package bible.servlets;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.io.*;
- import java.util.*;
- public class AdminServlet extends HttpServlet {
- public void service(HttpServletRequest request, HttpServletResponse response)
- throws IOException {
- ServletContext ctx = this.getServletContext();
- HttpSession session = request.getSession();
- String servletName = this.getServletName();
- if (request.getParameter("action") == null) {
- // Generate an HTML page listing all active sessions.
- // This servlet does NOT store data into the session
- // or the servlet context.
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- out.println("<html>");
- out.println("<head><title>" + servletName + "</title></head>");
- out.println("<body>");
- out.print("<p>Welcome to the " + servletName + ". ");
- out.print("Click your browser's Refresh button ");
- out.print("to update the display.</p>");
- // Get the names of all attributes in the servlet context.
- Enumeration sessions = ctx.getAttributeNames();
- int numSessions = 0;
- // Loop through the attributes, rendering the ones we recognize
- // as our sessions to the HTML output.
- while (sessions.hasMoreElements()) {
- String sessionName = (String) sessions.nextElement();
- if (sessionName.startsWith("session_")) {
- Hashtable sessionInfo = (Hashtable) ctx.getAttribute(sessionName);
- String userName = (String) sessionInfo.get("userName");
- Integer pageNumber = (Integer) sessionInfo.get("pageNumber");
- out.println("<p>" + userName + " is on page " + pageNumber.intValue() + ".</p>");
- numSessions++;
- }
- }
- // If there are no sessions, indicate that as well.
- if (numSessions == 0) {
- out.println("<p>There are no active sessions.</p>");
- }
- out.println("<p></p>");
- out.println("<p><a href="" + response.encodeURL(servletName + "?action=logoff") + "">Log Off</a></p>");
- out.println("</body></html>");
- } else if (request.getParameter("action").equals("logoff")) {
- // User wants to log off. Invalidate their session and
- // redirect them to the home page.
- session.invalidate();
- response.sendRedirect(response.encodeURL("/BibleServlets/Home.html"));
- }
- }
- }