您的位置:首页 > 其它

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

2015-06-13 12:33 453 查看
在上一篇博客里,我们使用Maven构建了一个Web项目,我们在这里写一个简单的Servlet,测试一下。


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

[java] view
plaincopy

package com.deppon.text01.action;  

  

import java.io.IOException;  

import javax.servlet.ServletException;  

import javax.servlet.http.HttpServlet;  

import javax.servlet.http.HttpServletRequest;  

import javax.servlet.http.HttpServletResponse;  

  

public class UserServlet extends HttpServlet {  

    private static final long serialVersionUID = 1L;  

      

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  

        doPost(request , response);  

    }  

      

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  

        request.setCharacterEncoding("UTF-8");  

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

          

        String action = request.getParameter("action");  

        if("login_input".equals(action)) {  

            request.getRequestDispatcher("login.jsp").forward(request , response);  

        } else if("login".equals(action)) {  

            String name = request.getParameter("name");  

            String password = request.getParameter("password");  

              

            System.out.println("name->" + name + ",password->" + password);  

        }  

    }  

  

}  


2. 修改web.xml

[html] view
plaincopy

<?xml version="1.0" encoding="UTF-8"?>  

<web-app version="3.0"   

    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_3_0.xsd">  

    

  <servlet>  

    <servlet-name>UserServlet</servlet-name>  

    <servlet-class>com.deppon.text01.action.UserServlet</servlet-class>  

  </servlet>  

  <servlet-mapping>  

    <servlet-name>UserServlet</servlet-name>  

    <url-pattern>/user</url-pattern>  

  </servlet-mapping>  

    

</web-app>  


3. 新建JSP

index.jsp

[java] view
plaincopy

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

    pageEncoding="UTF-8"%>  

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

<html>  

    <head>  

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

        <title>Hello Maven</title>  

    </head>  

      

    <body>  

        <p>大家好!</p>  

        <a href="user?action=login_input">去登录</a>  

    </body>  

</html>  

login.jsp

[java] view
plaincopy

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

    pageEncoding="UTF-8"%>  

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

<html>  

    <head>  

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

    <title>登录界面</title>  

    </head>  

      

    <body>  

        <form action="user?action=login" method="post">  

            Name:<input type="text" name="name" />  

            Password:<input type="password" name="password" />  

              

            <input type="submit" value="登录" />  

        </form>  

    </body>  

</html>  


4. 测试







项目结构如下图所示:



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

2013-04-28 日修改

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

pom.xml

[html] view
plaincopy

<project xmlns="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.0 http://maven.apache.org/maven-v4_0_0.xsd">  

   

  <modelVersion>4.0.0</modelVersion>  

  <groupId>com.deppon.demo</groupId>  

  <artifactId>test01</artifactId>  

  <packaging>war</packaging>  

  <version>0.0.1-SNAPSHOT</version>  

  <name>test01 Maven Webapp</name>  

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

    

  <!-- 属性配置 -->  

  <properties>  

      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  

  </properties>  

  

  <!-- 依赖配置 -->       

  <dependencies>  

    <!-- 添加JUnit -->  

    <dependency>  

      <groupId>junit</groupId>  

      <artifactId>junit</artifactId>  

      <version>3.8.1</version>  

      <scope>test</scope>  

    </dependency>  

      

    <!-- 添加Servlet -->  

    <dependency>    

        <groupId>javax.servlet</groupId>    

        <artifactId>servlet-api</artifactId>    

        <version>2.5</version>    

        <scope>provided</scope>    

    </dependency>    

      

  </dependencies>  

    

  <build>  

    <finalName>test01</finalName>  

  </build>  

    

</project>  

很抱歉,之前忘记写了.

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

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Maven学习(一)- 环境搭建
Maven学习(二)- 安装m2eclipse插件 
Maven学习(三)- 使用Maven构建Web项目
Maven学习(四)- 使用Maven构建Web项目-测试
Maven学习(五)- 使用Maven构建Struts2项目
Maven学习(六)- 构建Hibernate项目
Maven学习(七)- 构建Spring项目
Maven学习(八)- 构建MyBatis项目
Maven学习(九)- 构建SSH项目
Maven学习(十) - 阶段小结

专栏:Maven学习之旅
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: