Servlet and JSP Technical Features

Source: Internet
Author: User

J2EE 1.4 provides the new Servlet 2.4 and JavaServer Pages (JSP) 2.0 technologies for developing Web applications using Java. This article demonstrates the new features of these two technologies and provides sample code for each feature where appropriate. This document assumes that you are familiar with the previous Servlet 2.3 and JSP 1.2 versions. The example is tested with Tomcat 5 included in Java Web Services Developer Pack 1.2.

Servlet and JSP are undoubtedly the two most widely used J2EE technologies. Servlet technology is the basis for Web application programming using Java and also the basis of JSP. However, servlet programming may be very troublesome. This is especially true when you have to send a long HTML page with little code. Each HTML Tag must be embedded in a string and sent as a PrintWriter object. It is a tedious and annoying job. Another disadvantage of using servlet is that each change requires the involvement of servlet programmers.

Sun developed JSP as a solution after learning about this problem. In JSP, the division of labor between programmers and page designers is much easier, and the Servlet and JSP pages are automatically compiled when they are changed. However, note that JSP is an extension of servlet technology, rather than discarding servlet. In practical applications, servlet and JSP pages are used together.

Servlet 2.4 provides several new classes and does not support the javax. servlet. SingleThreadModel interface. This version only supports HTTP 1.1, so Servlet 2.4 applications are not applicable to HTTP 1.0 client programs. Version 2.4 adds request listeners and request property listeners, and can use servlets as welcome pages in an application. In addition, Servlet 2.4 provides better ServletRequest and RequestDispatcher objects, and better supports internationalization. In addition, the document-type definition, DTD) file is defined based on the mode rather than the document type to verify whether the deployment descriptor is valid. This means that deployment descriptor scalability is supported.

The following describes the new features of Servlet 2.4. Request listener and request property listener. Added Servlet context-related listeners and session-related listeners in servlet 2.3. Servlet 2.4 adds the new javax. servlet. ServletRequestListener and javax. servlet. ServletRequestAttributeListener interfaces, which will notify you of the events related to the Request object. If you are interested in the initialization and revocation of each Request object, you can implement the ServletRequestListener interface. This interface has two methods: requestInitialized () and requestDestroyed (). When a Request object is required, the servlet container calls the requestInitialized method. When the Request object is no longer needed, the servlet container calls the requestDestroyed method.

Both methods receive a javax. servlet. ServletRequestEvent object from the servlet container. You can obtain servlet context and servlet requests from the ServletRequestEvent instance.

The second listener interface ServletRequestAttributeListener processes the addition, modification, and deletion of Request object attributes. This interface has the following methods:
AttributeAdded. The servlet container calls the Request object to add a new property.
AttributeRemoved. The servlet container calls the Request object to delete attributes.
AttributeReplaced. When the existing property values in the Request object are replaced, the servlet container calls the Request object.

These three methods obtain an instance of the javax. servlet. ServletRequestAttributeEvent class from the servlet container. The ServletRequestAttributeEvent class extends the ServletRequestEvent class and adds two new methods: getName and getValue. The getName method returns the name of the trigger event property, and the getValue method returns the property value.

Code List 1 shows the sample classes for these two new listeners. When the servlet container calls a method, both display the method name. After the listeners are compiled, their class files must be deployed under the WEB-INF/classes directory. The new method in ServletRequest. In Servlet 2.4, four new methods are added to the javax. servlet. ServletRequest interface:

GetRemotePort. Returns the Internet ProtocolIP of the client sending the request or the last proxy server.
GetLocalName. Returns the Host Name of the IP interface from which the request is received.
GetLocalAddr. Returns the IP address of the interface from which the request is received.
GetLocalPort. Returns the IP Port Number of the interface from which the request is received.

Note that in Servlet 2.3, the values returned by the getServerName and getServerPort methods are the values returned by the current getLocalName and getLocalPort methods. In version 2.4, getServerName and getServerPort have been redefined. For more information, see the API documentation.

Sample Code on a JSP page:

 
 
  1. out.println("<br>Remote Port : " +  
  2. request.getRemotePort());  
  3. out.println("<br>Local Name : " +  
  4. request.getLocalName());  
  5. out.println("<br>Local Addr : " +  
  6. request.getLocalAddr());  
  7. out.println("<br>Local Port : " +  
  8.  
  9. request.getLocalPort()); 

If a Request object is not passed, the values of these properties are null. On the other hand, these attributes will have non-null values in the resource of the passed object. These attribute values are useful when a resource must be called only through another resource but cannot be called directly.

For example, in a Context called myApp, there is a servlet named ModernServlet, and ModernServlet is passed to TargetServlet. In TargetServlet, the code in code list 2 is displayed.

The deployment descriptor of myApp contains the following elements:

 
 
  1. <servlet> 
  2. <servlet-name>Modern servlet-name> 
  3. <servlet-class>ModernServlet  
  4. servlet-class> 
  5. servlet> 
  6. <servlet-mapping> 
  7. <servlet-name>Modern servlet-name> 
  8. <url-pattern>/Modern url-pattern> 
  9. servlet-mapping> 
  10. <servlet> 
  11. <servlet-name>Target servlet-name> 
  12. <servlet-class>TargetServlet  
  13. servlet-class> 
  14.  
  15. servlet> 
  16. <servlet-mapping> 
  17. <servlet-name>Target servlet-name> 
  18. <url-pattern>/Target url-pattern> 
  19. servlet-mapping> 

Edit recommendations]

  1. Wizard for creating Servlet Filters
  2. Install Servlet and JSP development tools
  3. Extended Future Response Servlet
  4. Servlet Container matching process
  5. User cache Servlet

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.