In my previous article, I introduced using Get to submit data to the Tomcat server. This article will introduce using Post to submit data to the server. Since the Post method is exactly the same as the Get method to create a web project, the code in only a few places is different, so I will directly introduce different places. The first difference is that the submission method is different, so I modify the code in LoginServlet.Java
package com.fyt.org; import java.io.IOException; import java.io.OutputStream; 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 { public LoginServlet() { super(); } public void destroy() { super.destroy(); } //Submit data to the server using Get public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } //Submit data to the server using Post public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Get the username sent from the browser String username = request.getParameter("username"); //Get the password sent from the client String password = request.getParameter("password"); //Use iso8859-1 encoding to convert username to byte array//Use utf-8 to convert the byte array to string Username = new String(username.getBytes("iso8859-1"), "utf-8"); //Print the username and password in the console System.out.println("username=" + username); System.out.println("password=" + password); //Get an output stream OutputStream os = response.getOutputStream(); //If both username and password are entered correctly if("Xiaozhi".equals(username) && "123".equals(password)) { //Send characters to the browser os.write("Login successfully".getBytes("utf-8")); } else { //Send string to the browser os.write("Login failed".getBytes("utf-8")); } } }The second place that needs to be modified is index.jsp. Modify the code in index.jsp into the following code
<%@ 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 'index.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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="servlet/LoginServlet" method="post"> Username:<input type="text" name="username"><br> Password:<input type="password" name="password"><br> <input type="submit" value="submit"> </form> </body> </html>
After the modification is completed, the project will be deployed on the Tomcat server. The deployment method can refer to my blog to submit data to the Tomcat server using Get method. After the deployment is completed, enter http://192.168.1.102:8080/WebProject/index.jsp in the browser. When the interface shown in the figure below is displayed in the browser, it means that the project has been successfully deployed to the browser.
Enter Xiaozhi in the username and 123 in the password. When the login is successful in the browser, it means that the login is successful, because the correct username I set in the server is Xiaozhi, and the correct password is 123
Login failed when there is an error in the username or password
I will introduce so much to you about how to submit data to the Tomcat server using Post method, I hope it will be helpful to you!