实验环境:MyEclipse 2015(自带Tomcat7.x和Maven)
实验步骤:
第一步:打开MyEclipse 2015,新建Maven工程
File→New→Other
进入下一步,使用默认值,继续下一步
按照上图中内容进行选择,继续下一步
如上图所示,输入Group Id、Artifact Id,点击finish,出现以下目录结构:
这里出错,有两个问题:
(1)JRE System Library是JDK1.5,我的MyEclipse中是1.7,自己也安装了JDK1.8,因此必须修改JRE System Library;
(2)项目存在错误,index.jsp报错:The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path
解决方法,如下图所示:
(1)修改库
(2)添加库
第二步:添加Maven约定文件夹
在Maven项目有一些约定:src/main/java , src/main/resources , src/test/java , src/test/resources文件夹必须存在。如果没有这些文件夹,那么添加这些文件夹。在MyEclipse2015中只缺少src/test/resources文件夹,添加方法如下:
双击每个文件夹的Output folder,选择路径,文件夹对应关系如下
src/main/java、src/main/resources对应target /classes;
src/test/java 、src/test/resources对应target/test-classes;
第三步:将项目转换为Dynamic Web Project
项目右键→properties→MyEclipse→Project Facets
设置部署程序集(Web Deployment Assembly)
第四步:测试
先看看,我的测试文件目录结构
在src/main/java下创建测试Servlet:UserServlet
package controller;
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 {
System.out.println("============doGet============");
doPost(request , response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
System.out.println("===========doPost=============");
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);
}
}
}
新建index.jsp
<%@ 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
<%@ 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>
修改项目部署文件web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>UserServlet</servlet-name>
<display-name>This is the display name of my J2EE component</display-name>
<description>This is the description of my J2EE component</description>
<servlet-class>controller.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/user</url-pattern>
</servlet-mapping>
</web-app>
修改pom.xml文件
<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>org.gsfan</groupId>
<artifactId>web01</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>web01 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>web01</finalName>
</build>
</project>
【测试方法】启动Servlet/JSP容器Tomcat7.x,在IE浏览器中输入一下链接:http://localhost:8080/web01/index.jsp,其他过程省略。在登录页面点击登录的时候,MyEclipse控制台会有信息输出。
【说明】以上测试代码参考:http://blog.csdn.net/yuguiyang1990/article/details/8797153