Eclipse搭建Maven项目-servlet实例(5)

上一篇eclipse搭建maven web项目这里实现简单的servlet传参小Demo,项目结构:
Eclipse搭建Maven项目-servlet实例(5)
文章图片

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 - 锐客网 用户登录
用户名:
密码:
${someinfo }

运行效果如图:
Eclipse搭建Maven项目-servlet实例(5)
文章图片

【Eclipse搭建Maven项目-servlet实例(5)】TIP:在Tomcat中运行时出现了无法加载到wtpwebapps或webapps下的情况,解决方案是servers下右击clean:将原先编译到tomcat服务器上的程序,先清除掉,然后再重新编译。

    推荐阅读