Simple writing method of JavaBean landing page (three methods)

The first one: if successful, output directly on this page

requirement:

For the example of JavaBean, first write a login JavaBean, and then write a login html page (login.html). The submitted processing page (login.jsp) applies the login JavaBean to obtain the value of the form. If the user name and password are correct (for example, the user name is equal to QQ and the password is equal to 123), the page jumps to the success page, otherwise it jumps to the login page (login.html).

1. First, we need to store the user information Java, because I didn't write the registration information here, it can be omitted

public class User {
private String name;
private String password;

public void setName(String name) {
this.name=name;
}
public String getName (){
return name;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
}

Explanation: considering that there may be students without java foundation, the setName here is actually the value of name passed into the database. Then we need to read the data, so we need to get the return value of the function, the user here java actually defines these two functions

2. Login html

<html lang="en">
<head>
<meta charset="utf-8">
<title>login interface</title>
</head>
<body>
<div align="center">
<font size="10px" color="#00008b "> user login < / font >
<form action="login.jsp" method="get">
<table border="2">
<tr>
<th>user name</th>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<th>password</th>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Sign in"/>
<input type="reset" />
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
explain:The most important thing here**a key**That's the first three codes

```html
<form action="login.jsp" method="get">

Note: the value of name cannot be taken arbitrarily. It needs to be followed by user The name in Java and the next login JSP is the same!!!

<th>user name</th>
<td><input type="text" name="name"/></td>
<th>password</th>
<td><input type="password" name="password"/></td>

The meaning of this HTML code is to set the login and reset functions. If submit, it will be directly sent to login jsp in this jsp file

<input type="submit" value="Sign in"/>
<input type="reset" />

3. login.jsp handles the passed value

<%@ page contentType="text/html" pageEncoding="utf-8" %>
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Data landing page</title>
</head>
<body>
<%
String name=request.getParameter("name");
String password=request.getParameter("password");
if(name.equals("qq")&&password.equals("123")){
%>
<%="Login successful, welcome"%>
<%=request.getParameter("name")%>
<%
}else{
%>
<jsp:forward page='login.html'>
<jsp:param name='user' value="<%=name%>"/>
</jsp:forward>
<%
}
%>
</body>
</html>

Explanation:

  • java script code needs to use the <%% > symbol. This script body means to accept login HTML value
  • If there is Chinese in the request, it may lead to Chinese garbled code. Therefore, the UTF-8 encoding method needs to be specified on the initial header
  • In java, you can't directly = = whether two strings are equal. You need to use the equals() method
<%
String name=request.getParameter("name");
String password=request.getParameter("password");
if(name.equals("qq")&&password.equals("123")){
%>
  • If the two requirements in if are met at the same time, the name filled in by the user name will be returned on the page
  • It should be noted that if it is outputting jsp code, it will not be followed; The symbol of is: <% =% >
<%="Login successful, welcome"%>
<%=request.getParameter("name")%>
  • If the two requirements in if are not met at the same time, it will jump to HTML again for re input
  • Jsp: the code after the forward action instruction will not be executed.
  • jsp:param action is used to decorate
<jsp:forward page='login.html'>
<jsp:param name='user' value="<%=name%>"/>
</jsp:forward>

Here are the results of the operation:

Second: if you succeed, jump to a new page

The only difference from the first one is in login Just change the successful jump page in JSP~

<%
String name=request.getParameter("name");
String password=request.getParameter("password");
if(name.equals("qq")&&password.equals("123")){
%>
<jsp:forward page='success.jsp'>
<jsp:param name="user" value="<%=name%>"/>
</jsp:forward>
<%
}else{
%>
<jsp:forward page='login.html'>
<jsp:param name='user' value="<%=name%>"/>
</jsp:forward>
<%
}
%>

of course. success.jsp also needs to be written

<%@ page contentType="text/html" pageEncoding="utf-8" %>
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>User login success page</title>
</head>
<body>
Login successful
<br>
Welcome,<%=request.getParameter("name")%>
</body>
</html>

Third: if you succeed, jump to other pages

Just redirect ~ response senRedirect(" path")

<%
String name=request.getParameter("studentNumber");
String password=request.getParameter("password");
if(name.equals("jwzx")&&password.equals("jwzx")){
    response.sendRedirect("http://jwc.cqupt.edu.cn/");
}else{
 %>
 <jsp:forward page='login.html'>
 <jsp:param name='user' value="<%=name%>"/>
 </jsp:forward>
<%
}
%>

Keywords: Web Development JSP

Added by jonabomer on Tue, 01 Feb 2022 15:42:18 +0200