实验环境: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"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello Maventitle>
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"%>
<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
<web-app>
<display-name>Archetype Created Web Applicationdisplay-name>
<servlet>
<servlet-name>UserServletservlet-name>
<display-name>This is the display name of my J2EE componentdisplay-name>
<description>This is the description of my J2EE componentdescription>
<servlet-class>controller.UserServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>UserServletservlet-name>
<url-pattern>/userurl-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.0modelVersion>
<groupId>org.gsfangroupId>
<artifactId>web01artifactId>
<packaging>warpackaging>
<version>0.0.1-SNAPSHOTversion>
<name>web01 Maven Webappname>
<url>http://maven.apache.orgurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
<scope>providedscope>
dependency>
dependencies>
<build>
<finalName>web01finalName>
build>
project>
【测试方法】启动Servlet/JSP容器Tomcat7.x,在IE浏览器中输入一下链接:http://localhost:8080/web01/index.jsp,其他过程省略。在登录页面点击登录的时候,MyEclipse控制台会有信息输出。
【说明】以上测试代码参考:http://blog.csdn.net/yuguiyang1990/article/details/8797153