设为首页 收藏本站
查看: 1131|回复: 0

[经验分享] 用Maven构建Java Web开发环境(Jetty容器)之二

[复制链接]

尚未签到

发表于 2017-2-27 08:58:11 | 显示全部楼层 |阅读模式
本文接上一篇第一部分继续来介绍。
    目前为止我们还是手工命令行方式执行程序的,没有和IDE结合,其实Maven天生就对Eclipse做了集成,我们使用mvn eclipse:eclipse就得到了一个Eclipse的项目结构,在Eclipse中使用import功能就能直接导入到IDE中了。我们来看一下这个过程:
DSC0000.jpg
    此时的demo就是Eclipse项目格式的了,出现了.project和.classpath文件。我们在Eclipse中引入这个项目,此时的Eclipse没有安装Maven插件,不能自动运行Maven命令,我们来安装Maven的Eclipse插件M2E。
DSC0001.jpg
    在Eclipse的Install New Software中直接选择安装即可,非常简单。下面我们来创建Web项目并导入Eclipse中,在Jetty容器中运行程序。首先执行mvn archetype:generate命令创建。
DSC0002.jpg
    可以看到,刚创建的web项目结构包含了resources目录,而没有java代码目录,我们需要手工创建,在Eclipse中创建source folder,路径为src/main/java/src,现在我们得到如下一个项目结构,新建一个Servlet用于测试。
DSC0003.jpg
    此时,项目中没有Servlet的依赖,需要添加,我们使用m2eclipse插件来直接添加依赖,如下所示:
DSC0004.jpg
    相应的XML为:

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

    下面就可以编写Servlet了,很简单,就输出HelloWorld吧。

package org.ourpioneer.servlets;
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 HelloWorldServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.process(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.process(request, response);
}
private void process(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String title="Webapp Demo";
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
out.println("<head>");
out.println("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />");
out.println("<title>" + title + "</title>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}
}

    然后不能忘了在web.xml中配置这个Servlet,这里是Servlet 2.5的规范,不是Servlet 3,不能用注解。这也很简单。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>helloworld</servlet-name>
<servlet-class>org.ourpioneer.servlets.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/helloworld</url-pattern>
</servlet-mapping>
</web-app>

    程序都有了,剩下就是运行了,Maven既然天生和Jetty是一对儿,这里我们就使用Jetty吧,在Maven中配置Jetty,首先是webdefault.xml要准备好,它是配置Jetty的,这个可以从Jetty的包中找到,并复制到resources下,这里多说一点,默认Jetty运行时是锁定JS/CSS等静态文件的,如果想在Jetty运行时也能修改它们,要在webdefault.xml中修改如下设置:

<init-param>
<param-name>useFileMappedBuffer</param-name>
<param-value>false</param-value>
</init-param>

    Jetty也准备了,运行命令是jetty:run,这要在Maven中设置,那么需要在pom.xml中加入Jetty的插件的设置信息。这里直接贴出其整体构建信息。

<build>
<finalName>webapp</finalName>
<sourceDirectory>src/main/java/src</sourceDirectory>
<testSourceDirectory>src/test</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.1.6.v20100715</version>
<configuration>
<stopKey>stop</stopKey>
<stopPort>5599</stopPort>
<webAppConfig>
<contextPath>/</contextPath>
<defaultsDescriptor>src/main/resources/webdefault.xml</defaultsDescriptor>
</webAppConfig>
<scanIntervalSeconds>0</scanIntervalSeconds>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>80</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.7</version>
<configuration>
<addVersionToProjectName>false</addVersionToProjectName>
<useProjectReferences>false</useProjectReferences>
<encoding>UTF-8</encoding>
<wtpmanifest>false</wtpmanifest>
<wtpapplicationxml>true</wtpapplicationxml>
<wtpversion>1.5</wtpversion>
<additionalBuildcommands>
<buildcommand>org.eclipse.jdt.core.javabuilder</buildcommand>
<buildcommand>org.eclipse.wst.common.project.facet.core.builder</buildcommand>
<buildcommand>org.eclipse.wst.validation.validationbuilder</buildcommand>
</additionalBuildcommands>
<additionalProjectnatures>
<nature>org.springframework.ide.eclipse.core.springnature</nature>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
</additionalProjectnatures>
<classpathContainers>
<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
</classpathContainers>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-beta-1</version>
<configuration>
<warName>webapp</warName>
</configuration>
</plugin>
</plugins>
</build>

    此时,更新一下Maven依赖,它们就都自动下载到本地了,到这个过程结束,我们就可以在Eclipse中配置Debug运行了。配置很简单,如下。
DSC0005.jpg
    这是Debug模式运行,Run模式下是一样的,用Debug模式可以在Eclipse中断点运行程序,非常便于调试。下面我们就让它跑起来吧。运行命令是jetty:run,Base directory配置是:${workspace_loc:/应用名},启动调试,看到如下信息,Jetty就成功启动了。
DSC0006.jpg
    这里我们使用了80端口,配置方式在pom.xml中,上面的代码已经体现了。在浏览器中访问地址如下:http://localhost/helloworld,之后,我们就看到了效果。
DSC0007.jpg
    本文系作者本人的实践和探索,希望对使用者有用,欢迎交流。
(全篇完)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-347677-1-1.html 上篇帖子: eclipse maven jetty 实现热部署项目(转) 下篇帖子: jetty->请求的操作无法在使用用户映射区域打开的文件上执行
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表