Intellij Idea社区版 上使用maven构建并使用插件jetty和tomcat运行servlet
Intellij Idea是一个不错的工具,刚开始可能有点陌生,但是使用一段时间之后就习惯了。idea有两种,IC(社区版)和IU(这个应该算是商业版),社区版是免费的,商业版是收费的,所以社区版比商业版少了一些功能,比如:JavaScript支持、java ee等,不过我们可以使用maven来安装插件来达到使得idea支持一些功能。在网上查询了一些资料和博文后总结一下。
1.使用maven安装jetty运行一个servlet
1.1:安装maven,在idea上设置maven路径之后创建一个maven的webapp项目,并创建一个servlet类,这步网上有其他详解。(我们让访问到这个servlet之后跳转到forward.jsp页面中)
forward.jsp:
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8" isELIgnored="false"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <h1>${message}</h1>
- </body>
- </html>
1.2:在pom.xml文件中安装jetty插件:
在pom.xml文件中的<plugins>节点下写入下面代码:
- <plugin>
- <groupId>org.mortbay.jetty</groupId>
- <artifactId>maven-jetty-plugin</artifactId>
- <version>6.1.26</version>
- <configuration>
- <connectors>
- <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
- <port>8080</port>
- </connector>
- </connectors>
- </configuration>
- </plugin>
- <dependency>
- <groupId>org.eclipse.jetty.orbit</groupId>
- <artifactId>javax.servlet</artifactId>
- <version>3.0.0.v201112011016</version>
- </dependency>
之后需要在web.xml中配置servlet:
- <servlet>
- <servlet-name>TestServlet</servlet-name>
- <servlet-class>com.ys.test.TestServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>TestServlet</servlet-name>
- <url-pattern>/TestServlet</url-pattern>
- </servlet-mapping>
1.3:配置运行命令:在工具栏中点击如果按钮:
点击绿色的+号之后如图:
写入名称和命令:
之后再工具栏上有这样的图标,点击运行即可:
运行部分结果:
[INFO] Starting jetty 6.1.26 ...
[INFO] jetty-6.1.26
[INFO] No Transaction manager found - if your webapp requires one, please configure one.
[INFO] Started [email protected]:8080
[INFO] Started Jetty Server
1.4:在浏览器中测试:
我们在servlet中给request赋值一个messag的属性,在forward.jsp中取得这个属性:
结果:可见我们已经成功创建并部署了一个简单的servlet应用。
2.0:安装tomcat插件:
2.1:在<plugins>节点中加上:
- <plugin>
- <groupId>org.apache.tomcat.maven</groupId>
- <artifactId>tomcat7-maven-plugin</artifactId>
- <version>2.2</version>
- <configuration>
- <port>8080</port>
- <path>/my-WebApp</path>
- <uriEncoding>UTF-8</uriEncoding>
- <server>tomcat</server>
- </configuration>
- </plugin>
步骤和安装jetty插件一样,只是在填写命令时填写:tomcat7:run
这里我们可以使用@WebServlet的注解:这时web.xml中的servlet映射可以注释掉:
- <web-app>
- <!--如果是使用jetty启动,则需要以下的servlet映射配置-->
- <!--<display-name>Archetype Created Web Application</display-name>-->
- <!--<servlet>-->
- <!--<servlet-name>TestServlet</servlet-name>-->
- <!--<servlet-class>com.ys.test.TestServlet</servlet-class>-->
- <!--</servlet>-->
- <!--<servlet-mapping>-->
- <!--<servlet-name>TestServlet</servlet-name>-->
- <!--<url-pattern>/TestServlet</url-pattern>-->
- <!--</servlet-mapping>-->
- </web-app>
servlet类:
@WebServlet("/test") public class TestServlet extends HttpServlet {
最后我们一个在maven依赖的servlet中加上:
- <dependency>
- <groupId>org.eclipse.jetty.orbit</groupId>
- <artifactId>javax.servlet</artifactId>
- <version>3.0.0.v201112011016</version>
- <span style="color:#ff0000;"><scope>provided</scope>//我们在打包是不需要这个依赖包</span>
- </dependency>
运行部分结果:
[INFO] create webapp with contextPath: /my-WebApp
九月 23, 2016 12:39:15 上午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-bio-8080"]
九月 23, 2016 12:39:15 上午 org.apache.catalina.core.StandardService startInternal
信息: Starting service Tomcat
九月 23, 2016 12:39:15 上午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.47
九月 23, 2016 12:39:24 上午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]
测试结果:
这样我们完成了jetty和tomcat安装过程和测试,希望能对您有用。