JSP program
First, write a JSP program to submit the form:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="Login" method="post"> username: <input type="text" name="username"><br> password: <input type="password" name="password"><br> <input type="submit" value="submit"> <input type="reset" value="reset"> </form> </body> </html>
Note that the action is the url-pattern in web.xml, (the relative path is given here), so that the corresponding servlet program can be started after submission.
Servlet program
Then, write a servlet program to display the received data:
package demo; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { processLogin(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { processLogin(req, resp); } private void processLogin(HttpServletRequest req, HttpServletResponse resp) throws IOException { String username = req.getParameter("username"); String password = req.getParameter("password"); resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.println("<html><head><title>Login Result</title></head>"); out.println("<body> username: " + username + "<br>"); out.println("password: " + password + "</body></html>"); out.flush(); } }
web.xml
Create a mapping relationship in web.xml:
<servlet> <servlet-name>LoginResult</servlet-name> <servlet-class>demo.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginResult</servlet-name> <url-pattern>/Login</url-pattern> </servlet-mapping>
Start the browser and enter: http://localhost:8050/WebApi2d/Login.jsp
After submitting, the address bar shows: http://localhost:8050/WebApi2d/Login
There is no username and password information behind. This is the POST method
Difference between get and post methods:
1. The results presented by the browser address bar are different (appearance);
2. When uploading files through a browser, you must use the post method instead of the get method.
3. Access server-side resources by entering the URL in the browser address bar, all of which are requested by the get method.