public static void showSession(HttpServletRequest req, PrintStream out) { // res.setContentType("text/html"); // Get the current session object, create one if necessary HttpSession session = req.getSession(); out.println("Session id: " + session.getId()); out.println(" session.isNew(): " + session.isNew()); out.println(" session.getMaxInactiveInterval(): " + session.getMaxInactiveInterval() + " secs"); out.println( " session.getCreationTime(): " + session.getCreationTime() + " (" + new Date(session.getCreationTime()) + ")"); out.println( " session.getLastAccessedTime(): " + session.getLastAccessedTime() + " (" + new Date(session.getLastAccessedTime()) + ")"); out.println(" req.isRequestedSessionIdFromCookie: " + req.isRequestedSessionIdFromCookie()); out.println(" req.isRequestedSessionIdFromURL: " + req.isRequestedSessionIdFromURL()); out.println(" req.isRequestedSessionIdValid: " + req.isRequestedSessionIdValid()); out.println("Saved session Attributes:"); Enumeration atts = session.getAttributeNames(); while (atts.hasMoreElements()) { String name = (String) atts.nextElement(); out.println(" " + name + ": " + session.getAttribute(name) + "<BR>"); } }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String amount = request.getParameter("amount"); String amount2 = request.getParameter("amount2"); String amount3 = request.getParameter("amount3"); Integer posotita = Integer.parseInt(amount); Integer posotita2 = Integer.parseInt(amount2); Integer posotita3 = Integer.parseInt(amount3); HttpSession session = request.getSession(); if (session.isNew()) { request.setAttribute("sessionVal", "this is a new session"); } else { request.setAttribute("sessionVal", "Welcome Back!"); } double total = ((posotita * 18.50) + (posotita2 * 6.95) + (posotita3 * 1.29)); session.setAttribute("totalVal", total); request.setAttribute("currency", total); request.setAttribute("from", amount); request.setAttribute("from2", amount2); request.setAttribute("from3", amount3); RequestDispatcher view = request.getRequestDispatcher("index.jsp"); view.forward(request, response); }
/** Get the current session, creating it if necessary (and set the timeout if so) */ protected HttpSession getSession() { if (session == null) { session = req.getSession(true); if (session.isNew()) { setSessionTimeout(session); } } return session; }
public static void showSession(HttpServletRequest req, HttpServletResponse res, PrintStream out) { // res.setContentType("text/html"); // Get the current session object, create one if necessary HttpSession session = req.getSession(); // Increment the hit count for this page. The value is saved // in this client's session under the name "snoop.count". Integer count = (Integer) session.getAttribute("snoop.count"); if (count == null) { count = 1; } else count = count + 1; session.setAttribute("snoop.count", count); out.println(HtmlWriter.getInstance().getHtmlDoctypeAndOpenTag()); out.println("<HEAD><TITLE>SessionSnoop</TITLE></HEAD>"); out.println("<BODY><H1>Session Snoop</H1>"); // Display the hit count for this page out.println( "You've visited this page " + count + ((!(count.intValue() != 1)) ? " time." : " times.")); out.println("<P>"); out.println("<H3>Here is your saved session data:</H3>"); Enumeration atts = session.getAttributeNames(); while (atts.hasMoreElements()) { String name = (String) atts.nextElement(); out.println(name + ": " + session.getAttribute(name) + "<BR>"); } out.println("<H3>Here are some vital stats on your session:</H3>"); out.println("Session id: " + session.getId() + " <I>(keep it secret)</I><BR>"); out.println("New session: " + session.isNew() + "<BR>"); out.println("Timeout: " + session.getMaxInactiveInterval()); out.println("<I>(" + session.getMaxInactiveInterval() / 60 + " minutes)</I><BR>"); out.println("Creation time: " + session.getCreationTime()); out.println("<I>(" + new Date(session.getCreationTime()) + ")</I><BR>"); out.println("Last access time: " + session.getLastAccessedTime()); out.println("<I>(" + new Date(session.getLastAccessedTime()) + ")</I><BR>"); out.println( "Requested session ID from cookie: " + req.isRequestedSessionIdFromCookie() + "<BR>"); out.println("Requested session ID from URL: " + req.isRequestedSessionIdFromURL() + "<BR>"); out.println("Requested session ID valid: " + req.isRequestedSessionIdValid() + "<BR>"); out.println("<H3>Test URL Rewriting</H3>"); out.println("Click <A HREF=\"" + res.encodeURL(req.getRequestURI()) + "\">here</A>"); out.println("to test that session tracking works via URL"); out.println("rewriting even when cookies aren't supported."); out.println("</BODY></HTML>"); }
/** @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(); }
/** * Permet de repondre a une requete web En affichant la liste des Spectacles et representations : * Utiliste JQuery javascript pour la mise en forme * * @param HttpServletRequest request requete * @param HttpServletResponse response reponse * @throw IOException, ServletException * @return void */ public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // Get the session object HttpSession session = req.getSession(true); // Get the output stream ServletOutputStream out = res.getOutputStream(); res.setContentType("text/html"); out.println("<HEAD><TITLE>Reservation de tickets </TITLE></HEAD><BODY>"); out.println("<h1> Reservations de tickets </h1>"); out.println("<BODY bgproperties=\"fixed\" background=\"/images/rideau.JPG\">"); out.println("<p align=\"Right\"><font face=\"Monotype Corsiva\"style=\"font-size: 16pt\">"); try { // Open the file that is the first // command line parameter String relativeWebPath = "/WEB-INF/files/JAVASCRIPTPROG.txt"; String absoluteDiskPath = this.getServletContext().getRealPath(relativeWebPath); File file = new File(absoluteDiskPath); FileInputStream fstream = new FileInputStream(file); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; // Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console out.println(strLine); } // Close the input stream in.close(); } catch (Exception e) { // Catch exception if any out.println("Error: " + e.getMessage()); } if (session.isNew() || session.getAttribute("session.PanierListe") == null) out.println("<a href=\"admin/admin.html\">Caddie (vide)</a></font><br></p>"); else if (session.getAttribute("session.PanierListe") != null) if (((PanierListe) session.getAttribute("session.PanierListe")).getSize() > 0) out.println( "<a href=\"admin/admin.html\">afficher caddie(" + ((PanierListe) session.getAttribute("session.PanierListe")).Liste.size() + "Representations dans le panier)" + "</a></font><br></p>"); try { Utilisateur user = Utilitaires.Identification(this); out.println(Utilitaires.AffichageAchat(user)); } catch (Exception e) { out.println(e.getMessage()); } out.println("</BODY>"); out.close(); }
public void processRequest(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { HttpSession session = req.getSession(true); root = req.getContextPath(); String addButton = req.getParameter("addButton"); try { session = req.getSession(true); if (session.isNew()) { session.invalidate(); res.setContentType("text/html;charset=UTF-8"); PrintWriter out = res.getWriter(); out.println( (new StringBuilder()) .append("<html><HEAD><META HTTP-EQUIV='Refresh' CONTENT='0; URL=") .append(root) .append("/AID'/></HEAD></html>") .toString()); out.close(); } } catch (IllegalStateException e) { res.sendRedirect((new StringBuilder()).append(root).append("/AID").toString()); } String originalQuery = (String) session.getAttribute("query"); if (addButton != null) { String newQueryTerms[] = (String[]) req.getParameterValues("newQueryTerms"); if ((newQueryTerms == null) || (newQueryTerms.length == 0)) { res.sendRedirect( res.encodeURL( root.concat( (new StringBuilder()) .append("/AID?query=") .append(originalQuery.replaceAll("\\s+", "+")) .toString()))); } else { String newQuery = originalQuery.replaceAll("\\s+", "+"); for (int i = 0; i < newQueryTerms.length; i++) { newQuery = (new StringBuilder()) .append(newQuery) .append("+") .append(newQueryTerms[i]) .toString(); } res.sendRedirect( res.encodeURL( root.concat( (new StringBuilder()).append("/AID?query=").append(newQuery).toString()))); } } else { String spellMatrix[] = (String[]) session.getAttribute("retSpellSuggestions"); String termsInQuery[] = (String[]) session.getAttribute("termsInQuery"); String wordnetMatrix[][] = (String[][]) session.getAttribute("retWordnetSynsMatrix"); String synonymMatrix[][] = (String[][]) session.getAttribute("retSynsMatrix"); String onlineSynMatrix[][] = (String[][]) session.getAttribute("onlineSynMatrix"); res.setContentType("text/html;charset=UTF-8"); PrintWriter out = res.getWriter(); out.println( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">"); out.println("<html>"); out.println("<head>"); out.println("<title>AID Search interface</title>"); out.println( (new StringBuilder()) .append("<style type='text/css'>@import url(") .append(root) .append("/css/qckcss.css);") .toString()); out.println("</style>"); out.println( (new StringBuilder()) .append("<link REL=\"SHORTCUT ICON\" HREF=\"") .append(root) .append("/images/favicon.ico\">") .toString()); out.println( (new StringBuilder()) .append("<script type=\"text/javascript\" src=\"") .append(root) .append("/javascript/overlib.js\">") .toString()); out.println("<!-- bla -->"); out.println("</script>"); out.println("</head>"); out.println( (new StringBuilder()) .append("<body bgcolor=white background=\"") .append(root) .append("/images/background.gif\">") .toString()); out.println("<div id=\"AIDfp\">"); out.println("<a name=\"top\"/>"); out.println(" <table border=\"0\" width=600>"); out.println("<tr><td colspan=3>"); out.println("<table>"); out.println("\t<tr valign=\"top\" bgcolor=white><td class=\"navtext\">"); out.println("\t<div id=\"navlinks\">"); out.println( (new StringBuilder()) .append("\t\t<img src=\"") .append(root) .append( "/images/top.png\" width=\"709\" height=\"200\" border=\"0\"><!-- bla --> </img>") .toString()); out.println( (new StringBuilder()) .append( "\t\t<div style=\"position: relative;top: -55px;margin-left: 15px;\"><a href=\"") .append(root) .append("/\">Home</a></div>") .toString()); out.println( "\t\t<div style=\"position: relative;top: -71px;margin-left: 180px;\">Concept Finder</div>"); out.println( "\t\t<div style=\"position: relative;top: -87px;margin-left: 410px;\">Search Details</div>"); out.println( "\t\t<div style=\"position: relative;top: -103px;margin-left: 620px;\">History</div>"); out.println("</div></td></tr></table>"); out.println("</td></tr>"); out.println( (new StringBuilder()) .append(" <form name=\"selectionForm\" method=\"get\" action=\"") .append(res.encodeURL(req.getRequestURI())) .append("\">") .toString()); out.println(" <tr>"); out.println(" <td width=33% class='resultItemCenter'>"); if (synonymMatrix != null) { out.println(" Found index-specific syonyms:<br>"); out.println(" <select name='newQueryTerms' multiple>"); for (int i = 0; i < synonymMatrix.length; i++) { for (int j = 0; j < synonymMatrix[i].length; j++) { if (synonymMatrix[i][j] != null) { out.println( (new StringBuilder()) .append(" <option value='") .append(synonymMatrix[i][j]) .append("'>") .append(synonymMatrix[i][j]) .append("</option>") .toString()); } } } out.println(" </select>"); } out.println(" </td>"); out.println(" <td width=33% class='resultItemCenter'>"); if (wordnetMatrix != null) { out.println(" Found Wordnet syonyms:<br>"); out.println(" <select name='newQueryTerms' multiple>"); for (int i = 0; i < wordnetMatrix.length; i++) { for (int j = 0; j < wordnetMatrix[i].length; j++) { if (wordnetMatrix[i][j] != null) { out.println( (new StringBuilder()) .append(" <option value='") .append(wordnetMatrix[i][j]) .append("'>") .append(wordnetMatrix[i][j]) .append("</option>") .toString()); } } } out.println(" </select>"); } out.println(" </td>"); out.println(" <td width=33% class='resultItemCenter'>"); if (onlineSynMatrix != null) { out.println(" Found online acronyms:<br>"); out.println(" <select name='newQueryTerms' multiple>"); for (int i = 0; i < onlineSynMatrix.length; i++) { for (int j = 0; j < onlineSynMatrix[i].length; j++) { if (onlineSynMatrix[i][j] != null) { out.println( (new StringBuilder()) .append(" <option value='") .append(onlineSynMatrix[i][j]) .append("'>") .append(onlineSynMatrix[i][j]) .append("</option>") .toString()); } } } out.println(" </select>"); } out.println(" </td> </tr>"); out.println(" <tr>"); out.println(" <td class='resultItemCenter' width=100% colspan=3>"); out.println(" <hr/>"); out.println(" </td>"); out.println(" </tr>"); out.println(" <tr>"); out.println(" <td class='resultItemCenter' width=100% colspan=3>"); out.println(" <input type=submit name=\"addButton\" value=\"Add\">"); out.println(" </td>"); out.println(" </tr>"); out.println(" </form>"); out.println(" </table>"); out.println("<div id=\"footer\">"); out.println("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">"); out.println("<tr>"); out.println( (new StringBuilder()) .append("<td width=\"1%\"><img src=\"") .append(root) .append( "/images/footer-leftcurve.gif\" width=\"10\" height=\"31\" border=\"0\"/></td>") .toString()); out.println("<td width=\"98%\" bgcolor=\"#\" class=\"footertext\">"); out.println("<a href=\"#top\">Top</a>"); out.println(" |\t\t"); out.println( (new StringBuilder()) .append("<a href=\"") .append(root) .append("/synonym\">Synonym client</a>") .toString()); out.println(" |\t\t"); out.println("<a href=\"http://www.vl-e.nl\">Vl-e</a>"); out.println("</td>"); out.println( (new StringBuilder()) .append("<td width=\"1%\"><img src=\"") .append(root) .append( "/images/footer-rightcurve.gif\" width=\"10\" height=\"31\" border=\"0\"/></td>") .toString()); out.println("</tr>"); out.println("</table>"); out.println("</div>"); out.println("</div>"); out.println("</body>"); out.println("</html>"); out.close(); } }