Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 110 111 112 113 114 115 < 116 > 117 118 119 120 121 122 .. 166 >> Next

Listing 36.3 RedirectServlet.java
Too Many Submits 319
07
08 public class RedirectServlet extends HttpServlet {
09
10 static int count = 2;
11
12 public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
13
14 HttpSession session = req.getSession(false);
15 System.out.println("");
16 System.out.println("---------------------------------------");
17 System.out.println("SessionServlet::doGet");
18 System.out.println("Session requested ID in request: " +
req.getRequestedSessionId());
19
20 if ( null == req.getRequestedSessionId() ) {
21 System.out.println("No session ID, first call, creating new 2
session and forwarding");
22 session = req.getSession(true);
23 System.out.println("Generated session ID in Request: " + 2
session.getId());
24 String encodedURL = res.encodeURL("/resubmit/TestServlet");
25 System.out.println("res.encodeURL(\"/TestServlet\";=" + encodedURL);
26 res.sendRedirect(encodedURL);
27 //
28 // RequestDispatcher rd = getServletContext() 2
.getRequestDispatcher(encodedURL);
29 // rd.forward(req, res);
30 //
31 return;
32 }
33 else {
34 System.out.println("Session id = " + req.getRequestedSessionId() );
35 System.out.println("--------------------------------------------- ");
36 }
37
38 HandleRequest(req, res);
39 System.out.println("SessionServlet::doGet returning");
40 System.out.println("-------------------------------");
41 return;
42 }
43
44 void HandleRequest(HttpServletRequest req, HttpServletResponse 2
res) throws IOException {
45
46 System.out.println("SessionServlet::HandleRequest called");
47 res.setContentType("text/html");
48 PrintWriter out = res.getWriter();
Listing 36.3 (continued)
320 Item 36
49 Date date = new Date();
50 n( iH t n i r ft t. u o >" ii m t h <
51 n( - t n i r ft t. u o >" d a e h </ > e ii t i t </ n 0 1 t a m r i LM n 0 C t e k c 1 T > e 11 t i t < > d a e h <
52 // javascript
53 n( iH t n i r .p t. u o <script language=\"javascript\">");
54 out.println(
55 n( iH t n i r .p t. u o var submitcount = " + count + ";");
56 out.println( function checksubmitcount()");
57 out.println( '(");
58 out.println( 1 if ( 2 == submitcount )");
59 out.println(' alert(\"You have already submitted this form.\"); );
60 out.println( " else");
61 out.println(" alert(\"You have submitted this form " + count + 2
" times already.\") ;");
62 out.println( 1 document.Order.submit();");
63 out.println( '}");
64 out.println( '//-->");
65 n( iH t n i r .p t. u o >" t p i r c s </
66 // body
67 n( - t n i r .p t. u o < 0 dy> V
68 n( - t n i r .p t. u o '<center>");
69 n( r-H t n i r .p t. u o '<form name=\"Order\" action=\"./RedirectServlet\" 2
method=\"GET\">");
70 n( r-H t n i r .p t. u o "<table border=\"2\" width=\"50%\" align=\"center\" 2
bgcolor=\"#cccccc\ >");
71 n( r-H t n i r .p t. u o >" r t <
72 n( r-H t n i r .p t. u o "<td align\"right\" width=\"40%\">Concert:</td>");
73 n( r-H t n i r .p t. u o "<td width=\"60%\"><input type=\"text\" 2
name=\"Concert\" value=\"\"></td>");
74 n( r-H t n i r .p t. u o >" r t </
75 n( r-H t n i r .p t. u o >" r t <
76 n( r-H t n i r .p t. u o "<td colspan=\"2\" align=\"center\">");
77 n( r-H t n i r .p t. u o <input type=\"button\" name=\"btnSubmit\" value=\"Do 2
Submit\" onClick=\' 0 h (D 0 tv w u bmi H- ft 0 0 u n ft >"
78 n( r-H t n i r .p t. u o >" d t </
79 n( r-H t n i r .p t. u o >" r t </
80 n( r-H t n i r .p t. u o >" m r o f </
81 // message
82 n( r-H t n i r .p t. u o >" r b <
83 n( r-H t n i r .p t. u o "<h1>The Current Date and Time Is:</h1><br>");
84 n( r-H t n i r .p t. u o "<h3>You have submitted this page before</h3><br>"),
85 n( r-H t n i r .p t. u o "<h3>" + date.toString() + "</h3>");
86 n( r-H t n i r .p t. u o < b 0 dy> V
87 n( r-H t n i r .p t. u o >" l m t h </
88
89 count++;
90
Listing 36.3 (continued)
Too Many Submits 321
91: System.out.println("SessionServlet::HandleRequest returning.");
92: return;
93: }
94:
95: }
Listing 36.3 (continued)
Just how does Listing 36.3 solve our problem? If we examine the code closely, we see that on line 14 we try to obtain a handle to a session. On line 20 we identify that an active session exists by comparing the session to null or by checking for a valid session ID. Either method suffices. Lines 20 to 31 are executed if no session exists, and to handle our problem, we:
1. Create a session, as shown on line 22.
2. Use URL encoding to add the new session ID to our URL, as shown on line 24.
3. Redirect our servlet to the newly encoded URL, as shown on line 26.
Those readers unfamiliar with URL rewriting are directed to lines 18 and 25. The request parameter to an HttpServlet can do what is known as URL rewriting. URL rewriting is the process whereby a session ID is automatically inserted into a URL. The underlying application server can then use the encoded URL to provide an existing session automatically to a servlet or JSP. Note that depending on the application server, you may need to enable URL rewriting for the above example to work.
Previous << 1 .. 110 111 112 113 114 115 < 116 > 117 118 119 120 121 122 .. 166 >> Next