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 .. 89 90 91 92 93 94 < 95 > 96 97 98 99 100 101 .. 166 >> Next

02 <%@page import="java.net.*" %>
03
04 <title>Fruit Stand</title>
05
06 <jsp:include page="header.jsp" flush="true" />
07
08 <table border="0" width="100%">
09 <tr valign="top">
10 <td width="25%" align="center" valign="top">
11 <jsp:include page="leftnav.jsp" flush="true" />
12 </td>
13 <td width=75%" align="center" valign="top">
14 <%
15 String sMode = request.getParameter("mode" );
16 String sTopicID = request.getParameter("topic");
17 %>
18
19 <table width="100%" border="0" cellspacing=" 0" cellpadding="0"
20 valign="top">
21 <tr>
22 <td bgcolor="#ffffff" align="center" class= "portletBoxTitle">
23 <b><font face="Verdana" size="2" color="#FFFFFF"> 2
INFORMATION</font></b>
24 </td>
25 </tr>
26 <tr>
27 <td bgcolor="#ffffff" align="center" class= "portletBoxBody">
28 <font face="Verdana" size="2"><br>
29 <%
30 StringBuffer strbuf = new StringBuffer();
31 strbuf.append("/default/" + sTopicID);
32 strbuf.append("/index.html");
33
34 String s = strbuf.toString();
35
36 // Obtain the custom view context
37 ServletContext scTemp = getServletConfig(). getServletContext();
38 ServletContext scHome = getServletConfig() .getServletContext();
39 ServletContext scRemote = scTemp.getContext("/newstuff");
40
Listing 29.1 home.jsp (continued)
258 Item 29
41 // Create the absolute url for c:import tag to use
42 String sURL = request.getScheme() + "://" +
43 request.getServerName() + ":" + request.getServerPort() +
44 "/newstuff/" + s;
45 URL urlRsrcFile = null;
46
47 try {
48 // Try to obtain resource from remote context
49 urlRsrcFile = scRemote.getResource((String)s);
50 } catch (Exception e) {
51 scTemp.log("\n\nError: home.jsp - error occured when trying 2
to obtain a resource from the ’REMOTE' context. Please verify that 2
the server is configured to allow cross-context access.\n\n", e);
52 }
53
54 if (urlRsrcFile != null) {
55 %>
56 <c:import url="<%= sURL %>" />
57 <%--
58 This does not work.
59 <jsp:include page="<%= sURL %>" />
60 --%>
61 <%
62 }
63 %>
64
65 </font>
66 </td>
67 </tr>
68 </table>
69
70 </td>
71 </tr>
72 </table>
73
74 <jsp:include page="footer.jsp" />
Listing 29.1 (continued)
All appears to be okay until you attempt to add the content using the include directive as shown on line 59 from the [newstuff] context. Why? Apparently, JSP include operations add resources that are relative to their context, and since the application is being run from the [customviews] context, it looks there for the resource inclusion. To rectify this problem, we'll need to use the Java Standard Template Library (JSTL) implementation of the import tag because it allows for the retrieval of absolute URLs, which we'll need to obtain data from the [newstuff] context. Additionally, an
Some Direction about JSP Reuse and Content Delivery 259
entry has to be made to set the context path for your remote inclusion. With Tomcat, this means a context path inclusion in the server.xml file:
<Context path="/newstuff" docBase="newstuff" debug=M0" crossContext="true"/>
which will allow the customviews application to access the newstuff pages.
In the home.jsp script, the import tag library is used on line 56 to import a resource from a directory that lies outside the context of the current application [customviews]. With the import tag, a simple drop-off mechanism like a WebDAV client can be used to drop content into the [newstuff] location outside of your deployment area in [customviews]. This simple content management process will enable you to work uninterrupted, and without concern that someone outside of your office will corrupt your development platform.
Lastly, we'll look at the forward tag and its implementation in our application. In Listing 29.2, the forward action <jsp:forward...> on line 12 passes the request to the help.jsp page. Normally, if a response has already been partially sent to the browser, the forwarding operation will throw an IllegalStateException error. This could occur if a user specifies a page buffer equal to "none" , as shown on line 02. A buffer size is generally set to "none" on Web pages that are slow and to which the developer wants to feed the content as it comes in, rather than waiting for it to reach its buffer maximum or for the page to load to render data. This error can be captured by inserting a try/catch exception handler like the one shown in lines 10 to 17 below. When the page buffer directive is omitted, a default value of 8 KB is allocated and the request will be forwarded properly.
01 <%--
02 <%@ page buffer="none" %> // bad idea. throws an
IllegalStateException error.
03 --%>
04
05 <%
06 String mode = request.getParameter("mode");
07 if (mode==null) mode="";
08
09 if (mode.equals("forward")) {
10 try {
11 %>
12 <jsp:forward page="help.jsp" >
13 <jsp:param name="title" value="Help Screen Topic passed from 2
Previous << 1 .. 89 90 91 92 93 94 < 95 > 96 97 98 99 100 101 .. 166 >> Next