Archive for October, 2006

Page 34

Tuesday, October 31st, 2006

Page 34

Hint: This post is supported by Gama hrvatski web hosting services

Page 33

Tuesday, October 31st, 2006

Page 33
Note: If you are looking for best hosting provider to host and run your tomcat application check Astra tomcat hosting services

Page 32

Tuesday, October 31st, 2006

Page 32
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp hosting services

Page 31

Tuesday, October 31st, 2006

Page 31

Hint: If you are looking for very good and affordable webspace to host and run your tomcat hosting application check Virtualwebstudio tomcat web hosting provider

[ Team LiB ] 2.6 Java News Today

Monday, October 30th, 2006

[ Team LiB ] 2.6 Java News Today Java News Today, our fictional news site, is ready to begin constructing its site. JNT has decided to start with the new home page and for the moment will not worry about the dynamic elements. The first version is shown in Listing 2.4. Listing 2.4 The JNT index page <%@ page errorPage="error.jsp" %>

<%-- Big Empty Corner --%> <%-- Little buffer for the curvy bit --%> <%-- start header --%>

Java News Today: Welcome!

<%-- end header --%>
<%-- The curvy bit --%>
<%-- start navigation --%> Navigation - none yet <%-- end navigation --%> Page 30
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

[ Team LiB ] 2.5 Creating Custom Error

Monday, October 30th, 2006

[ Team LiB ] 2.5 Creating Custom Error Pages Many other directives are available for issuing instructions to the page translator. One of the most useful is called, not surprisingly, page. This directive takes a number of forms, many of which will be encountered throughout this blog as needed. One immediately useful option allows a JSP to specify where the user should be directed in the event of an error. Tomcat’s default error page, as already shown in Figure 2.1, can be useful to developers but more than a little scary to end users. Ideally, users will never see an error page, but a good site plans for all contingencies and so should include an error page that fits visually with the rest of the site and that allows the user to continue what he or she was doing, to whatever extent possible. Using a custom error page involves two steps. The first is to create the page, which can be another JSP. However, error pages need to be treated differently from regular pages, and so the page translator must be notified that a JSP will be used as an error page by use of the following page directive at the very top of the file: <%@ page isErrorPage="true" %> Once such an error page has been created and properly identified, it can be used in one of two ways. The first is globally, by telling Tomcat which error page to use for each type of error. This is done through a configuration file and is shown in Appendix B. In addition to the global approach, each JSP can specify its own error page through another variation of the page directive, as in <%@ page errorPage="error_page_url" %> Here, error_page_url is the URL of the error page, relative to the current page. Both of these versions of the page directive will be demonstrated in the next section. [ Team LiB ] Page 29

Hint: This post is supported by Gama besplatan domen provider

[ Team LiB ] 2.4 The Phases of

Monday, October 30th, 2006

[ Team LiB ] 2.4 The Phases of a JSP Each JSP goes through two distinct phases. The first phase, when the translator turns the file into a servlet, is called translation time. This translation from JSP to servlet happens whenever the JSP file is modified. The second phase, when the resulting servlet is run to generate the page, is called request time. This happens whenever a browser requests the page. Different things happen in each phase, and the distinction is important. The handling of JSP comment tags happens at translation time. The translator simply omits any text within comment tags, so the servlet will not even need to deal with it. Conversely, the jsp:include tag is handled at request time. For every request that comes in for the index.jsp URL, the content.jsp servlet will be run when the jsp:include tag is encountered. In principle, this could have been done at translation time; the chunk of HTML from the content.jsp file could have been dropped right into the index.jsp servlet. This would be much less powerful, however. By processing includes at request time, the contents of the included file can change independently of the main file. Another advantage to processing includes at request time is that doing so ensures that no JSP is fundamentally special or different. Every JSP is a little program that can be run by requesting the corresponding URL through a browser; there is no distinction between “top-level” and “included” JSPs. As mentioned, this means that a browser can directly request an included file, such as content.jsp, which is often useful when testing page components. This also guarantees that all JSP elements will work the same way in all pages. This means that JSP comments will be stripped out of included files and that the jsp:include tag will work inside included files! Files can include files that can include files, and so on, potentially to infinity. For the sake of completeness, it is worth mentioning the translation-time version of the jsp:include tag, called the include directive. This tag looks like this: <%@include file="contents.jsp" %> This tag is called a directive because it directs the page compiler to take some action. In this case, when it sees the directive, the page compiler will embed the contents of the contents.jsp file directly into the servlet that it is building for index.jsp. Subsequently, if the contents.jsp file is edited, index.jsp will not change. Browsers will continue to get the old message until the page compiler is forced to rebuild the index.jsp servlet. Note that the include directive specifies what is to be included with file=, whereas the jsp:include tag specifies page=. This nicely encapsulates the differences between the two: The directive includes a file as it is building the servlet at request time, and the tag includes another page at request time. In some obscure instances, the include directive and the jsp:include tag are not equivalent. For the most part, though, the two are functionally identical, and as the jsp:include tag is more convenient, it will be used in preference to the directive throughout this blog. [ Team LiB ] Page 28

Note: If you are looking for good and affordable webspace to host and run your servlet application check Virtualwebstudio servlet hosting services

[ Team LiB ] Page 27

Monday, October 30th, 2006

[ Team LiB ] Page 27

Hint: If you are looking for very good and affordable webspace to host and run your j2ee hosting application check Virtualwebstudio j2ee web hosting services

[ Team LiB ] 2.3 Including Text in

Sunday, October 29th, 2006

[ Team LiB ] 2.3 Including Text in a JSP Removing text from a page is only slightly useful; it is much more exciting to consider ways in which a JSP can add data to a page. This data may come from any number of places, such as a database, some Java code, or data explicitly provided by the user. Regardless of the source, it will be the JSP’s job to inject this data into the page. The first and simplest place a JSP can get data from is another JSP. Listing 2.2 shows a slightly modified version of Listing 2.1. Listing 2.2 A JSP that includes another JSP Hello again, world! This turns into a program just as Listing 2.1 did, and once again, all the HTML tags turn into instructions that send those tags to the browser. The jsp:include tag turns into an instruction for the JSP engine to run the program called content.jsp, which is shown in Listing 2.3. Listing 2.3 The included content This is some text from content.jsp. Note that Listing 2.3 contains a complete and valid JSP. A browser could request content.jsp directly, and the response would be the message with no HTML or other tags. However, the intended use is that the browser will request the top-level page, which will render its content, and then the jsp:include tag will call content.jsp. Control will then return to the original page, which will send the final closing body and HTML tags. The result, as far as the browser is concerned, will look exactly as if all the HTML was in the original page all along. Errors to Watch For Includes can suffer from the same kinds of errors as comments. First, tags can be broken, such as , which is missing the closing slash. The JSP engine will catch this error and report it as /index.jsp(3,0) Expected “param” tag with “name” and”value” attributes without the “params” tag. It is also possible to attempt to include a file that does not exist, which will usually happen because of a typo, such as typing headers.jsp instead of header.jsp. In this case, the JSP engine will report “Can’t read file headers.jsp,” which is easily fixed. Two files do not make for a very interesting site, but the jsp:include tag becomes much more useful when there are many more files. In one common scenario, many files may all want to include some common text. For example, every page on a site might have at the bottom a clever or amusing quote that the site administrators change once a week. If this quote is kept in its own JSP, it is necessary to change only that one file in order to change the whole site. Conversely, one file may want to include several others. A customized news site might have separate JSPs for top headlines, technology stories, weather, and sports. Many different Page 26

Hint: This post is supported by Gama web hosting hrvatska services

[ Team LiB ] Page 25

Sunday, October 29th, 2006

[ Team LiB ] Page 25
Note: If you are looking for good and high quality web space to host and run your java application check Vision java hosting services