• IDEA中使用Tomcat-Tomcat Maven插件
○ Pom.xml添加Tomcat插件
< build> < plugins> < plugin> < groupId> org. apache. tomcat. maven< / groupId> < artifactId> tomcat7- maven- plugin< / artifactId> < version> 2.2 < / version> < ! -- < configuration> < port> 80 < / port> < path> / < / path> < / configuration> -- > < / plugin> < plugin> < groupId> org. apache. maven. plugins< / groupId> < artifactId> maven- compiler- plugin< / artifactId> < configuration> < source> 6 < / source> < target> 6 < / target> < / configuration> < / plugin> < / plugins> < / build>
○ 使用Maven Helper插件快速启动项目,选中项目,右键-->Run Maven-->tomcat7:run
• Servlet快速入门
○ 创建web项目,导入Servlet依赖坐标<dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version><scope>provided</scope></dependency>○ 创建:定义一个类,实现Servlet接口,并重写接口中所有方法,并在service方法中输入一句话○ 配置:在类上使用@WebServlet注解,配置该Servlet的访问路径@WebServlet("/demo1")○ 访问:启动Tomcat,浏览器输入URL访问该Servlethttp://localhost:8080/web-demo/demo1
import javax. servlet. *;
import javax. servlet. annotation. WebServlet;
import java. io. IOException;
@WebServlet ( value = "/demo1" )
public class ServletDemo implements Servlet { @Override public void init ( ServletConfig servletConfig) throws ServletException { } @Override public ServletConfig getServletConfig ( ) { return null; } @Override public void service ( ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { System. out. println ( "Servlet hello world" ) ; } @Override public String getServletInfo ( ) { return null; } @Override public void destroy ( ) { }
}
选中项目,右键-->Run Maven-->tomcat7:run 启动Tomcat,浏览器输入URL访问该Servlet
访问完成后可以看到控制台输出了一句话