上一篇eclipse搭建maven web项目这里实现简单的servlet传参小Demo,项目结构:
文章图片
1.pom.xml文件加入servlet包
4.0.0
com.citywy
mytest02war
0.0.1-SNAPSHOT
junit
junit
3.8.1
test
javax.servlet
servlet-api
2.5
provided
mytest02
2.UserServlet.java
package com.citywy.servlet;
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;
public UserServlet() {
super();
} 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 name=request.getParameter("uname");
String pwd=request.getParameter("upwd");
System.out.println("输出"+name+"--"+pwd);
request.setAttribute("someinfo", "这是登陆成功提示信息!");
request.getRequestDispatcher("index.jsp").forward(request, response);
}}
3.web.xml
Archetype Created Web Application
UserServlet
UserServlet
com.citywy.servlet.UserServletUserServlet
/UserServlet
4.index.jsp
User Login - 锐客网
用户登录
运行效果如图:
文章图片
【Eclipse搭建Maven项目-servlet实例(5)】TIP:在Tomcat中运行时出现了无法加载到wtpwebapps或webapps下的情况,解决方案是servers下右击clean:将原先编译到tomcat服务器上的程序,先清除掉,然后再重新编译。
推荐阅读
- maven|Maven安装与配置
- JavaEE|Spring 创建和使用
- Spring|【Spring】Spring 更简单的读取和存储对象
- Java|JAVA常用工具收集总结
- java|基于SSM框架学习之Mybatis(一)详细步骤 附图
- 软件安装及常见配置|IDEA创建基于maven的web项目并在tomcat上部署运行
- 笔记|idea2021创建maven web项目集成tomcat
- html5|Redis优化及配置
- 探索类|快速使用 Jacoco 代码覆盖率统计