There is a flaw in the login.jsp currently created, assuming that when additional users are added to the database Background T_user table, because the user name pull box is static code, the newly created user does not appear on the page, so it is necessary to change the list of user names to dynamic code, using database tables T_ The user's record dynamically produces the data for the Drop-down box.
We do this through the Userlist.ava class, creating Userlist.java in the project, with the following code:
Code Listing 5 Userlist.java
1. Package bookstore;
2. Import java.sql.*;
3.
4. public class UserList
5. {
6.//Get the HTML dropdown box user list code
7. public static String getuserlisthtml () {
8. Connection conn = null;
9. StringBuffer sbuf = new StringBuffer ();
try {
conn = Dbconnection.getconnection ();
PreparedStatement PStat = conn.preparestatement (
"Select User_id,user_name from T_user");
ResultSet rs = Pstat.executequery ();
while (Rs.next ()) {
Sbuf.append ("<option value= '" + rs.getstring ("user_id") + "' >" +
Rs.getstring ("user_name") + "</option> \ n");
18.}
return sbuf.tostring ();
catch (SQLException ex) {
Ex.printstacktrace ();
Return "";
.} finally {
try {
IF (conn!= null) {
Conn.close ();
conn = null;
28.}
SQLException catch (EX1) {
30.}
31.}
32.}
33.}
Userlist.java only provides a static getuserlisthtml () method that obtains user records from the T_user table in the background database and generates the option code for the Select component of the HTML, as shown in line 16th to 17th.
After creating this class, we reference the class to adjust the login.jsp code, first referencing the UserList class through the Import property in the page instruction tag, and then adjusting the contents of the Select option to the value returned by the getuserlisthtml () method. As shown in the following illustration:
Figure 7 Writing code through Codeinsight
Replacing the original static HTML code with an expression label, it is worth mentioning that in the JSP you can also use Codeinsight input code as you would in writing a generic Java class. The adjusted login.jsp code is shown in bold, as follows:
Code Listing 6 Adjusted login.jsp code
1. <% @page contenttype= "text/html; CHARSET=GBK "import=" bookstore. UserList "%>
2. The 3. The 4. <title> Login </title>
5. The 6. <body bgcolor= "#ffffff"
7. Form Name= "Form 1" method= "POST" action= "switch.jsp" > user name:
8. <select name= "UserId"
9. <option value= "" selected>--logged in user--</option>
<%=userlist.getuserlisthtml ()%>
One. </select>
12. Password: <input name= "password" type= "password"
<input type= "Submit" name= "submit" value= "Login"
14. </Form
</body>
. After saving the login.jsp, right-click in the content pane login.jsp File tab and select the Web Run using Defaults,jbuilder to start the Tomcat 5.0 application server and run on port 8080. Compile and run the login.jsp file as shown in the following illustration:
Figure 8 Running login.jsp in JBuilder
JBuilder automatically switches to the Web View View page to show how the page works. However, the JBuilder browser's support for Web pages is weak and JavaScript scripting is not supported. So after running the login.jsp, it is best to view the effect of the Web page through IE, you only need to open IE, and the JBuilder access login.jsp address in the address bar can be copied, as shown in the following figure:
Figure 9 using IE to access the login.jsp running in JBuilder
The user in the Drop-down box for the user name is already in the dynamic list of users, and the user who is adding and removing users from the T_user table will change accordingly.
Before running the JSP, it is best to compile a separate JSP file: In the content pane of the JSP File tab right-click, in the pop-up menu select Make "xxx.jsp", you can complete the JSP file compilation, timely detection of errors. JSP files and Java files when editing, the biggest difference is that the Java program files in the editor and the structure pane are listed, but some errors in the JSP file need to compile to discover.
Tips:
Since compiling a JSP file requires converting it to a servlet file, then compiling the intermediate servlet file, and then redirecting to the JSP file after the error occurs, compiling a JSP file is time-consuming and often takes several times longer than compiling a Java file. By default, compiling a project compiles all the JSP in the project and consumes a lot of time. So it's best to cancel the setup of the JSP file in the compilation project: properties...->build-> the check JSPs for errors at Build-time option in the Build Settings page by Project->project. Check this option when you really need it, and then cancel the option after compiling. This setting will allow you to gain valuable time for your development.
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.