Jbuilder2005 actual combat JSP switching control graphic steps

Time:2021-5-12

The data of the user name and password components in the form will be sent to switch.jsp of the server through HTTP request. The server encapsulates this information in the request object and sends it to switch.jsp. Therefore, switch.jsp can obtain these two values through request.getparameter (string paraname).

Copy codeThe code is as follows:
String userId = request.getParameter(“userId”);
String password = request.getParameter(“password”);

Imagine that if the form of login.jsp has more than 10 data components, the corresponding number of request.getparameter() methods must be used in switch.jsp to obtain its value. In addition, if these data are not field string types, but integers or floating-point numbers, because the value returned by the request. Getparameter() method is string, the type conversion must be carried out. This kind of work is not only tedious, but also error prone.

JSP allows you to receive web form data by bean mapping. Bean maps form data by this rule: bean attribute name = form data component name, that is, all form data fields with the same bean attribute name are automatically filled into bean, and the data type conversion is completed. For example, there are two data components in the form of login.jsp, one is called userid, and the other is password. Define a user.java bean with the same userid and password attributes. This bean can automatically receive the values of the two data components in the form.

Write user. Java

Let’s first write the bean of user.java and create user.java in the project. The code is as follows:

Code listing 7 user. Java

Copy codeThe code is as follows:
package bookstore;

public class User
{
private String userId;// User ID
private String password;// password
private String userName;// user name
public String getPassword() {
return password;
}
public String getUserId() {
return userId;
}
public String getUserName() {
return userName;
}
public void setPassword(String password) {
this.password = password;
}
public void setUserId(String userId) {
this.userId = userId;
}
public void setUserName(String userName) {
this.userName = userName;
}
}

In addition to the two attribute names of userid and password, there is also a user name attribute, user name. The value of this attribute is not received from the form of login.jsp. When the user name and password are verified to be correct, the user name and password are retrieved from the data table t_ The user name obtained from the user table is saved in this property for reference in other places, and the class is saved and compiled.

Tips:

You can use JBuilder’s bean express tool to quickly create user. Java code. In general, you should use bean express to create bean attributes. In this way, you can not only automatically generate get / set attribute access methods, but also ensure the bean naming specification.

Write page program

After creating the bean of user. Java, we start to create switch. JSP and reference the bean in switch. JSP.

Double click the JSP icon through file – > New.. – > Web – > to start the JSP creation wizard.

1. Specify the switch.jsp name


Figure 10 specifies the name of switch. JSP

Press next all the way to step 3 of the wizard.

2. Reference user.java bean


Figure 11 specifies that bean is referenced in JSP

Click Add bean… To open the select a class dialog box, where you can select the book. User class, as shown in the following figure:


Figure 12 select class as bean

Press OK to return to the dialog box in step 3 of the wizard. At this time, there is one more line of record in the bean list of the dialog box. You can specify a name for the bean in the ID column and the scope of the bean in the scope, as shown in the following figure:


Figure 13 refers to a bean

We name the bean of user as userbean and set its scope as page domain. The page domain is the page scope, which is available in the scope of the current page. When the JSP returns a response or requests to go to other JSP pages, it is not available. The other three scopes are described as follows:

· request scope: when a request is generated until the response is returned, it is valid. For example, a bean declared as request scope in a.jsp, when a.jsp jsp:forward It is still available when transferring requests to B. JSP pages.

· session scope: it is available during the user’s session cycle, which is from the time the user logs in to the time the user logs out of the system.

· application scope: this scope is the longest, indicating that the web container is valid from Startup to shutdown.

Press next to go to the next step.

3. Set the running configuration item

In the last step of the wizard, you can generate a running configuration item for the created JSP. Although the wizard sets creating a running configuration item as the default option, I don’t think it is a reasonable default value. It is recommended to cancel the create a runtime configuration setting item instead of creating a JSP running configuration item, as shown in the figure below:

Press the finish button to create the switch.jsp file. The code is as follows:

Listing 8 switch. JSP created by the wizard

Copy codeThe code is as follows:
<%@ page contentType=”text/html; charset=GBK” %>
<html>
<head>
 <title>
  switch
 </title>
</head>
<jsp:useBean scope=”page” />
<jsp:setProperty name=”userBean” property=”*” />
<body bgcolor=”#ffffff”>
 <h1>
  JBuilder Generated JSP
 </h1>
</body>
</html>

Line 8 refers to the JSP tag of the bean. Line 9 fills the attribute value of the bean with the data of the form, that is, fills the parameter of request into the attribute of the bean by name matching, and completes the type conversion (only the basic data type or constructor can complete the conversion). After executing line 9, the userid and password properties in the userbean will be set to the values of the user name and password sent from the login. JSP page.

Because switch.jsp is only used for control and does not need to display content to the client, we remove the HTML code in switch.jsp and adjust switch.jsp to:

Listing 9: switch. JSP without static HTML code

Copy codeThe code is as follows:
<%@ page contentType=”text/html; charset=GBK” %>
<jsp:useBean scope=”page” />
<jsp:setProperty name=”userBean” property=”*” />

Provide a scriptlet in switch.jsp to send the userid and password to the database and t_ The users in the user table are compared to see if they are legal users, and they turn to different pages according to the verification results. The final code of switch.jsp is as follows:

Listing 10 shows the final switch. JSP

Copy codeThe code is as follows:
<%@page contentType=”text/html; charset=GBK”%>
<%@page import=”bookstore.*”%>
<%@page import=”java.sql.*”%>
<jsp:useBean scope=”session”/>
<jsp:setProperty name=”userBean” property=”*”/>
<%
Connection conn = null;
try {
 conn = DBConnection.getConnection();
 PreparedStatement pStat = conn.prepareStatement(
  ”select USER_NAME from T_USER where USER_ID=? and password = ?”);
 pStat.setString(1, userBean.getUserId());
 pStat.setString(2, userBean.getPassword());
 ResultSet rs = pStat.executeQuery();
If (rs.next()) {/ / the password is correct
  userBean.setUserName(rs.getString(1));// Set user name
  session.setAttribute(“ses_ userBean”, userBean);// Put the userbean into the session object
%><jsp:forward page=” welcome.jsp “></jsp:forward>
<%} else {/ / bad password% >
<jsp:forward page=”fail.jsp”></jsp:forward>
<%
  }} finally {
   if(conn != null) conn.close();
  }
%>

· introduce the required classes in scriptlet code in lines 2-3.

· lines 7-14 send the query SQL statement to the database and return the result.

Line 15 indirectly judges whether the user password is correct by checking the number of records in the result set.

Line 16-18 is the response code with correct user password. First, use the user of the result set_ The name property fills in the value of the user name property of the user bean, then puts the user bean object into the session, and finally turns to the welcome. JSP page.

· when the user’s password is incorrect, there will be no record in the result set. At this time, rs.next() returns false, and the program turns to line 20. The code in line 20 turns the page to fail.jsp, where the password is input incorrectly.

· lines 22-24 are used to close the connection to the database.

Maybe you have found that although lines 9-21 will throw sqlexception exceptions, we do not have corresponding exception capture blocks. In standard Java programs, it will lead to a compile time error, but in JSP, it can be compiled in sequence. This is because the JSP page itself will catch all the exceptions thrown in the page.

Suppose there is an error in the SQL query statement in line 11, such as mistaking the user table name as user (t is correct)_ When switch.jsp is called, line 14 will throw an sqlexception. At this time, switch.jsp will display the trace information page of the exception stack trace, as shown in the following figure:


Figure 14 terrible error handling page

The error handling page shown in the figure above is grim and unfriendly. For developers, this kind of error reporting page may be suitable, because it provides a lot of error tracking information, but it is impossible for end users to accept this kind of rough error page. JSP allows you to specify a special JSP page for handling errors through <% @ page errorpage% >, so as to display errors in a friendly and intuitive form. In the next section, we’ll create a JSP page to handle errors. After that, we’ll specify the error handling JSP page for switch. JSP.

Recommended Today

Looking for frustration 1.0

I believe you have a basic understanding of trust in yesterday’s article. Today we will give a complete introduction to trust. Why choose rust It’s a language that gives everyone the ability to build reliable and efficient software. You can’t write unsafe code here (unsafe block is not in the scope of discussion). Most of […]