JSP simple data interaction

Hello everyone!


Preface

Hello everyone! I have been learning JSP technology recently, and in my spare time I would like to share with you the simple data interaction of JSP.

1. What is JSP?

Before we learn any new technology, we must know what the technology is, what we can use it for, and what functions we can achieve. Then the full name of JSP is Java Server Pages, and the Chinese name is java server page. The program running on the server side is basically a simplified Servlet design. Simply put, JSP embeds Java script code in HTML and runs it on the server.

2. Data interaction

1.HTML code

The front-end code represents what the user can see, that is, the html code. Here we create a login.jsp file and write a simple form in it. The code is as follows:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="control.jsp" method="post">
        <p>姓名:<input type="text" name="userName"></p>
        <p>密码:<input type="password" name="pwd"></p>
        <p><input type="submit" value="登录"></p>
    </form>
</body>
</html>

Here we should pay attention to the two attributes that must be written in the form form. One is action , which represents where to submit the value of the attribute in the form, and the other is method , which is an encryption method. Here we use post to be relatively safe. . In addition, the input input box needs to be set with a name value, so as to ensure that we can get the corresponding value in the background. We run the code on the Tomcat server, the code runs as follows:

 

2.Java code

With the previous basic style, we can write Java code, we need to write Java code into another file, here we create a new file named control.jsp. code show as below:

<%
    request.setCharacterEncoding("utf-8");
    String name = request.getParameter("userName");
    String pwd = request.getParameter("pwd");
    if("admin".equals(name) && "123".equals(pwd)){
          out.print("登录成功");
    }else{
        out.println("登录失败");
    }
%>

Here we need to pay attention to the following points:

1. When writing Java code in a jsp file, it needs to be written in <%%>.

2. Here we need to use the request object among the nine built-in objects of JSP. The request object can be used to obtain the form submission data, but the data may be garbled during the data submission process. The reason is that the JSP page encoding format does not support Chinese. Here we need to write request.setCharacterEncoding("UTF-8"); on the first line to prevent us from garbled characters.

3. To accept the value of the input box, you need to use the getParameter method of the request object. Here we always use the String type to receive it.

Finally, we can compare the values.

If we want to judge the successful jump to a new page, we only need to slightly modify the code in the if, here we have two ways to jump to the new page, one is still using the request object (request forwarding) Another method is to use the response object (redirection) of the nine built-in objects of JSP. They both have the same purpose, but there is a little difference. Here I summarize the following points:

1. The request forwarding speed is fast, but the redirection speed is slow.

2. Request forwarding is the same request, but redirection is two different requests.

3. The URL address bar will not change after the request is forwarded, and the redirection will become the new address of the request.

They are written as follows:

if("admin".equals(name) && "123".equals(pwd)){
        //请求转发:登录成功,main.jsp
        request.getRequestDispatcher("main.jsp").forward(request,response);
    }else{
        //重定向:失败,login.jsp
        response.sendRedirect("login.jsp");
    }

Summarize

The above is what I want to share today. This article only briefly introduces the simple data interaction of JSP. I hope it can be useful to everyone. If you like it, don’t forget to like it! thanks for watching! !

Guess you like

Origin blog.csdn.net/m0_66403070/article/details/127820384