search
HomeJavajavaTutorialStruts User and Development Guide (Preface 2)

 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
What are the advantages of using bytecode over native code for platform independence?What are the advantages of using bytecode over native code for platform independence?Apr 30, 2025 am 12:24 AM

Bytecodeachievesplatformindependencebybeingexecutedbyavirtualmachine(VM),allowingcodetorunonanyplatformwiththeappropriateVM.Forexample,JavabytecodecanrunonanydevicewithaJVM,enabling"writeonce,runanywhere"functionality.Whilebytecodeoffersenh

Is Java truly 100% platform-independent? Why or why not?Is Java truly 100% platform-independent? Why or why not?Apr 30, 2025 am 12:18 AM

Java cannot achieve 100% platform independence, but its platform independence is implemented through JVM and bytecode to ensure that the code runs on different platforms. Specific implementations include: 1. Compilation into bytecode; 2. Interpretation and execution of JVM; 3. Consistency of the standard library. However, JVM implementation differences, operating system and hardware differences, and compatibility of third-party libraries may affect its platform independence.

How does Java's platform independence support code maintainability?How does Java's platform independence support code maintainability?Apr 30, 2025 am 12:15 AM

Java realizes platform independence through "write once, run everywhere" and improves code maintainability: 1. High code reuse and reduces duplicate development; 2. Low maintenance cost, only one modification is required; 3. High team collaboration efficiency is high, convenient for knowledge sharing.

What are the challenges in creating a JVM for a new platform?What are the challenges in creating a JVM for a new platform?Apr 30, 2025 am 12:15 AM

The main challenges facing creating a JVM on a new platform include hardware compatibility, operating system compatibility, and performance optimization. 1. Hardware compatibility: It is necessary to ensure that the JVM can correctly use the processor instruction set of the new platform, such as RISC-V. 2. Operating system compatibility: The JVM needs to correctly call the system API of the new platform, such as Linux. 3. Performance optimization: Performance testing and tuning are required, and the garbage collection strategy is adjusted to adapt to the memory characteristics of the new platform.

How does the JavaFX library attempt to address platform inconsistencies in GUI development?How does the JavaFX library attempt to address platform inconsistencies in GUI development?Apr 30, 2025 am 12:01 AM

JavaFXeffectivelyaddressesplatforminconsistenciesinGUIdevelopmentbyusingaplatform-agnosticscenegraphandCSSstyling.1)Itabstractsplatformspecificsthroughascenegraph,ensuringconsistentrenderingacrossWindows,macOS,andLinux.2)CSSstylingallowsforfine-tunin

Explain how the JVM acts as an intermediary between the Java code and the underlying operating system.Explain how the JVM acts as an intermediary between the Java code and the underlying operating system.Apr 29, 2025 am 12:23 AM

JVM works by converting Java code into machine code and managing resources. 1) Class loading: Load the .class file into memory. 2) Runtime data area: manage memory area. 3) Execution engine: interpret or compile execution bytecode. 4) Local method interface: interact with the operating system through JNI.

Explain the role of the Java Virtual Machine (JVM) in Java's platform independence.Explain the role of the Java Virtual Machine (JVM) in Java's platform independence.Apr 29, 2025 am 12:21 AM

JVM enables Java to run across platforms. 1) JVM loads, validates and executes bytecode. 2) JVM's work includes class loading, bytecode verification, interpretation execution and memory management. 3) JVM supports advanced features such as dynamic class loading and reflection.

What steps would you take to ensure a Java application runs correctly on different operating systems?What steps would you take to ensure a Java application runs correctly on different operating systems?Apr 29, 2025 am 12:11 AM

Java applications can run on different operating systems through the following steps: 1) Use File or Paths class to process file paths; 2) Set and obtain environment variables through System.getenv(); 3) Use Maven or Gradle to manage dependencies and test. Java's cross-platform capabilities rely on the JVM's abstraction layer, but still require manual handling of certain operating system-specific features.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools