Among the many websites on the Internet, dynamic websites based on Web databases are widely used. A dynamic website based on a Web network database consists of a Web browser as the client interface, a database server for information storage, and a Web application server that connects the two. The original CGI technology for developing dynamic websites has been gradually replaced by Java Applets, ActiveX controls, DHTML and JavaScript as the client technology of Web applications continues to develop. These technologies greatly improved the user interface, but when they tried to do some in-depth work, they began to encounter problems such as client browser incompatibility, overloaded servers, slow access, and security issues. JSP technology is a golden key to solving these problems. This article mainly discusses some technical issues in the process of using JSP technology to build dynamic websites.
JSP technology
JSP is a Web development technology based on Java Servlet and the entire Java system. This technology can be used to build advanced, safe, fast and cross-platform dynamic websites. Adding Java program fragments and JSP tags to traditional web page HTML files forms a JSP web page. When the Web server receives a request to access a JSP web page, it first executes the program fragment and then returns the execution result to the client in HTML format. Program fragments can operate databases, redirect web pages, send emails, etc. This is the functionality needed to build a dynamic website. All program operations are executed on the server side, and only the results are sent to the client on the network, with very low requirements on the client's browser. As shown in Figure 1, when the user connects to the JSP website, the user requests a web page, and the JSP page responds to the request alone, converts the user's request for the web page into a request for data, processes the request through JavaBean, and encapsulates the returned data into an HTML page for return to users.
JSP has many advantages:
1. Write the program once and run it anywhere. When designing JSP, the independence of the application platform was fully taken into consideration. Relying on the portability of Java, JSP is supported by many popular operating platforms and can be executed on servers such as Apache, NetScape, and IIS.
2. Fast execution. The JSP page only needs to be compiled once and converted into Java byte code, and then resides in the server memory, speeding up the response to the JSP page. If you do not consider the time it takes to compile the JSP page for the first time, the response speed of JSP is much faster than that of ASP.
3. Advantages of Java. JSP technology uses Java language as the scripting language. Cross-platform, mature, robust, and easily extensible Java technology makes developers' work easier and simpler in other aspects. When the Windows system is suspected of crashing, Java can effectively prevent the system from crashing. The Java language excels in memory management by providing methods to prevent memory leaks. In addition, JSP provides a more robust unexpected event handling mechanism for applications, giving full play to the advantages of Java.
JSP technical difficulties
1. Connect to the database
Database connection is the most important part of a dynamic website. ODBC or JDBC technology can be used when connecting to the back-end database. Although ODBC is an option as a traditional means of connecting to the database, ODBC has the following fatal flaws, making it incompetent for JSP requests:
(1) ODBC is an API implemented in C language. Calling a local C program from a Java program will cause It brings a series of issues similar to security, integrity and robustness.
(2) Secondly, a completely accurate implementation of ODBC from C code ODBC to Java API translation will not be satisfactory, because there are no pointers in Java, and ODBC uses a lot of pointers, including the extremely error-prone null pointer "void" *".
(3) Considering platform portability, using ODBC in developing JSP programs will have negative effects, making the code unsuitable for portability.
In order to make the program easy to transplant while having security, integrity, and robustness, it is more appropriate to use JDBC to connect to the database. JDBC is a Java API that can be used to execute SQL statements. It consists of some classes and interfaces written in Java language, allowing developers to write complete database applications in pure Java language. By using JDBC, SQL statements can be easily transmitted to almost any database. In other words, you don't have to write one program to access Sybase, another program to access Oracle, and another program to access Microsoft's SQL Server. Programs written with JDBC can automatically transmit SQL statements to the corresponding database management system.
When a local database program uses a database such as Microsoft's Access, the JDBC-ODBC bridge developed by Sun can be used. By borrowing this technology, a JSP program can access a database with an ODBC driver. This not only retains the advantages of JDBC, but also can use the ODBC data source provided by Microsoft to connect to Access. No matter what kind of database the other party is, as long as there is an ODBC interface, you can directly use the JDBC-ODBC bridge to connect to the database without changing the corresponding program code due to changes in the back-end database, achieving perfect separation between the application layer and the database layer. If you need to change the back-end database to MySQL, you only need to install the MySQL driver in the ODBC data source, and you can use the MySQL database directly.
2. When implementing a website with built-in components
, due to objective needs, in order to easily distinguish local LAN users from remote users and provide corresponding permissions, the built-in component Request can be used to capture every connection to the server. The IP address of the user is compared and the corresponding permissions are given. In this way, users within this local area network can use all public and non-public resources on the website. The existing method can also be improved by inputting various IP addresses into the database and granting different permissions to different IP addresses to completely control the user's use of website resources.
Session state maintenance is a problem that web application developers must face. In order to know whether the user is still online, the built-in Session component is used. By giving each logged-in user a Session variable, the resources used by the user can be closed after the user abnormally leaves the website, thereby saving memory and improving server performance.
The Cookie class is also provided in JSP, and its constructor has two parameters, representing the name and value of the Cookie. The Cookie class provides various methods to set the attributes of the Cookie. For example, the setMaxAge method can be used to set the lifetime of the Cookie. If the survival time is a negative value, it means that the browser closes the cookie and disappears; if the survival time is 0, it means that the cookie is deleted; if the survival time is a positive number, it means how many seconds the cookie exists. Cookies can be used to temporarily save the user's account and password, and JSP can read them at any time to verify the user's legitimacy. The user's browsing status can be saved in a cookie, and the next time the user visits the web page, the JSP will display a personalized page to the browser.
3. Convert Unicode encoding.
During the debugging process of many JSP pages, problems caused by the conversion of Chinese character encoding and Unicode encoding have been encountered. For example, the Chinese characters in the JSP page seen in the browser are all garbled, and the JSP page cannot be displayed normally. Chinese characters, JSP cannot receive Chinese characters submitted in the form, JSP database reading and writing cannot obtain correct content, etc. This is because most of the core character processing of software with international characteristics is based on Unicode. When the software is running, it is based on At that time, the "Locale/Lang/Codepage" setting determined the corresponding local character encoding settings, and processed local characters accordingly. Therefore, mutual conversion between Unicode and local character sets should be achieved during the processing process, and even two different local characters using Unicode as an intermediary Conversion between character sets. This method is further extended in the network environment, and any character information at both ends of the network also needs to be converted into acceptable content according to the character set settings.
Since the default character set of IE is GB2312, the default character set of Windows is GBK, and the default character set of Java is Unicode. Therefore, without certain conversion, the page obtained from GBK or Unicode displayed directly on the GB2312 character set will be garbled. The Java language uses Unicode to process characters, but from another perspective, non-Unicode can also be used in Java programs. The important thing is to ensure that the Chinese character information at the entry and exit of the program is not distorted. If ISO-8859-1 is completely used to process Chinese characters, correct results can be achieved. After conversion and the web page character set is forced to be displayed in the GB2312 character set, Chinese characters can be displayed normally.