(四)- 使用Maven构建Web项目-测试
最后更新于:2022-04-01 20:44:45
在上一篇博客里,我们使用Maven构建了一个Web项目,我们在这里写一个简单的Servlet,测试一下。
### 1.在src/main/java下,新建一个Servlet
~~~
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
~~~
UserServlet
com.deppon.text01.action.UserServlet
UserServlet
/user
~~~
### 3. 新建JSP
index.jsp
~~~
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Hello Maven
登录界面
~~~
### 4. 测试
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-09-06_57ce64fb49091.png)
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-09-06_57ce64fb625f8.png)
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-09-06_57ce64fb79f29.png)
项目结构如下图所示:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-09-06_57ce64fb8cb56.png)
其实,构建完成之后,开发的话,应该和平时开发Web项目是一样的。
2013-04-28 日修改
之前忘记说明pom文件了,需要添加依赖的:
pom.xml
~~~
4.0.0
com.deppon.demo
test01
war
0.0.1-SNAPSHOT
test01 Maven Webapp
http://maven.apache.org
UTF-8
junit
junit
3.8.1
test
javax.servlet
servlet-api
2.5
provided
test01
~~~
很抱歉,之前忘记写了.
ps:希望之前看过的朋友再看一下哦
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- [Maven学习(一)- 环境搭建](http://blog.csdn.net/jolingogo/article/details/8775046)
- [Maven学习(二)- 安装m2eclipse插件 ](http://blog.csdn.net/jolingogo/article/details/8796410)
- [Maven学习(三)- 使用Maven构建Web项目](http://blog.csdn.net/jolingogo/article/details/8796726)
- [Maven学习(四)- 使用Maven构建Web项目-测试](http://blog.csdn.net/jolingogo/article/details/8797153)
- [Maven学习(五)- 使用Maven构建Struts2项目](http://blog.csdn.net/jolingogo/article/details/8798052)
- [Maven学习(六)- 构建Hibernate项目](http://blog.csdn.net/jolingogo/article/details/8798684)
- [Maven学习(七)- 构建Spring项目](http://blog.csdn.net/jolingogo/article/details/8799307)
- [Maven学习(八)- 构建MyBatis项目](http://blog.csdn.net/jolingogo/article/details/8801158)
- [Maven学习(九)- 构建SSH项目](http://blog.csdn.net/jolingogo/article/details/8811817)
- [Maven学习(十) - 阶段小结
](http://blog.csdn.net/jolingogo/article/details/8821375)
- [专栏:Maven学习之旅](http://blog.csdn.net/column/details/yuguiyang-maven.html)
';
大家好!
去登录 ~~~ login.jsp ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>