Home  >  Article  >  Java  >  Struts User and Development Guide (Preface 2)

Struts User and Development Guide (Preface 2)

黄舟
黄舟Original
2016-12-17 10:55:141059browse

 0.6 Property (PRoperties) files and resource bindings (Resourse Bundles)
 Many java applications (including web applications) often perform some configuration through property files. Property files are the basis for the Struts framework to provide message resource resource binding for applications.
  
 For more information about property files, please refer to:
  
  Using Properties to Manage Program Attributes in The Java Tutorial
  
 Java resource binding provides users with internationalization through one or more properties files based on the user locale (Locale) support. Struts has had good support for application localization since its inception.
  
  For more about localization and resource binding, please refer to:
  
  About the ResourceBundle Class in The Java Tutorial
 
 0.7 Java Servlet
 Since Java is an object-oriented programming language, the Java Servlet platform Forcing HTTP into an object-oriented form. This strategy allows Java developers to save more time to deal with the functions of their own applications instead of dealing with the HTTP mechanism.
 
 HTTP provides a basic mechanism for extending the server, namely the Common Gateway Interface (CGI). The server can pass a request to the CGI program, and the CGI program returns a response. Similarly, a Java server passes a request to a Servlet container. The container can do some processing on the request, or it can return the request directly to the HTTP server. The container checks its Servlet list to decide whether to process the request. If the request After registering a Servlert, the container will transfer the request to the Servlet.
 
 When a request comes in, the container checks whether the request has a registered Servlet. If a matching Servlet is found, the container passes the request to the Servlet. If not, the request is returned to the HTTP server.
 
 The responsibility of the container is to manage the life cycle of the Servlet, create the Servlet, call the Servlet, and finally release the Servlet.
 
  Generally, a Servlet is a subclass of [javax.servlet.http.HttpServlet]. A Servlet must implement four methods that the container needs to call:
 
 .public void init(ServletConfig config): When the Servlet instance is first The Servlet container calls this method when it is created and before executing all requests;
 .public void doGet(HttpServletRequest request, HttpServletResponse response) This method is used to process a request using the HTTP GET protocol and generate a corresponding dynamic response;
 . public void doPost(HttpServletRequest request HttpServletResponse response) This method is used to process a request using the HTTP POST protocol and generate a corresponding dynamic response;
 .public void destroy() The container calls this method when the Servlet instance terminates the service, such as when When the web application is being undeployed or when the entire container is shut down;
  
  The Struts framework has provided a ready-made Servlet [org.apache.struts.action.ActionServlet] for our application. As a Struts application developer, while using the ActionServlet instance of the Struts framework, it is also important to understand the basics of Servlet and understand the role it plays in Web applications.
 
 For more knowledge about Servlets, please refer to:
 
 The Java Servlet Technology in .java.sun.com;
 The Servlet 2.2 and 2.3 Specifications in java.sun.com;
 .The Java Web Service Tutorial Java Servlet Technology;
  .The Java Web Service Tutorial's Web applications;
  
 0.7.1 Servlet and Thread
  
  To improve performance, the container supports multi-threaded Servlets. A specific Servlet can only create one instance, and serve each request registered with this Servlet through the same object. This strategy allows the container to make full use of system resources. At the same time, the thread safety issues of Servlet's doGet and doPost method coding must be considered.
 
 For more information about Servlets and thread safety, please refer to:
 
 .Controlling Concurrent access to Shared Resources in The Java Web Service Tutorialhttp://java.sun.com/webservices/docs/1.0/tutorial/doc/ Servlets5.Html#64386;
 
 0.7.2 Servlet context (Context)
 
 The ServletContext interface [javax.servlet.ServletContext] provides a view of the context (or environment) of the Web application in which the running Servlet is located. Servlets can be accessed through the getServletConfig() method, and jsp pages can be obtained through the implicit variable application variable. The Servlet context provides several APIs that are quite useful when creating Struts Web applications.
  
  Access Web application resources: Servlet can access static resource files within the Web application through the getResource() and getResourceAsStream() methods;
.Servlet context attributes: Context can be used to store Java objects and identify objects through string value keys. These attributes are global to the entire Web application. Servlet can use getAttribute(), getAttributeNames(), removeAtrribute() and setAttribute() method to access. For JSP pages, Servlet context attributes are equivalent to "application scope beans";
  
 For more information about Servlet context, please refer to:
  
 Accessing the Web Context in The Java Web Services Tutorial http:// java.sun.com/webservices/docs/1.0/tutorial/doc/Servlets10.html#64724;
  
 0.7.3 Servlet request
 
  Each request processed by a Servlet is represented by a Java interface, usually the HttpServletRequest interface [javax. servlet.http.HttpServletRequest]. This request interface provides a set of object-oriented mechanisms for accessing all information contained in the underlying HTTP request, including:
 
 .Cookie: Obtain the valid cookie set contained in the request through the getCookie() method;
 .Header: Can be passed Name access HTTP header included in the request. You can enumerate the names of all HTTP headers included;
 . Parameters: Request parameters, which can be accessed by name. Request parameters contained in the query string of the URL (doGet) or included in the request content (doPost);
 . Request characteristics: Enter some other characteristics of the HTTP request, such as the protocol specification ("http" or "https") used by the GET or POST method, etc.;
 Request URI information: The original request URL can be obtained through the getRequestURI() method . In addition, the Servlet container parses the request URL into some components that can be accessed individually (contextPath, servletPath and pathInfo);
 User information: If you use user-managed security, then you can search for an authenticated user name and obtain A Principal object representing the current user, and whether the current user is authorized to a specific role; In addition, Servlet requests also support request attributes (in JSP, request scope beans), similar to the Servlet context mentioned earlier Attributes. Request attributes are often used to communicate state information between the business logic layer and the view layer. The business logic layer generates these state information, and the view layer uses this information to generate corresponding responses.
 
 The Servlet container will ensure that a specific request is processed by a Servlet in a separate thread, so you don't have to worry about thread safety issues when accessing properties requested by the Servlet.
 
 For more information about Servlet requests, please refer to:
 
 .Getting Information from Requests in The Java Web Tutorialhttp://java.sun.com/webservices/docs/1.0/tutorial/doc/Servlets7.html#64433;
 
 0.7.4 Servlet response
 
 The main purpose of a Servlet is to process an input Servlet request [javax.servlet.http.HttpServletRequest] and generate a corresponding response. The process of generating a response is completed by calling the corresponding method of the Servlet response interface [javax.servlet.http.HttpServletResponse]. The available methods are as follows:
  
 . Set HTTP header: You can set the HTTP header information included in the response. The most important HTTP header information is Content-Type, which tells your client what type of information is included in the response body. Generally, setting it to text/html type means an HTML page, or setting it to text/xml type. It is an XML document;
.Set Cookies: You can add cookies to the current response;
.Send an error response: You can use sendErro() to send an HTTP error status message (instead of normal page content);
.Redirect to Other resources: You can use the sendRedirect() method to redirect the client to other URL resources you specify;
  
An important principle of using the Servlet response API is that all methods called to maintain header information and Cookies must be in the entire cached response content. Completed before the first update is given to the client. The reason is that this information is transmitted as the first part of the HTTP response, so trying to add header information after the header information has been sent is necessarily futile.
 
Using the presentation layer of a Model 2 application, you may not directly use the Servlet response APIs to generate responses. This is usually done using JSP pages. In the Servlet container, the JSP page will be converted into a Servlet by the JSP compiler. This JSP Servlet will generate a response, which may contain some dynamic information generated by JSP tags.
 
 Other presentation systems, such as the Struts tool Velocity framework, may delegate the task of generating responses to a specialized Servlet, but the principle is the same. You create a template, and dynamic responses are dynamically generated from the template.
 
 For more about Servlet responses, please refer to:
 
 .ConstrUCting Responses in The Java Web Tutorialhttp://java.sun.com/webservices/docs/1.0/tutorial/doc/Servlets7.html#64531;
 
0.7.5 Filtering
 
If you use a Servlet container with version 2.3 or newer specification (such as Tomcat 4.x), you can use the new filter APIs [javax.servlet.Filter] to combine some components to handle requests and generate responses. A filter is actually a collection of filter chains. Each filter can process the request and generate a response, then transfer the processing power to the next filter, and finally call the Servlet.
 
  Struts 1.x series (version 1.0, 1.1, etc.) only supports Servlet containers of version 2.2 or earlier Servlet specification, so Struts itself does not use filters. The next generation of Struts (2.x series) is based on the Servlet2.3 or later specification. Struts version 2.x may use filters.
 
 For more about filters, please refer to:
 
 .Filtering Requests and Responseshttp://java.sun.com/webservices/docs/1.0/tutorial/doc/Servlets8.html#64572;
 
 0.7.6 Session ( session

The above is the content of the Struts User and Development Guide (Part 2). For more related articles, please pay attention to the PHP Chinese website (www.php.cn)


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn