How to create a servlet3.0 in MyEclipse

Source: Internet
Author: User

servlet3.0 with the Java EE6 specification, how do you create a 3.0 version of the servlet in MyEclipse?

1, start MyEclipse, get:


2. Click on the "File" toolbar to get:


3. Move your mouse to "New alt+shift+n" and then select and click the "Web Project" option in the Red box to get:


4. Do the following:

A, "Project name" (blue box) Enter the name of the project created; here, enter test;

b, check "Java EE 6.0", note: Some myeclipse do not have this option, which is due to the low version of the cause;

C, complete the above two steps to directly click on the "Finish" button;

Through the steps A, B and C above, we obtain:


5, through three places by the red box text can be found that the project is servlet3.0 version of the. Select SRC in the new project, right-click the mouse to "new" and get:

6. Select and click the "Servlet" option in the red box, and get:


7. Do the following:

A, "Package" (red box) Enter the name of the servlet that was created; here input com.ghj.packageofservlet;

B, "Name" (blue box) Enter the name of the servlet being created; here input testservlet;

C, complete the above two steps to directly click on the "Next" button;

Through the steps A, B and C above, we obtain:

8, to do the following: If you want to use the Servlet3.0 new feature-the request path that the servlet accepts as a comment, the Red-box checkbox is canceled (the Web. xml file will not contain some configuration information for the servlet that was created. Uncheck the check box here ), or click the "Finish" button directly. This 3.0 version of the servlet was created successfully. Modify the servlet to the following code:

Package Com.ghj.packageofservlet;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; @WebServlet ( Urlpatterns = "/testservlet")//indicates that this servlet only accepts Testservlet request public class Testservlet extends HttpServlet {private static Final long serialversionuid = -4016775927444534220l;public void doget (HttpServletRequest request, HttpServletResponse Response) throws Servletexception, IOException {doPost (request, response);} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Response.setcontenttype ("text/html; Charset=utf-8 "); PrintWriter out = Response.getwriter (), OUT.PRINTLN ("<!doctype HTML public \"-//W3C//DTD HTML 4.01 transitional//en\ " > "); Out.println (" 

9. The index.jsp created when the project was created is modified as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><%string basepath = request.getscheme () +"://"+request.getservername () +" : "+request.getserverport () +request.getcontextpath () +"/";%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

10. Launch Tomcat and publish the project; Enter http://127.0.0.1:8080/test/in the browser and click Enter to get:


11. Click on the "Test" (red box) hyperlink above, and get:


Attention:

A, some children's shoes may have this problem: My project was published using Tomcat, where the servlet uses a comment to accept the request, but when I click on the "test" hyperlink times wrong, oh ah, start I also met, The study found that if the Tomcat version is less than 7.0 (excluding 7.0), post-release projects are not a--tomcat version to 7.0 (including 7.0) versions above.

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.