1、:配置好相应环境和检查tomcat8.5能否运行,详见https://www.cnblogs.com/LJHAHA/p/10461697.html

2、将tomcat8.5下的webapps目录中所有内容备份到另一文件夹(bak)中,再清空webapps目录;

3、进入eclipse,配置好相应环境后,进入Servers,点击下方红色圈处,将tomcat8.5集成到eclipse中;

JavaEE-Servlet的部署和配置-LMLPHP

4.选择新建一个Apache下的Tomcat v8.5 Server--->Finish

JavaEE-Servlet的部署和配置-LMLPHP

5.双击红色圈出的Tomcat v8.5 Server at localhost,进入上方的设计页面,其余的不做更改,需要将Server Locations改选为第二项Use Tomcat installation,同时检查下方路径是否正确,最好将Deploy path改为tomcat8.5下自带的webapps,最后别忘了点击保存;

JavaEE-Servlet的部署和配置-LMLPHP

6.新建一个Dynamic Web Project(名为prj1),在Java Resources->src目录下新建一个包,名为edu.lsnu.first,在该包下新建一个HelloServlet类,编辑以下内容;

 package edu.lsnu.first;

 import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class HelloServlet implements Servlet{ public static void main(String[] args) {
} @Override
public void destroy() {
} @Override
public ServletConfig getServletConfig() {
return null;
} @Override
public String getServletInfo() {
return null;
} @Override
public void init(ServletConfig arg0) throws ServletException {
} @Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("<h1>欢迎光临</h1>");
out.close();
} }

7.接下来,再进入web->MEB_INF目录下,点开web.xml文件,编辑以下内容;JavaEE-Servlet的部署和配置-LMLPHP

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>prj1</display-name>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>edu.lsnu.first.HelloServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

编译器的最终布局显示如下:

JavaEE-Servlet的部署和配置-LMLPHP

8.点击Tomcat v8.5 Server at localhost,右键选择Add and Remove...,将项目prj1添加到Tomcat中来;

JavaEE-Servlet的部署和配置-LMLPHP

9.启动服务器;

JavaEE-Servlet的部署和配置-LMLPHP

成功后会显示下图情况

JavaEE-Servlet的部署和配置-LMLPHPJavaEE-Servlet的部署和配置-LMLPHP

10.最后在浏览器中输入http://127.0.0.1:8080/prj1/hello或http://localhost:8080/prj1/hello,回车即可出现网页

JavaEE-Servlet的部署和配置-LMLPHP

05-07 15:38