before you read this article, make sure you understand Eclipse build Web engineering, and if you have questions, check out the first article in this series Eclipse software operation Highlights (i)
1. We add a servlet to configure the Web. XML to test if it is displayed properly.
:
The servlet and Web. xml file contents are as follows.
1 Packagecom.vastsum;2 3 Importjava.io.IOException;4 5 Importjavax.servlet.ServletException;6 ImportJavax.servlet.http.HttpServlet;7 Importjavax.servlet.http.HttpServletRequest;8 ImportJavax.servlet.http.HttpServletResponse;9 Ten Public classHelloServletextendshttpservlet{ One //overriding the Doget method A @Override - protected voidDoget (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException { - //TODO auto-generated Method Stub the //output content to the browser -Resp.getwriter (). Write ("Test servlet"); - } -}
View Code
corresponding XML code
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Web-appversion= "2.5" 3 xmlns= "Http://java.sun.com/xml/ns/javaee" 4 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">7 8 <servlet>9 <Servlet-name>HelloServlet</Servlet-name>Ten <Servlet-class>Com.vastsum.HelloServlet</Servlet-class> One </servlet> A - <servlet-mapping> - <Servlet-name>HelloServlet</Servlet-name> the <Url-pattern>/hello</Url-pattern> - </servlet-mapping> - - </Web-app>
View Code
After the add is complete, click Run
Error occurs after running:
2. Next, fix the problem of the error:
The error is because the directory of the generated class file is incorrect, so let's take a look at the directory of class in the workspace project root directory.
found that the generated class file is not under the Web-inf folder, but in the build folder, next we want to make the following settings.
First we want to delete the published myweb.
Then set the runtime to publish to the Tomcat server corresponding directory:
Modify the above two parameters, which means to publish the project to the Tomcat server.
Finally we select the project, right click on build path, Configure build path, and then:
This is to control the Java files to be built into the correct directory.
3. After the folder location has been modified. Run the Web project again.
Then enter in the browser address bar: Http://localhost:8080/MyWeb/hello can access the website normally.
**********************************************************************
Solutions to some common problems
1. Creating a new Web project with Eclipse does not automatically generate XML
To create a project click:
Eclipse for Java EE software Operations Highlights (ii)