一、创建web工程
- mvn archetype:generate -DgroupId=orc.cjj.site -DartifactId=orc.cjj.site.sample -DpackageName=org.cjj.site -Dversion=1.0-SNAPSHOT
mvn archetype:generate -DgroupId=orc.cjj.site -DartifactId=orc.cjj.site.sample -DpackageName=org.cjj.site -Dversion=1.0-SNAPSHOT
选择第8个类型
二、添加jetty插件
打开pom.xml添加
- <plugins>
- <!-- Configuring the Jetty Plugin -->
- <plugin>
- <groupId>org.mortbay.jetty</groupId>
- <artifactId>maven-jetty-plugin</artifactId>
- </plugin>
- </plugins>
<plugins>
<!-- Configuring the Jetty Plugin -->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>
</plugins>
三、install一下 ,install内容丰富,可以参看另外篇入门文章
- cd org.cjj.site.sample
- mvn install
cd org.cjj.site.sample
mvn install
四、启动jetty
- mvn jetty:run
mvn jetty:run
五、 浏览器输入http://localhost:8080/yoursite,是不是发现成功了?
上面的web工程只有一个jsp,来点稍微高级点的
六、生成eclipse工程
- mvn eclipse:eclipse
mvn eclipse:eclipse
然后用eclipse导入现有工程
七、添加servlet api依赖
打开pom.xml
- <!-- serlvet api -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.4</version>
- <scope>provided</scope>
- </dependency>
<!-- serlvet api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
再执行一次,给eclipse的添加Referenced Libraries
- mvn eclipse:eclipse
mvn eclipse:eclipse
八、创建一个简单的servlet
- package org.cjj.site.web;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class SimpleServlet extends HttpServlet
- {
- @Override
- protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException,
- IOException
- {
- final PrintWriter out = resp.getWriter();
- out.println("SimpleServlet Executed");
- out.flush();
- out.close();
- }
- }
package org.cjj.site.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SimpleServlet extends HttpServlet
{
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException,
IOException
{
final PrintWriter out = resp.getWriter();
out.println("SimpleServlet Executed");
out.flush();
out.close();
}
}
九、给web.xml添加servlet
- <servlet>
- <servlet-name>simple</servlet-name>
- <servlet-class>org.cjj.site.web.SimpleServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>simple</servlet-name>
- <url-pattern>/simple</url-pattern>
- </servlet-mapping>
<servlet>
<servlet-name>simple</servlet-name>
<servlet-class>org.cjj.site.web.SimpleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>simple</servlet-name>
<url-pattern>/simple</url-pattern>
</servlet-mapping>
10、编译发布
- mvn clean install
- mvn jetty:run
mvn clean install
mvn jetty:run
到此,servlet也可以访问了http://localhost:8080/yoursite/simple