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>