Use Tomcat and Eclipse to develop Servlet programs

Source: Internet
Author: User

1. Install eclipse
1) download Eclipse IDE for Java EE Developers on the official website and decompress the package;
2. install tomcat plug-in eclipse:
1) download tomcatpluginv33.zip from http://www.eclipsetotale.com/tomcatplugin.html.
2) decompress the package to the plugins directory under the eclipse directory.
3) Restart eclipse
4) if you cannot find the Server selection in Window> Prefences, it indicates that your eclipse version is incorrect. You need to download Eclipse IDE for Java EE Developers.
5) Select Runtime Environment in the Server column, select Add on the right, select the installation path, and then download. After the download is complete, click finished. The selected tomcatServer version has been created.
6) Configure Tomcat in Window> Prefences> tomcat. Tomcat home select the downloaded directory
7) Click the start tomcat button to find a large amount of printed information. Enter http: // localhost: 8080 in the browser. The tomcat web page appears, proving that it has been installed.
3. Create a Servlet Test Program
1) Select File> New> Project ..., in the new Project wizard, select Web-> Dynamic Web project, set Project name to MyFirstDynamicWebProject, next, and generate web. xml;
2) a new class, package com. johnny. test, whose name is helloworld, and super class is HttpServlet;

3), code:


[Java] package com. johnny. test;
 
Import java. io. IOException;
 
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
 
Public class HelloWorld extends HttpServlet implements javax. servlet. Servlet {
/* (Non-Java-doc)
* @ See javax. servlet. http. HttpServlet # HttpServlet ()
*/
Public HelloWorld (){
Super ();
}

/* (Non-Java-doc)
* @ See javax. servlet. http. HttpServlet # doGet (HttpServletRequest request,
HttpServletResponse response)
*/
Protected void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Response. getWriter (). write ("Hello, world 1112! ");
}

/* (Non-Java-doc)
* @ See javax. servlet. http. HttpServlet # doPost (HttpServletRequest request,
HttpServletResponse response)
*/
Protected void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
// TODO Auto-generated method stub
}
}

Package com. johnny. test;

Import java. io. IOException;

Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;

Public class HelloWorld extends HttpServlet implements javax. servlet. Servlet {
/* (Non-Java-doc)
* @ See javax. servlet. http. HttpServlet # HttpServlet ()
*/
Public HelloWorld (){
Super ();
}

/* (Non-Java-doc)
* @ See javax. servlet. http. HttpServlet # doGet (HttpServletRequest request,
HttpServletResponse response)
*/
Protected void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Response. getWriter (). write ("Hello, world 1112! ");
}

/* (Non-Java-doc)
* @ See javax. servlet. http. HttpServlet # doPost (HttpServletRequest request,
HttpServletResponse response)
*/
Protected void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
Web. xml


[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id = "WebApp_ID" version = "2.5">
<Servlet>
<Servlet-name> HelloWorld </servlet-name>
<Servlet-class> com. johnny. test. HelloWorld </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> HelloWorld </servlet-name>
<Url-pattern>/helloworld </url-pattern>
</Servlet-mapping>

</Web-app>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id = "WebApp_ID" version = "2.5">
<Servlet>
<Servlet-name> HelloWorld </servlet-name>
<Servlet-class> com. johnny. test. HelloWorld </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> HelloWorld </servlet-name>
<Url-pattern>/helloworld </url-pattern>
</Servlet-mapping>
 
</Web-app>
4) Right-click HelloWorld. java and select rus as: run on server. The browser displays Hello, world 1112!
5), or right-click the project, export MyFirstDynamicWebProject. war, and place it in the webapps directory of tomcat In ubuntu.

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.