maven+servlet3.0显示当前时间

Myeclipse:Java1.7+Tomcat8.5+maven+servlet3.0简易示例

整个项目预览图:

1.      新建maven程序:


2.      点下一步:


点完成,新建项目完成。

 

3.      进入build path


将两个错误的删除。

4.      添加maven要求的必须的四个目录(file->new->source Folder)


5.      进入build  path  设定4个文件夹的输出Output folder,双击修改


分别修改输出路径为

src/main/resources  对应  target/classes

src/main/java  对应  target/classes

src/test/resources  对应  target/test-classes

src/test/java  对应  target/test-classes

6.      修改后的文件


7.   改变LibrariesDynamic Web Projec


点击项目,右键进入properties,双击Myeclipse,选择property faces,点击ok

8.      至此,程序就不报错了。之后配置pom.xml,引入servlet3.0依赖

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd">

 <modelVersion>4.0.0</modelVersion>

  <groupId>com</groupId>

 <artifactId>test2</artifactId>

 <packaging>war</packaging>

 <version>0.0.1-SNAPSHOT</version>

 <name>test2 Maven Webapp</name>

 <url>http://maven.apache.org</url>

 <dependencies>

   <dependency>

     <groupId>junit</groupId>

     <artifactId>junit</artifactId>

     <version>3.8.1</version>

     <scope>test</scope>

   </dependency>

     <!--要求Maven下载Servlet3.0jar --> 

       <dependency> 

           <groupId>javax.servlet</groupId> 

           <artifactId>javax.servlet-api</artifactId> 

           <version>3.0.1</version> 

       </dependency> 

 </dependencies>

 <build>

   <finalName>test2</finalName>

 </build>

</project>

9.      这时候,系统自动将servlet3.0依赖包下载到了项目中。配置servlet3.0的web.xml

<?xmlversion="1.0" encoding="UTF-8"?> 

<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

   xmlns="http://java.sun.com/xml/ns/javaee" 

   xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 

    version="3.0"> 

</web-app>

10.  创建index.jsp

<%@page language="java" contentType="text/html;charset=UTF-8" 

    pageEncoding="UTF-8"%> 

<!DOCTYPEhtml PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> 

<html> 

<head> 

<metahttp-equiv="Content-Type" content="text/html;charset=UTF-8"> 

<title>Hello</title> 

</head> 

<body> 

Helloworld! 

<ahref="./hello">Hello</a> 

</body> 

</html> 

11. 再于src/main/java新建一个hello.java,这是一个普通类,不用servlet创建,此时已经不需要再web.xml中配置路径,用注解代替了。如下
package com.test; 

 

import java.io.*; 

import javax.servlet.*; 

import javax.servlet.http.*; 

import javax.servlet.annotation.*; 

 

@WebServlet(name="hello",urlPatterns={"/hello"}) 

public class hello extends HttpServlet{ 

     

   private static final long serialVersionUID = 1L; 

 

   protected void service(HttpServletRequest request, 

           HttpServletResponse response) throws ServletException, IOException{ 

       PrintStream out = new PrintStream(response.getOutputStream()); 

       response.setContentType("text/html;charSet=utf-8"); 

       out.print("hello world"); 

   } 

 

12.把工程发布到Tomcat中运行,运行结果如下:


点击hello超链接进入:


至此,一个完整的servlet3.0的maven程序搭建成功,可以尝试高级功能了。大笑大笑

       本内容由安康学院"雨季"原创。

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值