JSP & servlet Study Notes (2) helloservlet Analysis

Source: Internet
Author: User

Note: before learning this tutorial, complete the configuration of the servlet development environment.

I. file organization

First, let's take a look at the dynamic Web project directory structure created in Eclipse:


Important directory descriptions:
WEB-INF: Resource items in this directory are not included in the Project root directory for direct access.
WEB-INF/Web. xml: deploy the description file (which needs to be created manually ).
WEB-INF/classes: place the custom class (. Class), which must include the package structure.
WEB-INF/lib: place the JAR file used by the application.

2. Deployment
1) Use @ webservlet
In servlet3.0, you can use annotation to tell the container which servlets will provide services and additional information.
For example
@ Webservlet ("/Hello. View ")
Public class helloservlet extends httpservlet
As long as @ webservlet is set on the servlet, the container will automatically read the information in it. The example above tells the container that if the requested URL is
"/Hello. View" is provided by the helloservlet instance.

2) use web. xml
Web. XML is a more common deployment method, which may be more troublesome.
The settings in Web. xml overwrite the annotation settings in servlet.
Based on the previous example, we can deploy it using web. xml.

Create a web. xml in the WEB-INF and type the following code:

<?xml version="1.0" encoding="UTF-8"?><web-app  version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><servlet><servlet-name>HelloServlet</servlet-name><servlet-class>HelloServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>HelloServlet</servlet-name><url-pattern>/Hello</url-pattern></servlet-mapping></web-app>

Several important labels are explained:
<Servlet> The element defines a servlet instance. The <servlet> element must contain <servlet-Name> and <servlet-class> child elements, and may also contain other initialization parameters.

The <servlet-Name> element defines the unique name of the servlet instance. Each servlet instance must have a unique name. This name is only used for URL ing of this instance, so it does not have to be consistent with the servlet class or servlet URL.

The <servlet-class> element tells the servlet container how to build a servlet class instance. The <servlet-class> element consists of the servlet package name and Servlet class name.

<Servlet-mapping> defines mappings.

<Load-on-startup> 1 </load-on-startup> indicates that the servlet class is loaded when the application is started.

After setting, we can only access the servlet through "http: // localhost: 8080/servlettest1/Hello" instead of the previous hello. view, because @ webservlet is overwritten.


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.