Java web development, the consequences of putting too much java code in a jsp, from the java web lightweight development interview tutorial, jspjava

Source: Internet
Author: User
Tags import database

Java web development, the consequences of putting too much java code in a jsp, from the java web lightweight development interview tutorial, jspjava

 

A simple logon page is displayed. If the user passes the verification, the Welcome word of the Welcome user name is displayed. Otherwise, the logon page is returned for the user to enter the Welcome word again.

The complete code of this part is login. jsp in the JSPDemo project. The following describes the key code.

Code Location

Video location

Code/Chapter 2/JSPDemo

Video/Chapter 4/JSP Case Explanation

According to the JSP syntax, you can use @ import to import the required jar package. Here, you need to import database files that support MySQL. The code for this part is as follows.

1 <% @ page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>

2 <% @ page import = "java. SQL. *" %>

3 <% @ page import = "com. mysql. jdbc. Driver" %>

JSP is equivalent to adding Java code to the HTML page. Generally, the main code is put in the <body> tag.

First, analyze the business. When entering this page, you need to determine whether there is a user name and password information input for different actions, you need to use an embedded object request to complete this function. The main code is as follows.

4 <body>

5 <%

6 if (request. getParameter ("username") = null)

7 {

8%>

9 <form method = "post" action = "login. jsp" name = "userInfo" id = "userInfo">

10 <table>

11 <tr>

12 <td> User Name </td>

13 <td> <input type = "text" name = "username" id = "username"/> </td>

14 </tr>

15 <tr>

16 <td> Password </td>

17 <td> <input type = "password" name = "password" id = "password"/> </td>

18 </tr>

19 <tr>

20 <td colospan = "2" align = "center">

21 <input type = "submit" name = "logon" value = "Login"/> & nbsp;

22 <input type = "reset" name = "reset" value = "Reset"/>

23 </td>

24 </tr>

25 </table>

26 </form>

We can see that Java code is included in JSP with <%... %>. Here, request. getParameter is used to read whether username exists. A request is an embedded object that can be directly used without being defined. The method for obtaining parameters is used here.

There are many embedded objects in JSP, which are often used in projects, such as request, response, Session, and out. You do not need to remember the embedded object knowledge, you can check the existing APIs when using them.

Once no user name information is input, an HTML form is used to display the window for user input.

If request. getParameter ("username") is not null, that is, the user has already entered the user name, you need to obtain the user name and password information and verify it in the database. The Code is as follows.

27 <%

28}

29 else

30 {

31 String username = request. getParameter ("username"). toString ();

32 String pwd = "";

33 if (request. getParameter ("password ")! = Null)

34 {

35 pwd = request. getParameter ("password"). toString ();

36}

37 // connect to DB

38 Class. forName ("com. mysql. jdbc. Driver ");

39 Connection connection = null;

40 connection = DriverManager. getConnection (

41 "jdbc: mysql: // localhost: 3306/books", "root", "123456 ");

42 String SQL = "select username from users where UserName =? And Pwd =? ";

43 PreparedStatement pst = connection. prepareStatement (SQL );

44 pst. setString (1, username );

45 pst. setString (2, pwd );

46 ResultSet rs1_pst.exe cuteQuery ();

47 // can login

48 if (rs. next ())

49 {

50%>

51 Welcome, <% = rs. getString ("username") %>

After obtaining the username and password from lines 31st and 35 in the above Code, use the Class of line 38th. forName loads the JDBC driver, defines a PreparedStatement in row 43rd to execute the SQL statement, and uses a ResultSet in row 46th to receive the running result.

Please note that the PreparedStatement used here is to avoid SQL injection. You can read the database related content in this book.

Once the rs is determined through the if statement in the first row. if there is a return object for next (), you need to end the JDBC database access code in Java with %> in row 50th, and then display the Welcome in row 51st. Here we can see another Syntax of JSP, and use <% = %> to display the result of the variable.

If rs. if no matching user information is found in the database, you need to return to the logon page and ask the user to enter the logon information again. The code for this part is as follows.

52 <%

53}

54 else

55 {

56%>

57 <form method = "post" action = "login. jsp" name = "userInfo" id = "userInfo">

58 <table>

59 <tr>

60 <td> User Name </td>

61 <td> <input type = "text" name = "username" id = "username"/> </td>

62 </tr>

63 <tr>

64 <td> Password </td>

65 <td> <input type = "password" name = "password" id = "password"/> </td>

66 </tr>

67 <tr>

68 <td colospan = "2" align = "center">

69 <input type = "submit" name = "logon" value = "Login"/> & nbsp;

70 <input type = "reset" name = "reset" value = "Reset"/>

71 </td>

72 </tr>

73 </table>

74 </form>

75 <%

76}

77}

78%>

79 </body>

80

This is actually a code copy, and a form that provides user name and password input is written again.

JSP syntax is not simple, but you can have a selective understanding, so that you can use the learning time to learn more important knowledge points (such as framework ideas and optimization ideas.

 

In the example given by the above code, we can see the following defects:

① The Code was copied and the form for user input was copied twice.

② Frequent switch between JSP and HTML logic causes reading and Development troubles. It will be difficult for others to maintain this part of code in the future.

This kind of defect is not obvious at the beginning of development. When it comes to in-depth development, this kind of defect will erupt.

① Modify point 1: We need to adjust the Form code for the user to authenticate the identity, and add the verification code input box.

We have to modify two duplicate and identical codes, which will not only increase the workload, but also lead to the forgetting to modify one of them, which increases the difficulty of code maintenance.

② Modify the data table name and field in the Oracle database.

We not only need to modify the JDBC code, but also the display code of Welcome xxx. That is to say, database modifications directly involve other types of business code.

③ Modify point 3: implement the "three-time authentication, but the user is locked" function.

We need to count the JDBC code. If the number of logins is less than 3, we need to copy the Form code repeatedly. If there are more than three times, you also need to add a "screen lock" display page in the Java code, which leads to more confusion in JSP.

We have seen many small projects with fewer than 10 pages. developers put all the functions directly in JSP to save trouble, just as in the previous example, after two or three months of delivery, the JSP code became like tianshu and had a poor reading performance after the user made several suggestions for improvement.

When submitting an interview question, if we find that the candidate uses a "big and comprehensive" development method, we will ask the interviewer to give a reason. If the reason is similar to "the project is tight and the employee is absent, the trade-off would rather sacrifice the quality of the Code, which means that the interviewer must at least compare with the MVC development mode and is acceptable. If the interviewer does not know the MVC development mode or even the disadvantages of "Big and comprehensive", then our evaluation should at least be "no architecture consciousness ".

How can we improve it? Break down the business and use a layered approach to break down different types of business.

Specifically, the Code irrelevant to display is stripped from the JSP page. A good JSP page should be rare or even not use the Java code included in <%>.

 

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.