public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { HttpSession sess = req.getSession(false); sess.invalidate(); System.out.println("Session Closed"); res.sendRedirect("index.html"); }
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); Enumeration values = req.getParameterNames(); String name = ""; String value = ""; String id = ""; while (values.hasMoreElements()) { name = ((String) values.nextElement()).trim(); value = req.getParameter(name).trim(); if (name.equals("id")) id = value; } if (url.equals("")) { url = getServletContext().getInitParameter("url"); cas_url = getServletContext().getInitParameter("cas_url"); } HttpSession session = null; session = req.getSession(false); if (session != null) { session.invalidate(); } res.sendRedirect(cas_url); return; }
/** * this is the main method of the servlet that will service all get requests. * * @param request HttpServletRequest * @param responce HttpServletResponce */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = null; try { try { session = request.getSession(true); } catch (Exception e) { Log.error(e, "PingSession2.doGet(...): error getting session"); // rethrow the exception for handling in one place. throw e; } // Get the session data value Integer ival = (Integer) session.getAttribute("sessiontest.counter"); // if there is not a counter then create one. if (ival == null) { ival = new Integer(1); } else { ival = new Integer(ival.intValue() + 1); } session.setAttribute("sessiontest.counter", ival); // if the session count is equal to five invalidate the session if (ival.intValue() == 5) { session.invalidate(); } try { // Output the page response.setContentType("text/html"); response.setHeader("SessionTrackingTest-counter", ival.toString()); PrintWriter out = response.getWriter(); out.println( "<html><head><title>Session Tracking Test 2</title></head><body><HR><BR><FONT size=\"+2\" color=\"#000066\">HTTP Session Test 2: Session create/invalidate <BR></FONT><FONT size=\"+1\" color=\"#000066\">Init time: " + initTime + "</FONT><BR><BR>"); hitCount++; out.println( "<B>Hit Count: " + hitCount + "<BR>Session hits: " + ival + "</B></body></html>"); } catch (Exception e) { Log.error(e, "PingSession2.doGet(...): error getting session information"); // rethrow the exception for handling in one place. throw e; } } catch (Exception e) { // log the excecption Log.error(e, "PingSession2.doGet(...): error."); // set the server responce to 500 and forward to the web app defined error page response.sendError(500, "PingSession2.doGet(...): error. " + e.toString()); } } // end of the method
/** @service the servlet service request. called once for each servlet request. */ public void service(HttpServletRequest servReq, HttpServletResponse servRes) throws IOException { String name; String value[]; String val; servRes.setHeader("AUTHORIZATION", "user fred:mypassword"); ServletOutputStream out = servRes.getOutputStream(); HttpSession session = servReq.getSession(true); session.setAttribute("timemilis", new Long(System.currentTimeMillis())); if (session.isNew()) { out.println("<p> Session is new "); } else { out.println("<p> Session is not new "); } Long l = (Long) session.getAttribute("timemilis"); out.println("<p> Session id = " + session.getId()); out.println("<p> TimeMillis = " + l); out.println("<H2>Servlet Params</H2>"); Enumeration e = servReq.getParameterNames(); while (e.hasMoreElements()) { name = (String) e.nextElement(); value = servReq.getParameterValues(name); out.println(name + " : "); for (int i = 0; i < value.length; ++i) { out.println(value[i]); } out.println("<p>"); } out.println("<H2> Request Headers : </H2>"); e = servReq.getHeaderNames(); while (e.hasMoreElements()) { name = (String) e.nextElement(); val = (String) servReq.getHeader(name); out.println("<p>" + name + " : " + val); } try { BufferedReader br = servReq.getReader(); String line = null; while (null != (line = br.readLine())) { out.println(line); } } catch (IOException ie) { ie.printStackTrace(); } session.invalidate(); }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { HttpSession session = request.getSession(); String food = (String) session.getAttribute("FOOD"); String animal = request.getParameter("ANIMAL"); session.invalidate(); response.setContentType("text/html;charset=euc-kr"); PrintWriter out = response.getWriter(); out.println("<HEAD><TITLE>성격 테스트</TITLE></HEAD>"); out.println("<BODY>"); out.println("<H3>성격 테스트</H3>"); out.printf("당신은 %s와 %s를 좋아하는 성격입니다.", food, animal); out.println("</BODY>"); out.println("</HTML>"); }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // tengo que eliminar al usuario de la sesión HttpSession session = request.getSession(false); String next = "/error.html"; if (session != null) { User user = (User) session.getAttribute("user"); session.removeAttribute("user"); session.invalidate(); next = "/login.html"; // JSP destino } RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(next); dispatcher.forward(request, response); }
/** * Logout a Trade User Dispatch to the Trade Welcome JSP for display * * @param userID The User to logout * @param ctx the servlet context * @param req the HttpRequest object * @param resp the HttpResponse object * @param results A short description of the results/success of this web request provided on the * web page * @exception javax.servlet.ServletException If a servlet specific exception is encountered * @exception javax.io.IOException If an exception occurs while writing results back to the user */ void doLogout(ServletContext ctx, HttpServletRequest req, HttpServletResponse resp, String userID) throws ServletException, IOException { String results = ""; try { tAction.logout(userID); } catch (java.lang.IllegalArgumentException e) { // this is a user error so I will // forward them to another page, at the end of the page. req.setAttribute("results", results + "illegal argument:" + e.getMessage()); // log the exception with an error level of 3 which means, handled exception but would // invalidate a automation run Log.error( e, "TradeServletAction.doLogout(...)", "illegal argument, information should be in exception string", "treating this as a user error and forwarding on to a new page"); } catch (Exception e) { // log the exception and foward to a error page Log.error( e, "TradeServletAction.doLogout(...):", "Error logging out" + userID, "fowarding to an error page"); // set the status_code to 500 throw new ServletException( "TradeServletAction.doLogout(...)" + "exception logging out user " + userID, e); } HttpSession session = req.getSession(); if (session != null) { session.invalidate(); } Object o = req.getAttribute("TSS-RecreateSessionInLogout"); if (o != null && ((Boolean) o).equals(Boolean.TRUE)) { // Recreate Session object before writing output to the response // Once the response headers are written back to the client the opportunity // to create a new session in this request may be lost // This is to handle only the TradeScenarioServlet case session = req.getSession(true); } requestDispatch(ctx, req, resp, userID, TradeConfig.getPage(TradeConfig.WELCOME_PAGE)); }
/** * Creates a db connecton. * * @return true if OK, false if there's a problem */ private boolean createDbConnection( HttpSession session, HttpServletRequest req, HttpServletResponse res) throws Exception { // At this point a driver and connection may already be set // up. So here it first tests if a connection can be made. // If not, set params up, and test the setup. if (ConnManager.getConn() == null) { ConnManager.getInstance() .setLoginParams( session.getAttribute("dbtDbDriver"), session.getAttribute("dbtDbUrl"), session.getAttribute("dbtDbUser"), session.getAttribute("dbtDbPassword")); if (ConnManager.getConn() == null) { Util.putMessagePage(res, "Cannot login to DBT database"); // Invalidates the session in case connection is "stuck" in error session.invalidate(); return false; } } return true; }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { boolean orderCompleted = true; // Get the user's session and shopping cart HttpSession session = request.getSession(true); ResourceBundle messages = (ResourceBundle) session.getAttribute("messages"); ShoppingCart cart = (ShoppingCart) session.getAttribute("cart"); if (cart == null) { cart = new ShoppingCart(); session.setAttribute("cart", cart); } // Update the inventory try { utx.begin(); bookDB.buyBooks(cart); utx.commit(); } catch (Exception ex) { try { utx.rollback(); } catch (Exception e) { System.out.println("Rollback failed: " + e.getMessage()); } System.err.println(ex.getMessage()); orderCompleted = false; } // Payment received -- invalidate the session session.invalidate(); // set content type header before accessing the Writer response.setContentType("text/html"); response.setBufferSize(8192); PrintWriter out = response.getWriter(); // then write the response out.println( "<html>" + "<head><title>" + messages.getString("TitleReceipt") + "</title></head>"); // Get the dispatcher; it gets the banner to the user RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/banner"); if (dispatcher != null) { dispatcher.include(request, response); } if (orderCompleted) { out.println("<h3>" + messages.getString("ThankYou") + request.getParameter("cardname") + "."); } else { out.println("<h3>" + messages.getString("OrderError")); } out.println( "<p> <p><strong><a href=\"" + response.encodeURL(request.getContextPath()) + "/bookstore\">" + messages.getString("ContinueShopping") + "</a> " + "</body></html>"); out.close(); }
/** Cracks the command and invokes the appropriate next screen */ public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { try { // Sets up session tracking ServletContext context = getServletContext(); HttpSession session = req.getSession(); // Gets command, which says what button was pressed String command = req.getParameter("command"); if (command == null) { Util.putMessagePage(res, "Please go back and use a button"); return; } // "Exit" invalidates the session if (command.equals("Exit")) { Util.exitSession(session, res); return; } // "Relogin" invalidates the session and starts over if (command.equals("Relogin")) { session.invalidate(); RequestDispatcher dispatcher = context.getRequestDispatcher(Util.BASE + "index.jsp"); dispatcher.forward(req, res); return; } // At this point the session may be new, or may be already // validated. If session is not valid, or if a login was // specified, do a dbt database login, and validate the // user's login. if (!Util.isSessionValid(session, null) || req.getParameter("Username") != null) { if (!createDbConnection(session, req, res)) return; if (!validateLogin(session, req, res)) return; } // Shows the DBT_DB records if (command.indexOf("Databases") > -1) { RequestDispatcher dispatcher = context.getRequestDispatcher(Util.BASE + "ShowDbs.jsp"); dispatcher.forward(req, res); return; } // Displays all the history records if (command.indexOf("History") > -1) { RequestDispatcher dispatcher = context.getRequestDispatcher(Util.BASE + "ShowHistory.jsp"); dispatcher.forward(req, res); return; } // Changes the user's password if (command.indexOf("Password") > -1) { RequestDispatcher dispatcher = context.getRequestDispatcher(Util.BASE + "ChangePassword.jsp"); dispatcher.forward(req, res); return; } // Performs admin functions if (command.indexOf("Admin") > -1) { RequestDispatcher dispatcher = context.getRequestDispatcher(Util.BASE + "AdminFnc.jsp"); dispatcher.forward(req, res); return; } Util.putMessagePage( res, "<p>Dbtracker internal error<p>Unknown command in " + getServletInfo()); } catch (ServletException e) { throw e; } catch (IOException e) { throw e; } catch (Throwable t) { Util.putExceptionPage(res, t); } }