MyEclipse2014中的web项目部署到tomcat中
一、MyEclipse中的web project目录结构

二、MyEclipse配置Tomcat插件
注:MyEclipse是完全破解的
1.点击下图中Preferences

2.搜索tomcat,配置tomcat7运行环境(如果电脑中没有安装Tomcat7,则需要先安装Tomcat7服务器)
3、选择服务器运行模式,选debug模式(开发模式)


4、启动tomcat7服务器,点击start按钮,控制台有信息输出


三、创建web工程
1、创建一个新工程

2、对工程进行设置,然后点击next

3、我们应将java代码放在src中,编译后的class文件会自动放置到class文件夹中,并会在将来被部署到tomcat服务器中,点击next

4、访问路径设置
图中的1指定部署到tomcat时,应用的名称(默认与工程名一致),即将来会在tomcat下有个MyServlet的文件夹,我们访问时url是这样的http://locaolhost/MyServlet。若将该名称更改,则URL也应该做出相应的改变
图3是可选得,因为我们使用了web3.1,直接使用在java代码中使用注解技术来配置工程,或者也可以通过web.xml配置整个工程

5、编写MyServlet类,实现Servlet接口

点击next

点击Finish

1 package com.dy.xidian; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 @WebServlet(name = "HelloServlet", value = "/HelloServlet") 13 public class HelloServlet extends HttpServlet { 14 15 /** 16 * The doGet method of the servlet. <br> 17 * 18 * This method is called when a form has its tag value method equals to get. 19 * 20 * @param request 21 * the request send by the client to the server 22 * @param response 23 * the response send by the server to the client 24 * @throws ServletException 25 * if an error occurred 26 * @throws IOException 27 * if an error occurred 28 */ 29 public void doGet(HttpServletRequest request, HttpServletResponse response) 30 throws ServletException, IOException { 31 32 response.setContentType("text/html"); 33 PrintWriter out = response.getWriter(); 34 out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); 35 out.println("<HTML>"); 36 out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); 37 out.println(" <BODY>"); 38 out.print(" This is "); 39 out.print(this.getClass()); 40 out.println(", using the GET method"); 41 out.println(" </BODY>"); 42 out.println("</HTML>"); 43 out.flush(); 44 out.close(); 45 } 46 47 /** 48 * The doPost method of the servlet. <br> 49 * 50 * This method is called when a form has its tag value method equals to 51 * post. 52 * 53 * @param request 54 * the request send by the client to the server 55 * @param response 56 * the response send by the server to the client 57 * @throws ServletException 58 * if an error occurred 59 * @throws IOException 60 * if an error occurred 61 */ 62 public void doPost(HttpServletRequest request, HttpServletResponse response) 63 throws ServletException, IOException { 64 65 response.setContentType("text/html"); 66 PrintWriter out = response.getWriter(); 67 out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); 68 out.println("<HTML>"); 69 out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); 70 out.println(" <BODY>"); 71 out.print(" This is "); 72 out.print(this.getClass()); 73 out.println(", using the POST method"); 74 out.println(" </BODY>"); 75 out.println("</HTML>"); 76 out.flush(); 77 out.close(); 78 } 79 80 }
6、浏览器访问

/MySerlet是开始开始建立工程时指定的context root,即我们的应用名称。/HeollServlet是我们在该应用下的访问路径(一个应用下可以有多个Servlet)
四、热部署问题
为了解决我们修改工程之后需要重启Tomcat的问题,可以使用热部署来实现
1.修改tomcat安装目录下的context.xml文件

在context标签中加入以下内容
<Context reloadable="true">
2.修改tomcat启动参数
打开Eclipse下的Preferences,找到tomcat7的设置,修改其JDK参数


浙公网安备 33010602011771号