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 .. 85 86 87 88 89 90 < 91 > 92 93 94 95 96 97 .. 166 >> Next

14 String pageNumber = req.getParameter("page");
15 if (pageNumber == null)
16 pageNumber = "Page1";
17 else {
18 if (direction.equals("next")) {
Listing 28.1 ControllerServletjava
Line 19 in Listing 28.1 illustrates how J2SDK 1.4 regular expressions can be used to parse string variables. The code parses the first four characters of the page request variable with \\w{4} and extracts the fifth character and, depending on the direction parameter, increases or decreases that value and resets the page mapping request parameter with the new value.
19 int iPage = 2
Integer parseInt(pageNumber.replaceAll('^(\\w{4})(\\d{1})", "$2")) + 1;
20 if (pageNumber.equals("Page4"))
21 pageNumber = "Page1";
22 else
23 pageNumber = "Page" + String.valueOf(iPage);
24 }
25 else {
26 int iPage =
Integer parseInt(pageNumber.replaceAll('^(\\w{4})(\\d{1})", "$2")) - 1;
27 if (pageNumber.equals("Page1"))
28 pageNumber = "Page4";
29 else
30 pageNumber = "Page" + String.valueOf(iPage);
31 }
32 }
33 // forward page request: Page1, Page2, Page3, Page4
34 // works too
35 // RequestDispatcher rd =
36 // req.getRequestDispatcher("/test2/jsp/second.jsp")
37 // rd.forward(req, res);
Listing 28.1 (continued)
Problems with Filters 247
38 // works too
39 // String contextPath = req.getContextPath();
40 // res.sendRedirect( contextPath + "/test2/jsp/second.jsp" );
41 getServletConfig().getServletContext().
42 getNamedDispatcher(pageNumber).forward(req, res);
43 }
44
45 }
46
Listing 28.1 (continued)
On line 35 of Listing 28.1 the getRequestDispatcher method gets a Request-Dispatcher instance from its request object so that it can dispatch the JSP component to the given URI path "/test2/jsp/second.jsp". In other words, this method takes a user-specified path that is relative to the servlet's root context and wraps it with a RequestDispatcher object to forward a user request. The relative pathname cannot extend outside the current servlet context, so if the path begins with a "/'', it is interpreted as relative to the current context root. The forward method of the RequestDispatcher interface emulates the JSP directive <jsp:forward>, which will throw the IllegalStateException if the application's response buffer has data that has not been committed to the user requests. The forward operation delegates all processing of the request to the target application. With filters, applications can now perform preprocessing prior to the forward operation, which includes logging, security, and request header modifications.
The migration of JSP, filter, and servlet configuration mappings to the deployment descriptor allows the getNamedDispatcher method on line 41 to map the appropriate JSP page in the pageNumber variable so that the request can be forwarded properly. The web.xml file in Listing 28.6 specifies all the JSP pages (Page1, Page2, Page3, Page4) that are available for invocation.
An important result of moving the business logic into the servlet controller is that scriptlet code is reduced in the JSP user interface components and maintenance is facilitated.
01
02 <form method="post">
03
04 [ content goes here. ]
05
06 <input type="hidden" name="page" value="Page1">
07 <input type="submit" name="direction" value="prev">
08 <input type="submit" name="direction" value="next">
09
10
11 </form>
Listing 28.2 This is a sample test page test1.jsp
248 Item 28
The FileServlet application in Listing 28.3 is invoked after the filter applications are processed. In our sample application the FileServlet application processes the filename parameter in the URL and displays the content data in HTML and XML format based on that XSL stylesheet filename and the XML filename affiliated with that request. Figure 28.2 shows the HTML presentation based on the filename htmlStates.xsl being applied to the states.xml file. The filename (states.xml) that has the user-specified XSL stylesheet applied to it is specified in line 31 of the web.xml file, which is a lot easier to maintain and modify than the property file (controller.proper-ties) shown on lines 19 to 22.
01 package org.javapitfalls.item28;
02
03 import java.net.URL;
04 import java.io.*;
05 import java.util.*;
06 import javax.servlet.*;
07 import javax.servlet.http.*;
08
09 public class FileServlet extends HttpServlet {
10
11 private String filename = "";
12
13 public void doGet (HttpServletRequest req,
14 HttpServletResponse res)
15 throws ServletException, IOException {
16 PrintWriter out = res.getWriter();
17 try {
18 /*
19 Properties resource = new Properties();
20 URL url = this.getClass().getClassLoader().
21 getResource("controller.properties");
22 resource.load( new FileInputStream(url.getFile()) );
23 */
24 // use filename from init() method
25 // File file = new File(getServletContext().
26 // getRealPath(resource.getProperty("filename")));
27 File file = new
Previous << 1 .. 85 86 87 88 89 90 < 91 > 92 93 94 95 96 97 .. 166 >> Next