Today is the second day of JavaWeb learning. I mainly focus on the advanced functions of Servlets, focusing on form processing, request forwarding and redirecting, and how to achieve simple user interaction through Servlets. These contents gave me a deeper understanding of JavaWeb development. In the morning, I learned how to process form data through Servlets. In web development, forms are an important way for users to interact with servers, and Servlets can obtain the data submitted by form through the HttpServletRequest object. For example, a simple user registration form might contain fields such as username, password, and mailbox. With the () method, I can easily get the values of these fields. Here is a simple example code: java@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = ("username");
String password = ("password");
String email = ("email");
// Simulate user registration logic
if (username != null && password != null) {
().write("Register successfully! Welcome," + username);
} else {
().write("Register failed, please fill in the complete information!");
}
}
In practice, I noticed the coding problem of form data. By default, the form data submitted by the browser is encoded as ISO-8859-1, while the server side may use UTF-8 encoding. If no encoding conversion is performed, it may lead to Chinese garbled code. The solution is to set the encoding method of the request before obtaining the parameters: ("UTF-8");
At the same time, it is also necessary to ensure that the coding method of the response is consistent with the page to avoid garbled code: ("text/html;charset=UTF-8");
In the afternoon, I learned two ways to jump pages, request forwarding and redirecting. Request forwarding is done internally by the server, and the client cannot sense the jump process. For example, after a user submits a form, the Servlet can forward the request to a result page: javaRequestDispatcher dispatcher = ("/");
(request, response);
Redirection is done by the client. The server will send a status code (such as 302) and a new URL address, and the client will initiate a new request. For example:("/");
These two methods have their own advantages and disadvantages: Request forwarding can share data in the request domain, but can only jump to the page under the same domain; redirecting can jump to any URL, but cannot share data in the request domain. To deepen my understanding, I wrote a simple login function. The user submits the user name and password on the login page. After Servlet verification, it will forward to the home page if it is successful. Otherwise, it will be redirected back to the login page and prompt for an error message. Here is some code: java@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = ("username");
String password = ("password");
if ("admin".equals(username) && "123".equals(password)) {
("message", "Login successfully!");
RequestDispatcher dispatcher = ("/");
(request, response);
} else {
("/?error=true");
}
}
In the evening, I summarized today's learning content. Through form processing, I mastered how to obtain data from the client and solved common coding problems; through request forwarding and redirecting, I understood the principles and applicable scenarios of two page jump methods. This knowledge lays the foundation for subsequent development of complex web applications. Tomorrow, I plan to learn JSP technology to understand how to realize dynamic display of pages through JSP. I hope to further improve my JavaWeb development capabilities in practice.