Java Web application Basics

Source: Internet
Author: User


This work is licensed using the "knowledge sharing signature"-"non-commercial use"-2.5 mainland China License Agreement in the same way.

 

 

Java-based web applications are an application that implements HTTP through Java technology. Servlet technology is the core technology used to implement HTTP in Java technology. With the continuous improvement of the architecture technology, the basic servlet theory is gradually forgotten. This article discusses the basic knowledge that has been forgotten by everyone.

Basic Java Web application theory

A unified Resource Locator (URL, abbreviated as "Uniform Resource Locator") is also called a webpage address, which is a standard resource address on the Internet.
A typical URL request is as follows:

Http: // localhost: 8080/Hello/index.html

It consists of the following parts,
HTTP header: http ://
Server address: localhost (IP address or domain name)
Port: 8080 (the default port number for HTTP protocol is 80)
Application context:/Hello (if the root directory of the application context is root, the application context is empty)
Resource or request:/index.html
Java Servlet technology solves the problem of resource or request ing.

Directory structure of Web Applications

To create a Java-based web application, the basic directory structure is as follows:

/[Webcontent] web application context root directory
/WEB-INF configuration file directory
Core web. XML Web application configuration file
/Lib: Class Library directory for storing all jar packages
/Classes stores the class paths of all class files not packaged

As needed, the Web application context can be defined as any name or root.
If the root directory of the Web application context is root, the user does not need to provide the Web application context when accessing the web application. For example:

Http: // localhost: 8080/index.html

If the root directory of the Web application context is not root, the Web application context must be explicitly indicated when accessing the client. For example, the access method with the context name "hello" is as follows:

Http: // localhost: 8080/Hello/Index.html

The/WEB-INF directory places all resources that do not allow direct access to users. A typical file is Web. XML, which is the core configuration file of Java Web applications.
The main task of Web. XML is to map user requests to resources that cannot be accessed directly by users.

Requests defined in Web. xml

In a web application, a servlet represents a URL request. Servlet can be a JSP file resource or a servlet class. Servlet is composed of <Servlet/> and <servlet-mapping/> in Web. xml. In <servlet-mapping> </servlet-mapping>, specify the Request Method of the client, and specify the servlet class or JSP file of the server response in <servlet> </servlet>. For example:

<Servlet> <br/> <servlet-Name> jspindex </servlet-Name> <br/> <JSP-File>/WEB-INF/JSP/index. JSP </JSP-File> <br/> </servlet> <br/> <servlet-Name> servletindex </servlet-Name> <br /> <servlet-class> test. servlet. indexservlet </servlet-class> <br/> </servlet> </P> <p> <servlet-mapping> <br/> <servlet-Name> jspindex </Servlet -Name> <br/> <URL-pattern>/index.html </url-pattern> <br/> </servlet-mapping> <br/> <servlet-mapping> <br/> <servlet-Name> sevletindex </servlet-Name> <br/> <URL-pattern>/index. JSP </url-pattern> <br/> </servlet-mapping>

Through the above example, you can understand the following points:
1. Associate <servlet-name/> <servlet> </servlet> with <servlet-mapping> </servlet-mapping>.
2. <URL-pattern/> specifies the client request method. The request must start with "/" and be based on the Web application context.
3. The request method specified by <URL-pattern/> is independent of the location of JSP resources or Servlet classes.
4. <JSP-file/> specifies the directory location of JSP resources in Web applications. The directory must start with "/" and be located in the root directory of the Web application context.
5. <servlet-class/> specifies the servlet class location. The location information includes the package name and class name.

Web Application resource locating method

We call the path starting with "/" an absolute path, and the path not starting with "/" a relative path. There are three common methods for locating Java resources: file system locating, class path locating, and Web application context locating. For file system positioning and class path positioning, refer to the article "positioning methods of Java resources". The context positioning of Web applications is based on the absolute path of the context root directory, use the directory where the source file is located as the relative path to locate the resource.
For example, the description of Web. XML in a web application whose root directory is "/hello"
<Servlet> <br/> <servlet-Name> jspindex </servlet-Name> <br/> <JSP-File>/WEB-INF/JSP/index. JSP </JSP-File> <br/> </servlet> <br/> <servlet-mapping> <br/> <servlet-Name> jspindex </servlet-Name> <br/> <URL-pattern>/index.html </url-pattern> <br/> </servlet-mapping>

The above description provides us with the following information:
/WEB-INF/JSP/index. jsp describes the directory location of the index. jsp file in the Web application (it is relative to the root directory of the Web application ):

/Hello (root directory of the Web application context)
WEB-INF
/Jsp
Index. jsp

Programmatic servlet class

The custom servlet class must inherit from javax. servlet. http. httpservlet to process HTTP requests. The complete package name and Class Name of the custom servlet class must be specified in <servlet> </servlet> of Web. XML, for example:
<Servlet> <br/> <servlet-Name> servletindex </servlet-Name> <br/> <servlet-class> test. servlet. indexservlet </servlet-class> <br/> </servlet> <br/> <servlet-mapping> <br/> <servlet-Name> sevletindex </servlet-Name> <br/> <URL-pattern>/index. JSP </url-pattern> <br/> </servlet-mapping>
Servlet class can be packaged into a jar package, stored in the/WEB-INF/lib directory; can also be not packaged, stored in the/WEB-INF/classes directory.

Resources that can be accessed directly

/[Webcontent] is the root directory of a Web application. Resources (subdirectories or files) stored in this directory can be directly accessed by the client, as shown in the following directory structure:

/Hello (root directory of the Web application context)
Welcome.html
/Images
Logo.png
WEB-INF
Web. xml
/Lib
/Classes

Based on the Web application of the overview structure, the user can directly access welcome.html, such:

Http: // localhost: 8080/Hello/Welcome.html

You can also directly export logo.png, such:

Http: // localhost: 8080/Hello/Images/logo.png

In a Java Web application, the system first locates the ing between the specified request and the resource in Web. XML, and then locates the directly accessible resource.
See the following directory structure:

/Hello (root directory of the Web application context)
Welcome.html
/Images
Logo.png
WEB-INF
Web. xml
/Lib
/Classes
/Test
/Servlet
Indexservlet. Class

The following definitions exist in Web. xml:
<Servlet> <br/> <servlet-Name> servletindex </servlet-Name> <br/> <servlet-class> test. servlet. indexservlet </servlet-class> <br/> </servlet> <br/> <servlet-mapping> <br/> <servlet-Name> sevletindex </servlet-Name> <br/> <URL-pattern>/welcome.html </url-pattern> <br/> </servlet-mapping>

At this time, if the client requests/Hello/welcome.html, the system locates test.servlet.indexservletbut not the welcome.html resource file.

Use Basic Theories to implement the MVC Architecture

MVC is the architectural mode of the UI Layer. It consists of three parts: M-model, V-view ), c-controller ). The basic process of MVC Architecture in Java-based Web applications is as follows:

  1. The client sends a request to the Web server, which contains form data;
  2. The Controller responds to the request to obtain the form data and verify its validity. After the verification is passed, the form data is transmitted to the model;
  3. Model obtains the form data for business processing, and sends the processing result to the Controller;
  4. The Controller saves the displayed data to the buffer zone based on the processing result;
  5. The Controller redirects the processing result to the specified view;
  6. View gets the Data Rendering screen used for display from the buffer;
  7. View writes the image to response.

The view role is usually completed by JSP web page resources, while the servlet class implements the controller function.

Model is a business module, which is mainly used by JavaBeans to implement business processing functions. We will not describe it in detail here.

Method for Extracting form data

Generally, form submission is divided into post and get methods. File Upload uses multi-part method to submit a form. On the server side, generally, the form can be extracted using the request. getparameter () and request. getparametervalues () methods. The multi-part form can be extracted using the following methods:

String filefieldname = "data"; <br/> multiparthttpservletrequest = <br/> (multiparthttpservletrequest) request; <br/> multipartfile = <br/> callback. getFile (filefieldname );

Buffer zones of different ranges

Based on different lifecycles, the Web application buffer distinguishes request, session, and application. In JSP files, these three objects can be directly accessed. In the servlet class, the access methods for these three objects are as follows:

Protected void doget (<br/> // request is introduced by the doget () and dopost () parameters in the servlet class <br/> httpservletrequest request, <br/> httpservletresponse response) <br/> throws servletexception {</P> <p> // session acquisition <br/> httpsession session = request. getsession (); </P> <p> // The application is obtained through getservletcontext () of the servlet class. <Br/> servletcontext application = getservletcontext (); <br/>}

Request, session, and application have the getattribute () and setattribute () methods for reading and saving data.

Request redirection

Forward and redirect are two basic redirection methods of servlet.
Forward is called in the following way:

Request. getrequestdispatcher ("/index.html"). Forward (request, response );

Redirect is called in the following way:

Response. sendredirect ("/index. jsp ");

The forward method completely retains the data in the request, session, and application during the redirection process. In the Redirect mode, session data is retained, but the data in the request is not retained, and all data is lost when switched.

From the business logic point of view, it turns to the internal and external directions of the substation.
Intra-site redirection refers to redirection to other JSP pages or other servlets on the site. Both forward and redirect can perform intra-site redirection. The out-of-site redirection refers to the redirection to other websites. The out-of-site redirection can only be completed in redirect mode, and the forward mode does not provide the out-of-site redirection function.

Rendering of JSP web pages

There are multiple methods to extract the buffer on the JSP webpage. The most basic method is the scripting language method, for example:

<% String contextpath = request. getcontextpath (); %> <br/> <form action = "<% = contextpath %>/index.html"> <br/> </form>

After jsp2.0, El expressions become part of the JSP standard and can be directly used in jsp2.0.
El expressions can only read information in requests, sessions, and applications. data cannot be obtained directly in a scripting language, for example:

<% <Br/> string contextpath = request. getcontextpath (); <br/> request. setattribute ("contextpath", contextpath); <br/>%> <br/> <form action = "$ {contextpath}/index.html"> <br/> </form>

In addition to the JSP standard, you can also use a large number of third-party tag libraries, such as the jstl standard tag library, Struts tag library, and spring tag Library, to simplify and improve data read and save operations in the data buffer zone.

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.