js| Control Because the switch.jsp is specified as the response JSP file in the Login.jsp form, when the user selects the logged-on user in login.jsp, the client sends an HTTP request to the server after the input password submits the form, and the server calls switch.jsp to respond to the request.
The user name and password in the form the data for both components will be passed to the server's switch.jsp via an HTTP request, and the server encapsulates the information in the request object to switch.jsp, so switch.jsp can pass Request.getparameter ( String Paraname) To get the two values.
String userId = Request.getparameter ("UserId");
String Password = request.getparameter ("password");
Imagine that if a login.jsp form has more than 10 data components, the value must be obtained from the corresponding number of request.getparameter () methods in switch.jsp. In addition, if the data is not a field string type, but an integer or floating-point number, because the value returned by the Request.getparameter () method is string, a type conversion is required, which is tedious and error-prone.
JSP allows you to receive data from a Web form in a mapped way through the bean, the bean maps the form's data with this rule: the Bean property name = The form data component name, or all form data fields that are the same as the Bean property name are automatically populated into the bean and the data type conversion is completed. For example, there are two data components in a login.jsp form, one named UserID, and the other is password, which defines a password Bean with the same name UserID and User.java properties. This bean will automatically receive two data component values from the form.
Write User.java
Let's start by writing this user.java Bean, creating User.java in the project, and the code looks like this:
Code Listing 7 User.java
1. Package bookstore;
2.
3. public class User
4. {
5. Private String userid;//User ID
6. Private String password;//Password
7. Private String username;//User name
8. Public String GetPassword () {
9. return password;
10.}
One. Public String GetUserID () {
return userId;
13.}
Public String GetUserName () {
return userName;
16.}
public void SetPassword (String password) {
This.password = password;
19.}
public void Setuserid (String userId) {
This.userid = userId;
22.}
public void Setusername (String userName) {
This.username = UserName;
25.}
26.}
In addition to the UserID and password two property names, there is also a Username property username, the value of this property is not received from the login.jsp form, and when the user name password is validated correctly, the user name is saved from the Datasheet t_user table in this property so that other places can refer to , save and compile this class.
Tips:
You can quickly create User.java code using the JBuilder Bean Express tool, and in general you should create the bean's properties through Bean Express, which not only automatically generates Get/set property access methods, The bean naming specification is also guaranteed. Writing a page program
After creating the User.java bean, we proceeded to create the switch.jsp and reference the Bean in switch.jsp.
Through File->new. ->web-> Double-click the JSP icon to start the Create JSP Wizard.
1. Specify swith.jsp Name
Figure 10 Specifies the name of the switch.jsp
Press Next to the 3rd step of the wizard.
2. Referencing User.java Beans
Figure 11 Specifying a reference bean in a JSP
Click Add Bean ... button, pop the Select a class dialog box and select Bookstore in the dialog box. The user class, as shown in the following illustration:
Figure 12 Selecting a class as a bean
After you press OK, return to the dialog box in step 3rd of the wizard, where you can specify a name for the bean in the ID bar with a single row in the Bean List of the dialog box, and specify the scope of the bean in the scope, as shown in the following illustration:
Figure 13 refers to a bean
We name the user's Bean Userbean and set its scope to the page field. The page field is a sheet scope that is available within the current page-range scope, when the JSP returns a response, or when the request is transferred to another JSP page, the other 3 scopes are described as follows:
Request scope: When a request is generated until a response is returned, such as a bean declared as the request scope in a.jsp, is available when a.jsp is in the <jsp:forward> transfer request to the B. JSP page.
Session scope: is available during the user session's cycle, and the session cycle is for the user to log on to the system until it exits the system.
Application scope: This scope is the longest, indicating that the Web container will start until shutdown is valid.
Press next to the next step.
3. Set up Run Configuration items
In the final step of the wizard, you can generate a run configuration entry for the JSP that you created, although the wizard will create a run configuration item as the default option, but I think this is not a reasonable default, we recommend that you cancel the create a runtime configuration settings item, Do not create a run configuration entry for the JSP, as shown in the following illustration:
Press the Finish button to create the switch.jsp file, whose code looks like this:
The switch.jsp created by the Code Listing 8 Wizard
1. <%@ page contenttype= "text/html; CHARSET=GBK "%>
2. The 3. The 4. The <title>
5. Switch
6. The </title>
7. The 8. <jsp:usebean id= "UserBean" scope= "page" class= "bookstore. User "/>
9. <jsp:setproperty name= "UserBean" property= "*"/>
<body bgcolor= "#ffffff"
One by one. JBuilder generated JSP
A. . </body>
Line 8th is the JSP tag that references the Bean, and line 9th populates the Bean's property value with the form's data, which fills the request's parameters in the Bean's properties in a name-matching manner, while completing the type conversion (only the base data type or the constructor supports the conversion to complete). After the 9th line is executed, the UserID and Password properties in Userbean are set to the value of the user name and password sent over the login.jsp page.
Since switch.jsp is only for control and does not need to display content to the client, we remove the HTML code from switch.jsp and adjust the switch.jsp to:
Code Listing 9 removing the switch.jsp after static HTML code
1. <%@ page contenttype= "text/html; CHARSET=GBK "%>
2. <jsp:usebean id= "UserBean" scope= "page" class= "bookstore. User "/>
3. <jsp:setproperty name= "UserBean" property= "*"/>
Provide a scriptlet in switch.jsp to compare the UserID and password to the users in the database and the T_user table to see if they are legitimate users and turn to different pages based on the results of the validation. The final code for the switch.jsp is as follows:
Code Listing 10 the final switch.jsp
1. <% @page contenttype= "text/html; CHARSET=GBK "%>
2. <% @page import= "bookstore.*"%>
3. <% @page import= "java.sql.*"%>
4. <jsp:usebean id= "UserBean" scope= "session" class= "bookstore". User "/>
5. <jsp:setproperty name= "UserBean" property= "*"/>
6. <%
7. Connection conn = null;
8. try {
9. conn = Dbconnection.getconnection ();
PreparedStatement PStat = conn.preparestatement (
One. "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 ()) {//password correct
Userbean.setusername (rs.getstring (1));//Set user name
Session.setattribute ("Ses_userbean", UserBean)//Put UserBean into the session object
%><jsp:forward page= "welcome.jsp" > </jsp:forward>
<%} else {//Password error%>
<jsp:forward page= "fail.jsp" > </jsp:forward>
21. <%
}} finally {
IF (conn!= null) Conn.close ();
24.}
25.%>
• Introduce the classes required in the Scriptlet code in line 2nd to 3rd.
• The 7th to 14th line of code sends a query SQL statement to the database and returns the result.
• Line 15th indirectly determines whether the user password is correct by checking the number of records in the result set.
• Line 16th to 18th is the correct response code for the user's password, first fill the Userbean Username property value with the user_name attribute of the result set, and then put the Userbean object into the session, and finally turn to the welcome.jsp page.
• When the user enters the password incorrectly, the result set will not be recorded, at which point Rs.next () returns false, the program shifts to line 20th, and the 20th line of code shifts the page to the password Input Error processing page fail.jsp.
• The code in line 22nd to 24th is used to close the connection to the database.
Perhaps everyone has found that although the 9th to 21st row throws SqlException exception, we do not have the corresponding exception capture block, in the standard Java program will result in a compile-time error, but in the JSP can be compiled sequentially, This is because the JSP page itself captures all the exceptions thrown in the page.
Suppose the SQL query statement on line 11th has an error, such as mistakenly writing the user table name to user (correctly t_user). When the switch.jsp is called, the 14th row throws a SqlException exception, at which point switch.jsp will display the trace information page for the exception stack, as shown in the following figure:
Figure 14 Horrible error-handling page
The error-handling page shown above is bloody fangs, grim, and unfriendly, which may be appropriate for developers because it provides a lot of error-tracking information, but it is impossible for end users to accept this rude error page. JSP allows you to assign an error-specific JSP page to the page through the <%@ page errorpage%>, so that you can display the error in a friendly, intuitive way. In the next section, we'll create a JSP page to handle the error, and after we create it, we'll specify the error-handling JSP page for switch.jsp.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.