Some knowledge points of WebApp

Source: Internet
Author: User
Tags auth configuration settings error handling versions tld tomcat
Web Engineering Catalog parsing

Directory structure as shown:


Web projects developed with Eclipse

SRC folder: Saves all Java class files (. java files) and configuration files (. Xml. Properties).

WebContent: Save all JSP files, including CSS, JavaScript, and so on. The Web-inf folder contained in it has a very important role to play.

It is generally thought that the post to Tomcat is under the following: Tomcat\webapps\abc directory is full copy of all the contents of myeclipse\workspace\abc\webroot\.

Meta-inf: Equivalent to an information package, files and directories in the directory get recognition and interpretation of the JAVA2 platform, used to configure applications, extenders, ClassLoader and services, MANIFEST.MF files, automatically generated when packaging with jars

The directory structure of the SRC directory and the classes directory is exactly the same, except for a. java file and configuration file, and a. class file and a configuration file. The actual server is published is the classes directory.

We can directly access the JSP by entering http://localhost:8080/TestProject/pages/error.jsp directly from the browser address, but there is a special/web-inf directory for the WEB application. Resource items that exist in this directory are not listed as items that are directly accessible in the application root directory. That is, the client (for example, the browser) can not directly request the resources in/web-inf (directly on the URL to indicate access to/web-inf), or 404 Not Found error results, so this inside the page must be for security relatively high page. The resource project in/web-inf has a certain name and structure. The Web-inf directory structure and parsing are as follows:

Web-inf
/web-inf/web.xml
Your Web application configuration file, which is an XML file that describes the servlet and other application component configuration and naming rules;
/web-inf/classes/
This directory contains all the class files used by the site, including Servletclass and non-servletclass, which cannot be included in the. jar file.
The storage rules for the class of the site should be executed in accordance with the Java packaging rules. For example, if you have a class named Com.mycompany.mypackage.MyServlet, you should deploy it in the following form:/web-inf/classes/com/mycompany/mypackage/ Myservlet.class;
Placing individual classes (JavaBean and Servlets) by package name structure
/web-inf/lib/
Store the various jar files required by your web app to place the jar files that are required only in this application, such as database-driven jar files
/web-inf/src/
Src=source Source directory, according to the package name structure to place each Java file
/web-inf/database.properties Property configuration file
/web-inf/tags/
Tag file library, store the customer-defined tag file, the directory is not necessarily tags, users can according to their own preferences and habits for their own tag library name, when using the user-defined tag file library names, users must declare the correct label file library path when using the tag file. For example: When the custom label file library name is Simpletags, when you use a label file in the Simpletags directory, you must declare the JSP file header as: <% @taglib prefix= "tags" tagdir= "/web-inf/ Simpletags "% >;
/web-inf/jsp/
File storage locations for the following versions of JSP 1.2. The directory does not have a specific declaration, and the user can be named according to their preferences and habits. This directory is mainly in JSP 1.2 version of the file, in order to differentiate JSP 2.0 files, usually use JSP name, of course you can also name jspoldedition;
/web-inf/jsp2/
Compared with the JSP file directory, the directory is mainly under the JSP 2.0 version of the file, of course, it can be arbitrarily named, but also to distinguish between JSP 1.2 versions of the file directory, usually named JSP2.Considerations for the Web-inf directory:

1. The page resource files can only be placed under the Webroot, such as Css,js,image and so on. It can't be quoted under Web-inf.

2. The page is placed under the Web-inf directory to restrict access and improve security. such as jsp,html

3. You can only access the JSP in the Web-inf directory using the redirect mode, without redirecting any resources in the directory. Figure:index.jsp>> main.jsp
4.web-inf directory When a file accesses a resource file, the Web-inf layer directory can be ignored. If main.jsp to use a CSS file in the CSS directory.

This is OK, from the client's address can be seen, the server steering main.jsp is under the Webroot. So main.jsp and CSS directories can be said to be the same level directory.
Access the images directory under the 5.web-inf/oa directory. What to do. or this.
How do I access files in the 6.web-inf directory? As in main.jsp with the test OA path access
Like main.jsp, there are 10 links to other pages in the Web-inf directory. Then there must be 10 steering action. This can be handled with the Dispatchaction class plus parameters specifically for the steering work.

NOTE: Steering mode: As configured in the Struts-config file
or write Request.getrequestdispatcher ("/web-inf/main.jsp") in action. Forward (Request,response);
The server reads the contents of the page and sends it to the client. The address of the client is unchanged. Content Jumps
REDIRECT mode: Configured in the Struts-config file

Redirect= "true"/> or Response.sendredirect ("/error.jsp") in action;
The meaning of redirection is that the server sends the address to the client and allows the client to access it. This approach clearly targets the Web-inf directory as useless.

Web. XML element Introduction
The web-inf of each station has a config file for Web. XML, which provides the configuration settings for our platform.
The Web. XML definition:

. Name and description of the platform
Initializing work for Environment parameters (context)
. Name and mapping of the servlet
. setting of the session
. The mapping of the TAG library
. JSP Web page settings
. Mime type Processing
. Error handling
Using Jdni to obtain platform resources

tags used in the Web. xml file:
<description>,<display-name>,<icon> ,<distributable> ,< Context-param> ,<filter>,<filter-mapping>,<servlet>,<servlet-mapping>,< Listener>,<session-cofing>,<mime-mapping>,<welcome-file-list> ,<error-page> ,<jsp-config>,<resource-ref> , < Security-constraint>,<Login-config>, <security-role>

Description:
<description> platform Description </discription> 
Description of the platform.  

<display-name> platform name </display-name> 
defines the name of the platform.  

<icon> 
The icon element contains the Small-icon and Large-icon two child elements. A path that specifies small icons and large icons for the web platform.  
<small-icon>/Path/smallicon.gif</ small-icon> 
<large-icon>/path/largeicon-jpg</large-icon> 


<distributable> 
distributable element is an empty label, it can specify whether the platform can be distributed processing. If this element is present in Web. XML, the platform is  
is designed to be distributed across multiple Jspcontainer.  
Example:   <distributable/> 

< context-param>  the
Context-param element is used to set the environment parameter (context) of the Web platform, which contains two child elements:  
Param-name and Param-value.  
<param-name> parameter name </param-name> 
Set context name  
<param-value> value </ param-value> 
Sets the value of the context name  
</context-param> 
Example:  
<context-param > 
  <param-name>param_name</param-name> 
  <param-value >PARAM_VALUE</PARAM-VALUE>&NBSP
</context-param> 
This parameter can be used in JSP Web pages to obtain: 
${initparam.param_name} 
If the servlet can be obtained using the following methods:  
Stringparam_name=getservletcontext (). Getinitparamter ("Param_name");  

<filter>
The filter element is used to declare the relevant settings for the filter. The filter element includes, in addition to the child elements described below, <servlet> introduced <icon>,<display-name>
, <description>,<init-param> it uses the same.
Name of <filter-name>filter </filter-name>
Defines the name of the filter.
Class name of <filter-class>filter </filter-class>
Defines the class name of the filter. For example: Com.foo.hello

Example:
<filter>
<filter-name>setCharacterEncoding</filter-name>
<filter-class>coreservlet.javaworld.CH11.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>

<filter-mapping>
The three main child elements of the filter-mapping element Filter-name and Url-pattern and. Dispatcher

Name of <filter-name>filter </filter-name>
<url-pattern>URL</url-pattern>

The above two are better understood, dispatcher is based on the request type class to filter

<dispatcher>request| include| forward| Error</disaptcher>
Set the filter corresponding request method, there are rquest,include,forwar,error four kinds, the default is request.

Examples of dispatcher can be found in: http://hintcnuie.iteye.com/blog/226251
Example:
<filter-mapping>
<filter-name>GZIPEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>

The servlet has two main child elements servlet-name and Servlet-class
Name of <servlet-name>servlet <servlet-name>
<servlet-class>class Full Name <servlet-class>

<servlet-mapping>
The servlet-mapping element contains two child elements servlet-name and url-pattern. Used to define the URL of the servlet.
The name of the <servlet-name>servlet, consistent with the Servlet-name name in the servlet tag </servlet-name>
<url-pattern>ServletURL</url-pattern>
Defines the rul that the Servlet corresponds to. For example:<url-pattern>/servlet/hello</url-pattern>

Example:
<servlet-mapping>
<servlet-name>LoginChecker</servlet-name>
<url-pattern>/LoginChecker</url-pattern>
</servlet-mapping>

<listener>
The listener element is used to define the listener interface, and its primary child element is <listener-class>
Class name of <listen-class>listener </listener-class>
Defines the class name for the listener. For example: Com.foo.hello
Example:
<listener>
<listener-class>coreservlet.javaworld.CH11.ContenxtListener</listener-class>
</listener>


<session-config>
The session-config contains a child element, Session-timeout. Defines the session parameter in the Web platform.
<session-timeout> minutes </session-timeout>
Defines the expiration date for all sessions of this web site. Units are minutes.
</session-config>
Example:
<session-config>
<session-timeout>20</session-timeout>
</session-config>

<mima-mapping>
The mime-mapping contains two child elements extension and Mime-type. Defines an extension and a MIME type to be mapped.

function is used to specify the download format
<extension> extension name </extension>
<mime-type>mime format </mime-type>

Example:
<mime-mapping>
<extension>doc</extension>
<mime-type>application/vnd.ms-word</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xls</extension>
<mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>
<mime-mapping>
<extension>ppt</extesnion>
<mime-type>application/vnd.ms-powerpoint</mime-type>
</mime-mapping>

<welcome-file-list>
The welcome-file-list contains a child element, welcome-file. Used to define the first page of the list.
<welcome-file> used to specify the home page file name </welcome-flie>
Welcome-file is used to specify the home page file name. We can use <welcome-file> to specify a few home pages, and the server will be in accordance with the set order to find the homepage.
Example:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>

<error-page>
The Error-page element contains three child elements, Error-code,exception-type, and location. Corresponds to the type of error code or exception (exception)
To the Web platform resource path.
<error-code> Error Codes </error-code>
HTTP Error Code, for example: 404
<exception-type>Exception</exception-type>
A full-name Java exception type
<location>/Path </location>
Related resource paths within the Web platform
</error-page>
Example: Return a 404 error request to the Error404.jsp page and return the java.lang.Exception exception request to the except.jsp page
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/except.jsp</location>
</error-page>

<jsp-config>
The Jsp-config element is primarily used to set the relevant configuration of the JSP,<jsp:config> including <taglib> and <jsp-property-group> two sub-elements. Where <taglib> The element already exists at JSP 1.2, and <jsp-property-group> is the new element in JSP 2.0.
<taglib>
The Taglib element contains two child elements Taglib-uri and taglib-location. Used to set the tag library path used by the JSP Web page.
<taglib-uri>URI</taglib-uri>
Taglib-uri the taglib directive of the URI,JSP Web page that defines the TLD file can be accessed through this URI to the TLD file.
<taglib-location>/WEB-INF/lib/xxx.tld</taglib-laction>
The TLD file corresponds to the location where the Web site is stored.
<jsp-property-group>
The Jsp-property-group element contains 8 elements, respectively:
<description>Description</descrition>
Description of this setting
<display-name>Name</display-name>
The name of this setting
<url-pattern>URL</url-pattern>
The range affected by the setpoint, such as:/ch2 or/*.jsp
<el-ignored>true|false</el-ignored>
True to indicate that El syntax is not supported.
<scripting-invalid>true|false</scripting-invalid>
True indicates that the <%scription%> syntax is not supported.
<page-encoding>encoding</page-encoding>
Set the encoding of the JSP Web page
<include-prelude>.jspf</include-prelude>
Sets the header of the JSP Web page with the extension. JSPF
<include-coda>.jspf</include-coda>
Sets the end of the JSP Web page with the extension. JSPF
Example:

<jsp-config>
<taglib>
<taglib-uri>Taglib</taglib-uri>
<taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
</taglib>
<jsp-property-group>
<description>
Special PropertyGroup for JSP Configuration JSP example.
</description>
<display-name>JSPConfiguration</display-name>
<uri-pattern>/*</uri-pattern>
<el-ignored>true</el-ignored>
<page-encoding>GB2312</page-encoding>
<scripting-inivalid>true</scripting-inivalid>
............
</jsp-property-group>
</jsp-config>

<resource-ref>
The Resource-ref element consists of five child element Description,res-ref-name,res-type,res-auth,res-sharing-scope. Use Jndi to get a platform
Leverage resources.
<description> Description </description>
Resource Description
<rec-ref-name> Resource Name </rec-ref-name>
Resource Name
<res-type> Resources Category </res-type>
Types of resources
<res-auth>application| Container</res-auth>
Resources are licensed by application or container
<res-sharing-scope>shareable| Unshareable</res-sharing-scope>
Whether the resource can be shared. Default value is shareable
Example:
<resource-ref>
<description>jndi Jdbcdatasource of Jspbook</description>
<res-ref-name>jdbc/sample_db</res-ref-name>
<res-type>javax.sql.DataSoruce</res-type>
<res-auth>Container</res-auth>
</resource-ref>

< security-constraint>
The Security-constraint security-constraint element formulates the URL that should be protected. It is used in conjunction with the Login-config element

< login-config>
Login-config uses the Login-config element to specify how the server should authorize users attempting to access a protected page. It is used in conjunction with the Sercurity-constraint element.

<security-role>
The Security-role security-role element gives a list of security roles that will appear in the Role-name child elements of the security-role-ref element within the servlet element. Declaring roles separately makes it easier for advanced Ides to handle security information.


the difference between Classpath and classpath*:

First, classpath refers to the classes directory under the Web-inf folder.
explain Classes Meaning:
1. Store Various resource profiles eg.init.properties log4j.properties struts.xml
2. Storing template files EG.ACTIONERROR.FTL
3. the storage class file corresponding to the project development of the src Directory compilation Files

Summary: This is an entry point for locating resources

Issues with file access priority under LIB and classes: Classes > Lib
Classpath and the classpath* difference:
Classpath : It will only come to you. class find files in path ;
classpath* : Contains not only class path, also includes Jar in the file (class path ) to find .

In the spring configuration file, you can use the classpath: prefix to load resources from the CLASSPATH
For example, there is a jdbc.properties file under SRC, which can be loaded as follows:

1. <bean id= "Propertyconfigurer"

2. class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

3. <property name= "Locations">

4. <list>

5. <value>classpath:jdbc.properties</value>

6. </list>

7. </property>

8. </bean>

For resources packaged in a jar package, you can do the same in the same way:

1. <import resource= "Classpath:meta-inf/cxf/cxf.xml" />

Another very similar way is to use the classpath*: prefix, such as

1. <property name= "mappinglocations">

2. <list>

3. <value>classpath*:/hibernate/*.hbm.xml</value>

4. </list>

5. </property>

Classpath: With classpath*: The difference is that the former will only be loaded from the first classpath, the latter will load from all the classpath if the resources to be loaded, not in the current ClassLoader path, then use Classpath : The prefix is not found, in this case you need to use classpath*: prefix in another case, there is a resource with the same name in more than one classpath, you need to load, then with classpath: Only the first one, in this case also need to use the classpath*: prefix Imagine, with classpath*: Need to traverse all the classpath, so the loading speed is very slow, so in the planning time, should be as far as possible to plan the path of the resource files, try to avoid the use of classpath*

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.