一、通过File->new->others找到Dynamic Web project,创建一个动态的Web项目.
在这里插入图片描述
二、配置项目
在这里插入图片描述
在这里插入图片描述
点击Next,继续配置。
在这里插入图片描述
点击Finish配置完成。
在这里插入图片描述
再次点击Finish,完成创建。这样就完成创建了一个简单的servlet项目。
在这里插入图片描述
以上是一个servlet项目包含的内容。
webContent 是项目根目录。


三、实现一个简单Form表单提交验证
1.在Java Resources下的src下通过new创建一个包,并在该包下创建一个servlet类

在这里插入图片描述
点击Next
在这里插入图片描述
点击Next
在这里插入图片描述
不用更改以上内容,点击Finish,完成创建servlet.自动生成以下代码.

package com.qcby.myfirstservlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TestServlet
 */
@WebServlet(description = "This is my first servlet.", urlPatterns = { "/TestServlet" })
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

  • doGet()方法用来处理通过get提交的表单信息.
  • doPost()方法用来处理通过Post提交的表单信息
  • urlPatterns 表示访问该类的路径(当前处于根目录下)

2.在WebContent下创建jsp文件,命名test.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>Insert title here</title>
</head>
<body>
<form action="TestServlet" method="post">
	账号:<input type="text" name="account" />${accountErrorString }<br>
	密码:<input type="password" name="psd" />${psdErrorString }<br>
	<input type="submit" value="提交" /><br>${errorString }
</form>
</body>
</html>

3.再创建一个test1.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>Insert title here</title>
</head>
<body>
<h1>登录成功</h1>
</body>
</html>

4.修改TestServlet.java

package com.qcby.myfirstservlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TestServlet
 */
@WebServlet(description = "我的第一个servlet", urlPatterns = { "/TestServlet" })
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String age = request.getParameter("age");
		String str = "{\"name\":\"" + name + "\",\"sex\":\"" + sex + "\",\"age\":" + age + "}";
		response.getWriter().write(str);
//		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		doGet(request, response);
		String account = request.getParameter("account");
		String psd = request.getParameter("psd");
		if(account.equals("admin") && psd.equals("123456")) {
			request.getRequestDispatcher("test1.jsp").forward(request, response);
		}else if(account.equals("admin") && !psd.equals("123456")) {
			request.setAttribute("psdErrorString", "password is error");
			request.getRequestDispatcher("test.jsp").forward(request, response);
		}else if(!account.equals("admin") && psd.equals("123456")) {
			request.setAttribute("accountErrorString", "account is error");
			request.getRequestDispatcher("test.jsp").forward(request, response);
		}else {
			request.setAttribute("errorString", "login error");
			request.getRequestDispatcher("test.jsp").forward(request, response);
		}
	}

}

5.以上就完成了一个简单的servlet项目,此时启动Tomcat服务器
在这里插入图片描述
点击红色圆圈处的按钮,重启服务器.

6.然后在浏览器地址栏输入http://localhost:8080/myfirstservlet/test.jsp
在这里插入图片描述
点击提交
在这里插入图片描述
此时浏览器地址栏变成http://localhost:8080/myfirstservlet/TestServlet

7.输入admin 123456 登录,将会显示提交成功.
在这里插入图片描述

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐