Servlet+tomcat to make the first Java application running on Tomcat

Source: Internet
Author: User

reprinted from: Http://www.linuxidc.com/Linux/2011-08/41685.htm
[Date: 2011-08-27] Source: Csdn Cloudyxuq
1.IDE tools are available for myeclipse or eclipse

If it is eclipse need to download Tomcatt http://tomcat.apache.org and a plugin after unpacking Tomcatpluginv32.zip

2. To better understand how the server-side application executes, it is created manually. Project as follows

Create a Webroot folder under Servletdemo to create Web-inf to hold Lib and classes

The servlet is actually a Java file

To create the Myservlet class:

  1. Package Com.servlet;
  2. import java.io.IOException;
  3. import Java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import Javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. /**servlet is actually a Java file * /
  9. Public class Myservlet extends httpservlet {
  10. /**
  11.      * 
  12.      */
  13. private static final long serialversionuid = 1L;
  14. //Call the construction method of the parent class
  15. Public Myservlet () {
  16. Super();
  17. }
  18. //Overriding the Doget () method of the parent class
  19. protected void doget (httpservletrequest req, HttpServletResponse resp)
  20. throws Servletexception, IOException {
  21. //Usually the GET request is forwarded to the POST request
  22. DoPost (REQ,RESP);
  23. }
  24. protected void doPost (httpservletrequest req, HttpServletResponse resp)
  25. throws Servletexception, IOException {
  26. //TODO auto-generated method stub
  27. //write response information for processing post requests
  28. PrintWriter Pw=resp.getwriter ();
  29. Pw.println ("This ismy fisrt Servlet");
  30. Pw.flush ();
  31. Pw.close ();
  32. }
  33. }

3. Build your servlet's configuration file with the Web. XML code:

  1. <? XML  version =< Span class= "Attribute-value" > "1.0"  encoding = "UTF-8" ?>   
  2. <Web-app id="webapp_id" version="2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"
  3. xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee
  5. Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
  6. <!--adding servlet elements --
  7. <servlet>
  8. <!--servlet implementation name, can be arbitrary, but it's best to match your Servlet implementation class name --
  9. <servlet-name> Myservlet</servlet-name>
  10. <!--To specify the implementation class for the servlet--
  11. <servlet-class>com.servlet.MyServlet</servlet-class >
  12. <!--load-enabled (Load-on-startup sets the server load time >0 is loaded numerically, and if it is <0, it will only be loaded when the servlet is called)--
  13. <load-on-startup>1</load-on-startup>
  14. <!--display name --
  15. <display-name> first servlet</display-name>
  16. </servlet>
  17. <!--access servlet through a page, requires servlet mapping configuration-
  18. <servlet-mapping>
  19. <!--name must be the same as that in the servlet--
  20. <servlet-name> Myservlet</servlet-name>
  21. < when calling a servlet class in a!--page, the name can be arbitrarily taken, but required/-
  22. <url-pattern>/myfirstservlet</url-pattern>
  23. </servlet-mapping>
  24. <!--default page--
  25. <welcome-file-list>
  26. <!--can set up many pages, such as index.htm,index.html, etc.--
  27. <welcome-file>
  28. index.jsp
  29. </welcome-file>
  30. <welcome-file>
  31. Index.html
  32. </welcome-file>
  33. <welcome-file>
  34. default.jsp
  35. </welcome-file>
  36. </welcome-file-list>
  37. </web-app>
4. Create your own first index.jsp
  1. <html>
  2. <head>
  3. <Meta   http-equiv= "content-type" Content="text/html; Charset=iso-8859-1 ">
  4. <title> my servlet</title>
  5. </head>
  6. <body>
  7. This is my index
  8. </Body>
  9. </html>
5. Configure the virtual directory in the Server.xml in the Conf file under the Tomcat folder to access it through IE and other browsers

Established between the

  1. <Host>
  2. .
  3. .
  4. .
  5. .
  6. .
  7. <!--Add virtual directory, docbase to real directory--
  8. <Context Path="/servletdemo" docBase="C:\Users\Cloudy\workspace\ Servletdemo\webroot " reloadable=" true "/>
  9. </Host>

6. Visit index and Myservletdemo separately

Servlet+tomcat to make the first Java application running on Tomcat

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.