Environment: WIN10 64-bit, Eclipse (installed Tomcat plugin), Tomcat 6.0.45
Time: September 10, 2016 19:10:09 first, build Java-tomcat project
In Eclipse, select File-> New-> Other-> java-> Tomcat Project
Fill in the name of the project, and click Finish to
Ii. to prepare a HelloWorld Web page
First create a package (package), right-click on the item you just created-> New-> package, and then fill in the package name
Then write a class hellowolrd inherit HttpServlet, in the package name just created right button, new Class (Class)
Then write the following Java code:
Package com.zzz.package1;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class HelloWorld extends HttpServlet {public
void doget (HttpServletRequest request, HttpServletResponse Response)
throws IOException, servletexception {
printwriter out = Response.getwriter ();
Out.write ("
Then configure Web.xml, right-click on the Web-inf folder (note, not web-inf\src folder), new-> XML file, name the Web, and click Finish
Then write the following code:
<?xml version= "1.0" encoding= "UTF-8"?> <web-app "xmlns="
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 "
version=" 3.0 "
>
<display-name>hello </display-name>
<description>
Hello
</description>
<!--definition Controller-->
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class> com.zzz.package1.helloworld</servlet-class>
</servlet>
<!--intercept/helloworld requests-->
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/ helloworld</url-pattern>
</servlet-mapping>
</web-app>
Note: The name of the class in the Servlet-class tag and the path is filled in front of the new package and class, need to be filled in with the same, I am here is the package name is Com.zzz.package1, class name is HelloWorld, so The contents of the Servlet-class label are: Com.zzz.package1.HelloWorld
third, visit the HelloWorld page
Note: If Tomcat is already running before code is written, it is recommended that you restart Tomcat.
We open our browser and enter the following path in the address bar to verify that you can access the page you just wrote: Http://localhost:8080/ZzzJavaServer2/HelloWorld, where ZzzJavaServer2 is the name of the project, HelloWorld is the class name
Our first web page has been successful when we write the words in HelloWorld on the Web page.