使用Maven构建Web项目-测试

来源:互联网 发布:深圳软件行业协会电话 编辑:程序博客网 时间:2024/05/20 23:33

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

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

2. 修改web.xml

[html] view plaincopy
  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. <servlet>
  8. <servlet-name>UserServlet</servlet-name>
  9. <servlet-class>com.deppon.text01.action.UserServlet</servlet-class>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>UserServlet</servlet-name>
  13. <url-pattern>/user</url-pattern>
  14. </servlet-mapping>
  15. </web-app>

3. 新建JSP

index.jsp

[java] view plaincopy
  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. <body>
  10. <p>大家好!</p>
  11. <a href="user?action=login_input">去登录</a>
  12. </body>
  13. </html>
login.jsp
[java] view plaincopy
  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. <body>
  10. <form action="user?action=login" method="post">
  11. Name:<input type="text" name="name" />
  12. Password:<input type="password" name="password" />
  13. <input type="submit" value="登录" />
  14. </form>
  15. </body>
  16. </html>

4. 测试




项目结构如下图所示:


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


2013-04-28 日修改

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

pom.xml

[html] view plaincopy
  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. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.deppon.demo</groupId>
  5. <artifactId>test01</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>test01 Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <!-- 属性配置 -->
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. </properties>
  14. <!-- 依赖配置 -->
  15. <dependencies>
  16. <!-- 添加JUnit -->
  17. <dependency>
  18. <groupId>junit</groupId>
  19. <artifactId>junit</artifactId>
  20. <version>3.8.1</version>
  21. <scope>test</scope>
  22. </dependency>
  23. <!-- 添加Servlet -->
  24. <dependency>
  25. <groupId>javax.servlet</groupId>
  26. <artifactId>servlet-api</artifactId>
  27. <version>2.5</version>
  28. <scope>provided</scope>
  29. </dependency>
  30. </dependencies>
  31. <build>
  32. <finalName>test01</finalName>
  33. </build>
  34. </project>
0 0