zoukankan      html  css  js  c++  java
  • Maven的学习资料收集--(四)使用Maven构建Web项目-测试

     

    目录(?)[+]

     
     

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

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

    1. <span style="font-weight: normal;">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. }</span>  

    2. 修改web.xml

    1. <pre name="code" class="html"><span style="font-weight: normal;"><?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></span>  
    
    

    3. 新建JSP

    index.jsp

    1. <span style="font-weight: normal;"><%@ 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></span>  

    login.jsp
    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

    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>  
  • 相关阅读:
    java实习生面试
    使用vue可视化界面创建vue项目
    Git操作分支
    ECharts获取Json文件数据
    Java中字符串反转
    es6新增的API
    es5新增的数组的方法
    操作字符串的相关方法
    排序方法总结
    js内置对象-Array
  • 原文地址:https://www.cnblogs.com/gxbk629/p/3891361.html
Copyright © 2011-2022 走看看