1. Trang chủ
  2. » Công Nghệ Thông Tin

OReilly java servlet and JSP cookbook jan 2004 ISBN 0596005725

886 123 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Chapter 14. Logging Messages from Servlets and JSPs

  • Chapter 19. Filtering Requests and Responses

  • Chapter 20. Managing Email in Servlets and JSPs

  • Chapter 1. Writing Servlets and JSPs

  • Chapter 7. Handling Web Form Data inServlets and JSPs

  • Chapter 6. Dynamically Including Contentin Servlets and JSPs

  • Chapter 9. Handling Exceptions in Web Applications

  • Chapter 23. Using the JSTL

  • Chapter 15. Authenticating Clients

  • Chapter 22. Using Custom Tag Libraries

  • Chapter 11. Session Tracking

  • Chapter 10. Reading and Setting Cookies

  • Chapter 16. Binding, Accessing, and Removing Attributes in Web Applications

  • Chapter 25. Using JNDI and Enterprise JavaBeans

  • Chapter 2. Deploying Servlets and JSPs

  • Chapter 3. Naming Your Servlets

  • Chapter 5. Altering the Format of JSPs

  • Chapter 4. Using Apache Ant

  • Chapter 24. Internationalization

  • Chapter 21. Accessing Databases

  • Chapter 18. Working With the Client Request

Nội dung

Recipe 19.2 Mapping a Filter to a JSP Problem You want to have the web container apply a filter to requests for a certain JSP page Solution Use the url-pattern child element of the filter-mapping element in the deployment descriptor to map the filter to the JSP Discussion Map a filter to a JSP by specifying the path to the JSP page using the filter-mapping element's url-pattern subelement Example 19-3 shows a web.xml configuration that maps the filter in Example 19-2 to the requestHeaders.jsp Example 19-3 Mapping a filter to a JSP LogFilter com.jspservletcookbook.LogFilter LogFilter *.jsp The URL pattern *.jsp is an extension mapping that associates the LogFilter with any of the web application's components that end with jsp See Also Recipe 7.9 on using a filter to read request parameter values; Recipe 11.11 on using a filter to monitor session attributes; Recipe 18.3 on using a filter to alter then forward the request; Recipe 19.3 on mapping more than one filter to a servlet; Recipe 19.4 on changing the order filters are applied to a servlet; Recipe 19.5 on configuring filter initialization parameters; Recipe 19.6 on blocking requests; Recipe 19.7 on filtering the HttpServletResponse; Recipe 19.8 on using filters with RequestDispatchers; Recipe 19.9 on using filters to check request parameters; Recipe 19.10 on using filters to disallow requests from certain IP addresses Recipe 19.1 Mapping a Filter to a Servlet Problem You want to map or apply a filter to an individual servlet Solution Use the filter and filter-mapping elements in web.xml to associate the filter with the servlet Discussion The web container finds out about the filters that you want to apply to a servlet by using information in the deployment descriptor The filter element associates a filter name with a Java class that implements the javax.servlet.Filter interface The filter-mapping element then associates individual filters with URL mappings or paths, similar to the servlet-mapping element that you have probably used before in web.xml Example 19-1 shows a deployment descriptor from the servlet API v2.3 that includes the mapping of a filter named LogFilter to the servlet path /requestheaders Example 19-1 Mapping a filter to a servlet Recipe 5.5 shows what these files look like The JSP specification describes an XML view as a description of a JSP page in XML form An XML view is generated by the JSP container during the translation phase A subclass of javax.servlet.jsp.tagext.TagLibraryValidator can use the XML view to parse a JSP in order to validate that custom tags have been used correctly, before the container finally converts the JSP into its page implementation class (a servlet) Recipe 5.6 shows how to generate XML views for a JSP, and how to save the resulting XML files JSPs can be created as XML files for the following reasons, among others: Web containers can accept JSP documents in web applications, meaning that the web application can contain XML files instead of the pages in traditional JSP syntax JSP documents can thus be integrated with other XML content, such as XHTML files, Scalable Vector Graphics (SVG), and the XML files that are part of web services transactions You can use XML editors to work with JSP documents You can use other XML technologies with JSP documents, such as XSLT, Simple Object Access Protocol (SOAP), SAX, and DOM Recipe 5.2 Precompiling a JSP in WebLogic Problem You want to precompile a JSP in WebLogic Solution Use the weblogic.jspc Java utility that installs with WebLogic Server 7.0 Discussion WebLogic Server 7.0 installs with its own Java utility for precompiling JSPs: weblogic.jspc This utility is part of the JAR file that can be found at this location: /weblogic700/server/lib/weblogic.jar When you precompile JSPs using weblogic.jspc, it places the class files in the specified destination directory Example 5-4 shows a simple batch file on Windows NT that precompiles an example.jsp JSP page into its servlet implementation class Example 5-4 Precompiling a JSP with weblogic.jspc @echo off set WLCLASSPATH=k:\bea\weblogic700\server\lib\weblogic.jar;%CLA java -cp %WLCLASSPATH% weblogic.jspc -d \classes example.jsp The second line of Example 5-4 sets an environment variable, WLCLASSPATH This variable prepends a reference to weblogic.jar to the existing CLASSPATH variable The next line of the example uses this combined classpath to run weblogic.jspc The -d switch tells the program where to store the resulting class files, in this case, in the classes directory beneath the directory containing the batch file and example.jsp This program generates a Java class file named jsp_servlet._ _example.class (including the package name) If you do not specify a package for the compiled servlet, jsp_servlet is used as the default package name (see Example 5-6) Example 5-5 shows a shell script that is written on Mac OS X for precompiling a JSP with WebLogic Example 5-5 Precompiling JSPs with weblogic.jspc and a shell script #!/bin/sh WLCLASSPATH=/Users/bruceper/java/weblogic_jar/weblogic.jar:$CLA export WLCLASSPATH; java -cp $WLCLASSPATH weblogic.jspc -d /Users/bruceper/books/cookbook/code/chap5/classes newfile weblogic.jspc is different from Tomcat's JspC utility in that it compiles a file in JSP page syntax into the servlet class file in a single operation Using Tomcat's JspC from the command line requires the use of a compiler, such as javac, to compile the java files generated by JspC into class files This second compilation step when using JspC is handled automatically when using weblogic.jspc The Windows batch file in Example 5-6 specifies a jspservletcookbook package for all the JSP pages found in the web application specified by the -webapp switch Example 5-6 Using weblogic.jspc to precompile all JSP pages in a web application @echo off set WLCLASSPATH=k:\bea\weblogic700\server\lib\weblogic.jar;%CLA java -cp %WLCLASSPATH% weblogic.jspc -d \classes -package jsps -webapp h:/home Example 5-7 shows a Unix shell script that does the same thing Example 5-7 Precompiling all JSP pages in a web application with a shell script #!/bin/sh WLCLASSPATH=/Users/bruceper/java/weblogic_jar/weblogic.jar:$CLA export WLCLASSPATH; java -cp $WLCLASSPATH weblogic.jspc -d /Users/bruceper/books/co -package jspservletcookbook -compileAll -webapp /Users/bruceper Note this portion of the instruction in the example: -compileAll -webapp h:/home The -compileAll switch, along with an argument to -webapp, tells weblogic.jspc to precompile all the JSP files found in the web application configured in the h:\home directory, including any JSP files nested in subdirectories This web application is in exploded directory format (not archived into a WAR file) In Example 5-6, the compiled classes are stored in the \classes\jspservletcookbook directory path See Also Recipe 5.3 on the precompilation protocol; Recipe 5.4 on mapping the compiled JSP(s) in web.xml; the JSP precompilation section of JavaServer Pages by Hans Bergsten (O'Reilly); Chapter JSP.11.4 of the JSP 2.0 specification Recipe 5.4 Mapping a JSP to Its Page Implementation Class Problem You have already precompiled a JSP and want to specify a mapping to the JSP page implementation class in your deployment descriptor Solution Cut and paste the servlet and servlet-mapping elements generated automatically by JspC into web.xml Create the proper package-related directories in the WEB-INF/classes directory of your web application, then place the precompiled JSPs into that directory Discussion Precompiling JSPs allows you to remove the JSP page syntax files from your web application and just use the resulting servlet class files You can then use the servlet-mapping element in web.xml to map a JSP-style URL (e.g., default.jsp) to the compiled servlet class Here is how to accomplish this task: Precompile the JSP(s) as described in Recipe 5.1 or Recipe 5.2, including the compilation of Java source files into class files using javac or another compiler tool Cut and paste the servlet and servlet-mapping elements generated automatically by JspC into your deployment descriptor (if you are using Tomcat), or add those elements manually to web.xml (if you are using WebLogic or another container) Make sure the servlet-mapping's url-pattern element points to a JSP-style filename, such as default.jsp, or an extension mapping such as *.jsp Place the class or classes, including the package-related directories, in WEB-INF/classes, or inside of a JAR file that is stored in WEB-INF/lib When the web users request the URL specified by the servletmapping for that JSP page implementation class, the web container will now direct that request to the mapped servlet class Example 5-8 shows a servlet configuration for a precompiled JSP Example 5-8 A web.xml entry for a precompiled JSP org.apache.jsp.precomp_jsp org.apache.jsp.precomp_jsp org.apache.jsp.precomp_jsp /precomp.jsp The directory structure for this class in your web application should be something like: /WEBINF/classes/org/apache/jsp/precomp_jsp.class If the context path for your web application is /home, users can request this JSP's implementation class (a servlet, behind the scenes) with a URL similar to http://localhost:8080/home/precomp.jsp See Also Recipe 5.1-Recipe 5.3; Chapter JSP.11.4 of the JSP 2.0 specification Chapter 4 Using Apache Ant Introduction Recipe 4.1 Obtaining and Setting Up Ant Recipe 4.2 Using Ant Targets Recipe 4.3 Including Tomcat JAR files in the Build File Classpath Recipe 4.4 Compiling a Servlet with an Ant Build File Recipe 4.5 Creating a WAR File with Ant Recipe 4.6 Creating a JAR File with Ant Recipe 4.7 Starting a Tomcat Application with Ant Recipe 4.8 Stopping a Tomcat Application with Ant Chapter 24 Internationalization Introduction Recipe 24.1 Detecting the Client Locale in a Servlet Recipe 24.2 Detecting the Client's Locales in a JSP Recipe 24.3 Creating a ResourceBundle as a Properties File Recipe 24.4 Creating a ResourceBundle as a Java Class Recipe 24.5 Using the ResourceBundle in a Servlet Recipe 24.6 Using the ResourceBundle in a JSP Recipe 24.7 Formatting Dates in a Servlet Recipe 24.8 Formatting Dates in a JSP Recipe 24.9 Formatting Currencies in a Servlet Recipe 24.10 Formatting Currencies in a JSP Recipe 24.11 Formatting Percentages in a Servlet Recipe 24.12 Formatting Percentages in a JSP Recipe 24.13 Setting the Localization Context in the Deployment Descriptor Chapter 21 Accessing Databases Introduction Recipe 21.1 Accessing a Database from a Servlet Without DataSource Recipe 21.2 Configuring a DataSource in Tomcat Recipe 21.3 Using a DataSource in a Servlet with Tomcat Recipe 21.4 Creating a DataSource on WebLogic Recipe 21.5 Using a JNDI Lookup to get a DataSource from WebLogic Recipe 21.6 Using a DataSource from WebLogic in a JSP Recipe 21.7 Calling a Stored Procedure from a Servlet Recipe 21.8 Calling a Stored Procedure from a JSP Recipe 21.9 Converting a ResultSet to a Result Object Recipe 21.10 Executing Several SQL Statements Within a Single Transaction Recipe 21.11 Using Transactions with JSPs Recipe 21.12 Finding Information about a ResultSet Chapter 18 Working With the Client Request Introduction Recipe 18.1 Examining HTTP Request Headers in a Servlet Recipe 18.2 Examining HTTP Request Headers in a JSP Recipe 18.3 Using a Filter to Alter Request Headers Recipe 18.4 Automatically Refreshing a Servlet Recipe 18.5 Automatically Refreshing a JSP Recipe 18.6 Counting the Number of Web Application Requests ... more filters or listener classes added here > MyServlet< /servlet- name> com.jspservletcookbook.MyServlet< /servlet- cl < /servlet> requestheaders< /servlet- name> com.jspservletcookbook.RequestHeaderView requestheaders< /servlet- name>... Snooping on parameter values with a servlet package com.jspservletcookbook; import javax .servlet. *; import javax .servlet. http.*; import java. util.Map; import java. util.Iterator; import java. util.Map.Entry; public class ParamSnoop implements Filter {

Ngày đăng: 26/03/2019, 16:31

TỪ KHÓA LIÊN QUAN