Maven学习(四)- 使用Maven构建Web项目-测试

Maven学习(四)- 使用Maven构建Web项目-测试

分类: Maven3   1867人阅读  评论(13)  收藏  举报

目录(?)[+]

在上一篇博客里,我们使用Maven构建了一个Web项目,我们在这里写一个简单的Servlet,测试一下。

1.在src/main/java下,新建一个Servlet

[java]  view plain copy
  1. package com.deppon.text01.action;  
  2.   
  3. import java.io.IOException;  
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.http.HttpServlet;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8.   
  9. public class UserServlet extends HttpServlet {  
  10.     private static final long serialVersionUID = 1L;  
  11.       
  12.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  13.         doPost(request , response);  
  14.     }  
  15.       
  16.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  17.         request.setCharacterEncoding("UTF-8");  
  18.         response.setContentType("text/html;charset=utf-8");  
  19.           
  20.         String action = request.getParameter("action");  
  21.         if("login_input".equals(action)) {  
  22.             request.getRequestDispatcher("login.jsp").forward(request , response);  
  23.         } else if("login".equals(action)) {  
  24.             String name = request.getParameter("name");  
  25.             String password = request.getParameter("password");  
  26.               
  27.             System.out.println("name->" + name + ",password->" + password);  
  28.         }  
  29.     }  
  30.   
  31. }  

2. 修改web.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="3.0"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
  7.     
  8.   <servlet>  
  9.     <servlet-name>UserServlet</servlet-name>  
  10.     <servlet-class>com.deppon.text01.action.UserServlet</servlet-class>  
  11.   </servlet>  
  12.   <servlet-mapping>  
  13.     <servlet-name>UserServlet</servlet-name>  
  14.     <url-pattern>/user</url-pattern>  
  15.   </servlet-mapping>  
  16.     
  17. </web-app>  

3. 新建JSP

index.jsp

[java]  view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5.     <head>  
  6.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7.         <title>Hello Maven</title>  
  8.     </head>  
  9.       
  10.     <body>  
  11.         <p>大家好!</p>  
  12.         <a href="user?action=login_input">去登录</a>  
  13.     </body>  
  14. </html>  
login.jsp

[java]  view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5.     <head>  
  6.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7.     <title>登录界面</title>  
  8.     </head>  
  9.       
  10.     <body>  
  11.         <form action="user?action=login" method="post">  
  12.             Name:<input type="text" name="name" />  
  13.             Password:<input type="password" name="password" />  
  14.               
  15.             <input type="submit" value="登录" />  
  16.         </form>  
  17.     </body>  
  18. </html>  

4. 测试




项目结构如下图所示:


其实,构建完成之后,开发的话,应该和平时开发Web项目是一样的。


2013-04-28 日修改

之前忘记说明pom文件了,需要添加依赖的:

pom.xml

[html]  view plain copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.    
  4.   <modelVersion>4.0.0</modelVersion>  
  5.   <groupId>com.deppon.demo</groupId>  
  6.   <artifactId>test01</artifactId>  
  7.   <packaging>war</packaging>  
  8.   <version>0.0.1-SNAPSHOT</version>  
  9.   <name>test01 Maven Webapp</name>  
  10.   <url>http://maven.apache.org</url>  
  11.     
  12.   <!-- 属性配置 -->  
  13.   <properties>  
  14.       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.   </properties>  
  16.   
  17.   <!-- 依赖配置 -->       
  18.   <dependencies>  
  19.     <!-- 添加JUnit -->  
  20.     <dependency>  
  21.       <groupId>junit</groupId>  
  22.       <artifactId>junit</artifactId>  
  23.       <version>3.8.1</version>  
  24.       <scope>test</scope>  
  25.     </dependency>  
  26.       
  27.     <!-- 添加Servlet -->  
  28.     <dependency>    
  29.         <groupId>javax.servlet</groupId>    
  30.         <artifactId>servlet-api</artifactId>    
  31.         <version>2.5</version>    
  32.         <scope>provided</scope>    
  33.     </dependency>    
  34.       
  35.   </dependencies>  
  36.     
  37.   <build>  
  38.     <finalName>test01</finalName>  
  39.   </build>  
  40.     
  41. </project>  

很抱歉,之前忘记写了.

ps:希望之前看过的朋友再看一下哦

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值