1.安装好之后开始创建项目
2.选择Maven类型项目,选择JDK
3.设置Maven坐标
略......
4.需要给新项目Add Framework support
5.选择Java EE项目模板
6.项目结构
修改pom.xml文件增加以下依赖
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> </dependencies>
7.配置Tomcat
8.需要注意的就是热部署问题。需要选择On frame dectivation为:Update classes and resources需要注意第一次不会有这个选项,需要先配置Deployment才可以
9.设置Deployment
10.如果需要可以设置Application context
11.运行Tomcat
12.jsp页面正确展现在浏览器中
13.新建一个Servlet
14.这里需要注意的是选中Create Java EE6 annotated class会默认创建带上注解的Serlvet,这里我们不选
不选的话会自动帮我们生成<servlet>标签,选了的话就没有了
15.在web.xml中配置Servlet
public class HelloServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); out.print(new Date().toLocaleString()); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>com.winner.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
16.部署方法