JSP Practice Essentials

Source: Internet
Author: User
Tags character set execution html page interface connect odbc mysql mysql database
JS in the Internet many websites, web-based database Dynamic Web site application is quite extensive. The Dynamic Web site based on the Web database is made up of a Web browser as the client interface, and a database server is made up of a Web application server that is used for information storage and a connection. The original CGI technology for developing dynamic Web sites has been gradually replaced by Java applets, ActiveX controls, DHTML, and JavaScript as the client technology for Web applications continues to evolve. These technologies greatly improve the user interface, but when they try to do some deep work, they start to encounter problems such as incompatible client browsers, overloaded servers, reduced access speed, and security. JSP technology is a golden key to solve these problems, this paper mainly discusses some technical problems in the process of constructing Dynamic Web site using JSP technology.

JSP Technology

JSP is a Web development technology based on the Java servlet and the entire Java system, using this technology to build advanced, secure, fast, and Cross-platform Dynamic Web sites. In the traditional Web page HTML file to add Java program fragments and JSP tags, it constitutes a JSP Web page. When a Web server receives a request to access a JSP Web page, it first executes the program fragment in it, and then returns the execution results to the customer in HTML format. Program fragments can manipulate databases, redirect Web pages, send e-mail, and so on, which is what you need to build a dynamic Web site. All program operations are performed on the server side, and the only results that are delivered to the client on the network are low requirements for the client's browser. As shown in Figure 1, when the user connects to the JSP site, the user requests the Web page, the JSP page responds to the request alone, converts the user's request to the Web page into a request for the data, processes the request through JavaBean, and returns the returned data to the user by encapsulating it into an HTML page.

JSP has many advantages:

1. The program is written once and can be run everywhere. JSP in the design, fully consider the application platform of the independent nature. Relying on Java portability, JSP is currently supported by many popular operating platforms and can be executed on servers such as Apache, NetScape, and IIS.

2. Faster execution. JSP pages can only be compiled once into Java byte code, and then reside in server memory, speeding up the response to JSP pages. If you do not take into account the time spent in the first compilation of the JSP page, the JSP will respond much faster than the ASP.

3. The advantages of Java. JSP technology is used in the Java language as a scripting language. Cross-platform, mature, robust, and extensible Java technology makes it easier and simpler for developers to work in other ways. Java can effectively prevent system crashes when Windows systems are suspected to be crashing. The Java language plays a role in memory management by providing a way to prevent memory leaks. In addition, JSP provides the application with a more robust incident handling mechanism, giving full play to the Java advantage.

JSP Technical Difficulties

1. Connecting to the database

Database connections are the most important part of a dynamic Web site, and you can use ODBC or JDBC technology when connecting to a backend database. Although ODBC is an option as a traditional means of connecting to a database, ODBC has the following fatal flaw that makes it unfit for JSP requests:

(1) ODBC is the C language implementation of the API, from Java programs to call the local C program will bring a series of similar security, integrity, robustness aspects of the problem.

(2) Second, the complete and precise implementation of ODBC from C code to Java API translation is not satisfactory because there are no pointers in Java, and in ODBC, pointers are heavily used, including error-prone null pointer "void *".

(3) Considering the platform portability, the use of ODBC in the development of JSP programs can have a negative impact, making the code unsuitable for porting.

In order for the program to be safe, complete and robust, it is easy to migrate, and it is more appropriate to use JDBC to connect the database. JDBC is a Java API that can be used to execute SQL statements, consisting of classes and interfaces written in the Java language, allowing developers to write complete database applications in a pure Java language. By using JDBC, you can easily transfer SQL statements to almost any database. That is, you don't have to write a program to access Sybase, write another program to access Oracle, and then write a program to access Microsoft's SQL Server. A program written with JDBC can automatically transmit SQL statements to the appropriate database management system.

When you use a database such as Microsoft Access for a local database program, you can use the Jdbc-odbc bridge developed by Sun Company to access a database with an ODBC driver by using this technology JSP program. This preserves both the benefits of JDBC and the use of Microsoft-supplied ODBC data sources to connect to access. No matter what the other database, as long as there is an ODBC interface can directly use the Jdbc-odbc Bridge to connect with the database, without the change due to the backend database changes in the corresponding program code, the application layer and the database layer to achieve a perfect separation. If you need to change the back-end database to MySQL, simply install the MySQL driver in the ODBC data source, you can use the MySQL database directly.

2. Built-in Components

In the implementation of the site, as a result of the objective needs, in order to facilitate the distinction between local LAN users and remote connected users, and provide the appropriate permissions, you can use the built-in component request to capture each of the users connected to the server IP address, through the comparison given the corresponding permissions. In this way, users in the local area network can use all the public and open resources in the website. You can also modify existing methods to enter various IP addresses into the database and give different IP addresses different permissions to completely control the user's use of Web resources.

Session state maintenance is a problem that Web application developers must face. In order to understand whether the user is still online, using the built-in session component, by giving each login user A session variable, you can turn off the user's resources to save memory and improve server performance after the user has left the site abnormally.

The cookie class is also provided in the JSP, and its constructor has two parameters, representing the name and value of the cookie, respectively. The cookie class provides a variety of ways to set the properties of a cookie, such as by using the Setmaxage method to set the cookie's lifetime. If the lifetime is negative, on behalf of the browser to close the cookie, that is, disappear; The survival time is 0, representing the deletion cookie; The lifetime is a positive number, representing how many seconds the cookie exists. You can use cookies to temporarily save the user's account and password, JSP can be read at any time, verify the legality of the user. The user's browsing state can be saved in a cookie, and the next time the user accesses the page, the JSP displays the personalized page to the browser.

3. Convert Unicode Encoding

In many JSP page debugging process has encountered because of encoding and Unicode encoding conversion caused by problems, such as in the browser to see the JSP page of the Chinese characters are garbled, JSP page can not normal display Chinese characters, JSP can not receive form submitted by the Chinese characters, JSP database read and write can not get the correct content and so on, this is because most of the international characteristics of the software core character processing is based on Unicode, at the time of the software runtime according to the "Locale/lang/codepage" setting to determine the corresponding local character encoding settings, In order to handle local characters, the conversion of Unicode and local character sets should be implemented during processing, and even two different local character sets, which are mediated by Unicode, are converted to each other. This approach is further extended under the network environment, and the character information at both ends of the network needs to be converted to acceptable content according to the set of character set.

Because the IE default character set is GB2312, Windows defaults to Unicode for Gbk,java, so if you do not pass a certain conversion, the page displayed from GBK or Unicode directly on the GB2312 character set will be garbled. The Java language uses Unicode processing characters, but from another perspective, non-Unicode can also be used in Java programs, and it is important to ensure that Chinese character information is not distorted in the entry and exit of the program. If the full use of iso-8859-1 to deal with Chinese characters can also achieve the correct results, after the conversion and the Web page character set to be forced to display the GB2312 character set, you can display the normal characters.



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.