The first deployment to a servlet application on tomcat
Built environment
EE version of eclipse
Creating a java application
1.File->new java project
Import package servlet
Right-click the project -> properties -> Java Build Path -> Libraries -> Add External JARs
Download servlet.jar package
Links: https://pan.baidu.com/s/17aJimvcbiip9E6M-vM6S3w
extraction code: gfxt
copy the contents of this open Baidu network disk phone App, the operation more convenient oh
Write HelloServlet
1. Create a class at the right place src HelloServlet
2.HelloServlet class code
import java.io.IOException;
import java.util.Date;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response){
try {
response.getWriter().println("<h1>Hello Servlet!</h1>");
response.getWriter().println(new Date().toLocaleString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Create the appropriate directories
1. In accordance with the directory structure created corresponding web directory, WEB-INF directory, web.xml file
The contents of the file 2.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
classes directory specified project output
1. Create classes directory under the WEB-INF directory
2. The classes directory to the output directory for the class files
Project Right -> properties-> Java Build Path-> Source-> lower right corner Brower-> specified location is j2ee / web / WEB-INF / classes
The project is deployed to tomcat
1. Open the server.xml file in the conf directory of tomcat
2. In the Host Context tag plus the tag below, and set the access path, the path and project
<Context path="/j2ee" docBase="D:\\programe\\eclipse-ee\\workspace\\myworkspace\\j2ee\\web" debug="0" reloadable="false" />
path is the access path, for example, I set up this way, access path is localhost: 8080 / j2ee / hello
docBase path of the project is to create different settings according to the situation
Running the Project
1. Restart Tomcat
2. Enter in the browser localhost: 8080 / j2ee / hello