2、将tomcat8.5下的webapps目录中所有内容备份到另一文件夹(bak)中,再清空webapps目录;
3、进入eclipse,配置好相应环境后,进入Servers,点击下方红色圈处,将tomcat8.5集成到eclipse中;
4.选择新建一个Apache下的Tomcat v8.5 Server--->Finish
5.双击红色圈出的Tomcat v8.5 Server at localhost,进入上方的设计页面,其余的不做更改,需要将Server Locations改选为第二项Use Tomcat installation,同时检查下方路径是否正确,最好将Deploy path改为tomcat8.5下自带的webapps,最后别忘了点击保存;
6.新建一个Dynamic Web Project(名为prj1),在Java Resources->src目录下新建一个包,名为edu.lsnu.first,在该包下新建一个HelloServlet类,编辑以下内容;
1 packageedu.lsnu.first;2
3 importjava.io.IOException;4 importjava.io.PrintWriter;5
6 importjavax.servlet.Servlet;7 importjavax.servlet.ServletConfig;8 importjavax.servlet.ServletException;9 importjavax.servlet.ServletRequest;10 importjavax.servlet.ServletResponse;11
12 public class HelloServlet implementsServlet{13
14 public static voidmain(String[] args) {15 }16
17 @Override18 public voiddestroy() {19 }20
21 @Override22 publicServletConfig getServletConfig() {23 return null;24 }25
26 @Override27 publicString getServletInfo() {28 return null;29 }30
31 @Override32 public void init(ServletConfig arg0) throwsServletException {33 }34
35 @Override36 public void service(ServletRequest request, ServletResponse response) throwsServletException, IOException {37 response.setContentType("text/html;charset=utf-8");38 PrintWriter out =response.getWriter();39 out.print("
欢迎光临
");40 out.close();41 }4243 }
7.接下来,再进入web->MEB_INF目录下,点开web.xml文件,编辑以下内容;
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 prj1
4
5 HelloServlet
6 edu.lsnu.first.HelloServlet
7
8
9
10 HelloServlet
11 /hello
12
13
14
15 index.html
16 index.htm
17 index.jsp
18
19
编译器的最终布局显示如下:
8.点击Tomcat v8.5 Server at localhost,右键选择Add and Remove...,将项目prj1添加到Tomcat中来;
9.启动服务器;
成功后会显示下图情况
10.最后在浏览器中输入http://127.0.0.1:8080/prj1/hello或http://localhost:8080/prj1/hello,回车即可出现网页