Servlet3.0-define servlet using annotations

Source: Internet
Author: User

My development environment: myeclipse10 + tomcat7 + jdk6.

Developing a servlet3 program requires some environmental support. Servlet3 is part of the Java ee6 specification. Both myeclipse10 and tomcat7 provide support for the Java ee6 specification.

Tomcat requires Tomcat 7 to support Java ee6, while Tomcat 7 requires JDK 6.

If the version of myeclipse is low and Java ee6 is not supported, you can download the Java ee6 SDK from the official Oracle website for installation, you can also reference all jar files in the Lib folder under the decompressed directory of Tomcat 7 to our project path. (For example, first create a Java ee5 web project, then introduce all the jar files in the Lib folder under the decompressed directory of Tomcat 7 to the project, and then delete the library reference of Java ee5, finally, extract the web from the conf folder in the decompressed directory of Tomcat 7. copy the XML template to the project and replace the original web. XML ).

Servlet3.0 provides annotation, which eliminates the need to deploy servlets in the web. xml file and simplifies the development process.

1. Create a new web project and select Java ee6.0 directly.

2. Check the project package view.

3. Java ee6 library files

4. Web. xml file

<?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">  </web-app>

Note version = "3.0" and http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd.

5. Create a servlet. Write the servlet name and next.

6. You do not need to generate servlet-related information in the web. xml file. Finish.

7. After a servlet is created, there is no description about the servlet in the web. xml file.

<?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"></web-app>

 

8. Use annotations to describe servlet.

 

Package COM. cndatacom. servlet; import Java. io. ioexception; import Java. io. printwriter; import javax. servlet. servletexception; import javax. servlet. annotation. webservlet; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse;/*** use annotation to describe servlet * @ author luxh * // *** annotation webservlet is used to describe servlet * attribute name to describe servlet name. Optional * attribute urlpattern S defines the access URL, or use attribute value to define the access URL. (define the access URL as a required attribute) */@ webservlet (name = "annotationservlet", urlpatterns = "/annotationservlet") public class annotationservlet extends httpservlet {public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcharacterencoding ("UTF-8"); response. setcontenttype ("text/html; charset = UTF-8"); printwriter out = Resp Onse. getwriter (); Out. println ("<! Doctype HTML> "); out. println ("<HTML>"); out. println ("

 

 

 

9. Release the project to Tomcat 7 and start Tomcat 7. Access: http: // localhost: 8080/servlet3/annotationservlet in a browser

Completed the development of a servlet program described using annotations.

Use @ webservlet to define a class inherited from javax. servlet. http. httpservlet as a servlet component.

@ Webservlet has many attributes:

Asyncsupported: Declares whether the servlet supports asynchronous operation mode.

Description: servlet description.

Displayname: servlet display name.

Initparams: init parameter of servlet.

Name: servlet name.

Urlpatterns: servlet access URL.

Value: the access URL of the servlet.

The servlet access URL is a required attribute of servlet. You can use urlpatterns or value to define it.

As the above annotationservlet can be described as @ webservlet (name = "annotationservlet", value = "/annotationservlet ").

Multiple URL accesses are also defined:

For example, @ webservlet (name = "annotationservlet", urlpatterns = {"/annotationservlet", "/annotationservlet2 "})

Or @ webservlet (name = "annotationservlet", value = {"/annotationservlet", "/annotationservlet2 "})

 

From: http://www.cnblogs.com/luxh/archive/2012/06/06/2537458.html

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.