This article will introduce the sending of data to the server, and the server returns the processing results of the data to the client. This time, we will introduce the use of Get to send data to the server. The next article will introduce the use of Post to send data to the server. For those who need it, please refer to it!
The implementation method is divided into the following steps:
Step 1: Create a Web project using MyEclipse, name the project WebProject-> Create a new package with the package name com.fyt.org in the src folder
-> Create a new Servlet in the package, name the Servlet LoginServlet, and add the following 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 method public void doGet(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 into byte array//Use utf-8 to convert byte array into 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")); } } //Submit data to the server using Post method public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }Step 2: Start the Tomcat server. The way to start the Tomcat server can refer to my blog to deploy the Tomcat server on MyEclipse.
Step 3: Modify the code in index.jsp in the WebRoot directory in the WebProject project. The code in index.jsp is as follows
<%@ 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="get"> Username:<input type="text" name="username"><br> Password:<input type="password" name="password"><br> <input type="submit" value="submit"> </form> </body> </html>
Step 4: Deploy the project to the Tomcat server, the deployment method is as follows
1. Click the circled icon in the picture below
2. Select WebProject in Project and click the Add button
3. Select Tomcat 7.x in Server and click the finish button
4. Click the OK button, and the WebProject project has been successfully deployed to the Tomcat server.
Step 5: Open the browser, enter http://192.168.1.102:8080/WebProject/index.jsp in the browser, and the interface shown in the figure below shows that the data in the server has been successfully accessed.
Enter Xiaozhi in the user name and enter 123 in the password. After clicking the login button, the login successful interface pops up and indicates that the login is successful. Because the correct user name set is Xiaozhi and the correct password is 123
When the wrong password is entered into the username and password, the login will be prompted to fail
Regarding the method of submitting data to the Tomcat server using Get method, the editor will introduce so much to you, I hope it will be helpful to you!