servelt tomcat

TestServlet.java

package myfirstservlet;

import java.io.IOException;
import java.net.http.HttpResponse;

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

import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;

/**
 * Servlet implementation class TestServlet
 */
@WebServlet(description = "my first servlet", urlPatterns = { "/TestServlet" })
//urlPatterns represents the path to access the class(currently in the root directory)
public class TestServlet extends HttpServlet {
	// HttpServlet class is to aid in processing HTTP-based requests
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TestServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @return 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	// doGet() for handling HTTP GET requests
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		/*
		 * Parameters: req - an HttpServletRequest object that contains the request the
		 * client has made of the servlet resp - an HttpServletResponse object that
		 * contains the response the servlet sends to the client Throws: IOException -
		 * if an input or output error is detected when the servlet handles the GET
		 * request ServletException - if the request for the GET could not be handled
		 */

//		    	httpResponse.getWriter().write(str);
		   
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	// doPost() for handling HTTP POST requests
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		/*
		 * Called by the server (via the service method) to allow a servlet to handle a
		 * POST request. The HTTP POST method allows the client to send data of
		 * unlimited length to the Web server a single time and is useful when posting
		 * information such as credit card numbers.
		 * 
		 */
		//response.getOutputStream().print("<html>");
		String account = request.getParameter("account");
		String psd = request.getParameter("psd");
		if (account.equals("admin") && psd.equals("123456")) {
			request.getRequestDispatcher("success.jsp").forward(request, response);
			// getRequestDispatcher():

		} else if (account.equals("admin") && !psd.equals("123456")) {
			request.setAttribute("psdErrorString", "password is error");
			// setAttribute():
			request.getRequestDispatcher("login.jsp").forward(request, response);

		} else if (!account.equals("admin") && psd.equals("123456")) {
			request.setAttribute("accountErrorString", "account is error");
			request.getRequestDispatcher("login.jsp").forward(request, response);

		} else {
			request.setAttribute("errorString", "login error");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
		doGet(request, response);
	}

}

login.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset="UTF-8">
<title>my fist servlet</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>

success.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset="UTF-8">
<title>my first servlet</title>
</head>
<body>
	<h1>登录成功</h1>
</body>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值