1. What is JSP (JSP pages) is a dynamic web page technical standard proposed by Sun Microsystems and jointly established by many companies. Its website is http://www.javasoft.com/products/jsp. Add Java to traditional webpage HTML files (*. htm, *. html) Program Snippets (scriptlet) and JSP tags constitute JSP web pages (*. jsp ). When the Web server encounters a request to access the JSP webpage, it first executes the program fragment and then returns the execution result to the customer in HTML format. Program snippets can operate databases, redirect webpages, and send emails. This is the function required to build a dynamic website. All program operations are performed on the server, and the results are only obtained after the network is uploaded to the client. The client has the lowest requirements on the client browser and can implement no plugin, no ActiveX, and no Java applet, no frame. This article describes how to use JSP technology to develop dynamic web pages, and briefly analyzes the differences between JSP technology and Microsoft's ASP technology.
Ii. How to install and start the experiment JSP technology first requires the establishment of the runtime environment. This process is quite simple:
1. Download JDK (Java 2 SDK, Standard Edition, V 1.2.2) at http://java.sun.com/jdk ).
2. Download jswdk (JavaServer Web Development Kit 1.0.1) at http://java.sun.com/products/jsp ). Linux users can download Tomcat 3.0 at ttp: // jakarta.apache.org.
3. The installation uses Windows ntenvironment as an example. JDK installation is the first download of JDK 1_2_2-win.exe. Then, modify the system environment parameters, add [X:] \ JDK 1.2.2 \ bin to the path parameter, and add the new environment parameter classpath = [X:]. \ jdk1.2.2 \ Lib \ tools. jar, where [X:] is the hard drive letter (C:, D:, etc.) for JDK installation ). Jswdk's installation only needs to release the jswdk1_0_1-win.zip directory to the hard disk root directory (C: \, D: \, etc.), then you can find the \ jswdk-1.0.1 \ directory on the hard disk. If you do not want to retain jswdk in the future, you can delete this directory without any system file or registry issues. For more details about the installation process and the installation of JDK and tomcat in Solaris/Unix and Linux, refer to the installation instructions in the downloaded package.
4. Start the Windows NT environment as an example, execute startserver. bat in the \ jswdk-1.0.1 \ directory, you can start a jswdk web server supporting JSP web technology. To avoid conflicts with existing Web servers (such as IIS and PWS), The jswdk web server uses port 8080. After you type http: // localhost: 8080 or http: // 127.0.0.1: 8080 in the address bar of your browser, if you can see the jswdk welcome page, the JSP experiment environment has been created, you can go to the next lab. To disable the web server, run stopserver. bat.
Iii. jsp simple example jswdk contains the web server document directory in the default state is \ jswdk-1.0.1 \ webpages, the main file in the initial state is index.html and index. jsp. That is to say access http: // localhost: 8080 is equal to access \ jswdk-1.0.1 \ webpages \ index.html. Use a text editor, such as notepad in windows, to create a text file hi. jsp, saved in the \ jswdk-1.0.1 \ webpages \ directory, the content is as follows:
<HTML>
<Head>
<Title> Hi-JSP experiment </title>
</Head>
<Body>
<%
String MSG = "This JSP test .";
Out. Print ("Hello world! ");
%>
<H2> <% = MSG %> </H2>
</Body>
</Html>
type http: // localhost: 8080/Hi in the address bar of the browser. JSP, the web server in jswdk will execute the Java program statements enclosed in <% and %> In the JSP file, where out. print outputs text to the webpage. The statement <% = variable | expression %> outputs the values of variables or expressions in Java scriptlet to the webpage. Execution result 1 is displayed. Figure 1 assign the MSG variable to a Chinese string and output it with <% = %> or out. print outputs a Chinese string. The experiment runs normally in NT4 and RedHat 6.1 English versions, but garbled characters appear in NT 4.0 and 98 Chinese versions.
4. Unified website interface JSP supports file inclusion on the server side, that is, multiple other files can be inserted in a JSP file to implement a unified website interface. Modify the preceding hi. JSP and save it as mypage. JSP:
<% @ include file = "top.htm" %>
<%
string MSG = "This JSP test. ";
out. print ("Hello world! ");
%>
<% = MSG %>
<% @ include file = "bot.htm" %>
V. Server parameter settings
The Web server parameters of jswdk are saved in \ jswdk-1.0.1 \ webserver. XML, and you can modify the default settings by opening and editing this file with a Windows wordpad. This section focuses on jswdk. The setting methods for tomcat in Linux are slightly different.
Jswdk default document directory is \ jswdk-1.0.1 \ webpages, under which you can create subdirectories, such as \ jswdk-1.0.1 \ webpages \ test, you can use HTTP in the browser: // localhost/test to access this directory. To make this sub-directory executable JSP program, it must also be in webserver. add the <service> </service> section in XML:
<Webapplication id = "test" Mapping = "/test" docbase = "WebPages/test"/>
Additionally, you must create the \ jswdk-1.0.1 \ webpages \ test \ WEB-INF directory and copy the following four files from the \ jswdk-1.0.1 \ webpages \ WEB-INF directory: mappings. properties, mime. properties, servlets. properties and webapp. properties. After these processes are completed, the jswdk web server can be notified to execute the JSP program in http: // localhost/test.
Vi. JavaBean
One of the most attractive aspects of JSP web pages is the ability to expand the functions of programs on the Web page with the JavaBean technology.
Javabean is a Java class that encapsulates attributes and methods to become objects with certain functions or processing services. Javabean is organized into a package for management. In fact, a group of Javabean is put together in a directory named XX. Add package XX before the definition of each class. In this example, test is used. The directory test must be placed in the Directory included in the System Environment classpath before the system can find the JavaBean. By default, jswdk adds \ jswdk-1.0.1 \ webpages \ WEB-INF \ JSP \ beans \ To classpath. When creating your own JavaBean and package, it is also a simple method to put it in this directory.
The following describes a simple JavaBean framework. Create a text file helloworld. Java in the text editor and save it in the \ jswdk-1.0.1 \ webpages \ WEB-INF \ JSP \ beans \ test directory with the following content:
Package test;
Public class helloworld {
Public string name = "My first Bean ";
Public String gethi ()
{
Return "hello from" + name;
}
}
After editing helloworld. Java, in the DOS state, enter the directory \ jswdk-1.0.1 \ webpages \ WEB-INF \ JSP \ beans \, compile helloworld. Java with JDK javac command as follows:
Javac helloworld. Java
Note that Java is case-sensitive. In a program, letters in the compiled command line cannot be written incorrectly.
After successful compilation, A Javabean is created. The following describes how to use this JavaBean in JSP. Create a text file hi-bean.jsp in the text editor and save it in the \ jswdk-1.0.1 \ webpages \ test directory with the following content:
<HTML>
<Head>
<Title> test JavaBean </title>
</Head>
<Body>
<JSP: usebean id = "hellobean" Scope = "session" class = "test. helloworld"/>
<% = Hellobean. gethi () %>
<HR>
<%
Hellobean. Name = "jsp ";
Out. Print (hellobean. gethi ());
%>
</Body>
</Html>
On the JSP page, use the <JSP: usebean.../> syntax to create a JavaBean object and name it hellobean. You can see from this simple example how to set and obtain the JavaBean attribute and call the JavaBean method. Type http: // localhost: 8080/test/hi-bean.jsp in the address bar of the browser, as shown in result 3.
Note: If you modify and re-compile the JavaBean program, you need to disable and restart the jswdk web server before the modification will take effect. If you only modify the JSP file, you do not need to restart the jswdk web server.
Although this completes a very simple JavaBean framework, you can follow this framework to design a variety of JavaBean. For example, data access from JSP is usually implemented through JavaBean.
VII. Database Connection
Database connection is the most important part for dynamic websites. The connection technology in Java is JDBC (Java database connectivity ). Many database systems have JDBC drivers. The Java program connects to the database through the JDBC driver and performs operations such as querying and extracting data. Sun also developed JDBC-ODBC bridge, with this technology Java program can access the database with ODBC driver, currently most database systems with ODBC driver, therefore, Java programs can access databases such as Oracle, Sybase, ms SQL Server, and MS Access. The following describes how to use access to implement a dynamic FAQ (FAQs and answers) website.
First, create an Access database FAQ. mdb, where the table FAQs has the field ID (automatic incremental type, and set as the primary keyword), subject (text type, length 200), and answers (Remarks type ). This table contains some common programming knowledge questions and answers.
Then, add system DSN to the ODBC datasource module of the control panel, name the FAQ, and point to FAQ. MDB.
Create a JavaBean named FAQ. Java and save it in the \ jswdk-1.0.1 \ webpages \ WEB-INF \ JSP \ beans \ test directory. FAQ. Java:
Package test;
Import java. SQL .*;
Public class FAQ {
String sdbdriver = "Sun. JDBC. ODBC. jdbcodbcdriver ";
String sconnstr = "JDBC: ODBC: FAQ ";
Connection conn = NULL;
Resultset rs = NULL;
Public FAQ (){
Try {
Class. forname (sdbdriver );
}
Catch (Java. Lang. classnotfoundexception e ){
System. Err. println ("FAQ ():" + E. getmessage ());
}
}
Public resultset executequery (string SQL ){
Rs = NULL;
Try {
Conn = drivermanager. getconnection (sconnstr );
Statement stmt = conn. createstatement ();
Rs = stmt.exe cutequery (SQL );
}
Catch (sqlexception ex ){
System. Err. println ("aq.exe cutequery:" + ex. getmessage ());
}
Return Rs;
}
}
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> my faq! </Title>
</Head>
<Body>
<P> <B> This Is My faq! </B> </P>
<% @ Page Language = "Java" Import = "Java. SQL. *" %>
<JSP: usebean id = "workm" Scope = "page" class = "test. FAQ"/>
<%
Resultset rs = workm.exe cutequery ("select * From FAQs ");
String tt;
While (Rs. Next ()){
Tt = Rs. getstring ("Answer ");
Out. Print ("<li>" + Rs. getstring ("subject") + "</LI> ");
Out. Print ("<PRE>" + tt + "</PRE> ");
}
Rs. Close ();
%>
In the address bar of the browser, type http: // localhost: 8080/test/faq. jsp. The FAQ. jsp calls JavaBean and reads and outputs the content from the database.
Due to space limitations, this article cannot list complex examples of JSP-JavaBean-jdbc/ODBC-database. You can find and download the database connection examples from the URL recommended at the end of this article.
VIII. Technical Analysis
Microsoft's ASP technology is also a dynamic web page development technology. JSP and ASP are very similar in form. asp programmers can recognize <%> and <% = %> at a glance. However, further exploration will reveal many differences, including the following three main points:
1. jsp is more efficient and secure
ASP is stored as the source code and run as an interpreter. The Source Code must be interpreted for each ASP Web page call, which is inefficient. In addition, the IIS vulnerability has exposed many website source programs, including the websites developed by the author using ASP before. All ASP programs have been downloaded.
JSP is compiled into byte code before execution. bytecode is interpreted and executed by Java Virtual Machine (Java Virtual Machine), which is more efficient than source code interpretation; the server also has the bytecode cache mechanism, which can improve the access efficiency of bytecode. The first time you call a JSP page, it may be a little slow because it is compiled into a cache, and it will be much faster in the future. At the same time, JSP Source programs are unlikely to be downloaded, especially the JavaBean program can be placed in a non-External directory.
2. More convenient JSP components (Component)
ASP expands complex functions through COM, such as file uploading, sending emails, and separating business processing or complex computing into independent Reusable Modules. JSP implements the same function expansion through JavaBean.
In terms of development, the development of COM is far more complex and complex than that of JavaBean. Learning ASP is not difficult, but learning to develop com is not easy. Javabean is much simpler. From the above examples, we can see that it is very convenient to develop JavaBean.
In terms of maintenance, COM must be registered on the server. If the com program is modified, it must be re-registered, or even shut down and restarted. JavaBean does not need to be registered and can be placed in the directory contained in classpath. If the JavaBean is modified, jswdk and tomcat still need to be shut down and re-run (but not shut down), but the developer has promised not to shut down the server in future versions.
In addition, JavaBean is fully oop. You can easily create a set of reusable object libraries for different service processing functions, such as user permission control and automatic email reply.
3. A Wider JSP adaptation platform
ASP currently only applies to NT and IIS. Although there are chilisoft plug-ins in UNIX to support ASP, ASP itself has limited functions and must be expanded through the combination of ASP + COM. It is very difficult to implement COM in UNIX.
JSP is different. Almost all platforms support Java, and JSP + JavaBean can be accessible on all platforms. IIS under NT supports JSP through a plug-in, such as JRun (http://www3.allaire.com/products/jrun/) or ServletExec (http://www.newatlanta.com. The famous web server Apache already supports JSP. Because Apache is widely used in NT, UNIX, and Linux, JSP has a wider operating platform. Although the NT operating system has a large market share, the Unix advantage in the server is still great, and the new Linux is not small.
Porting from one platform to another, JSP and JavaBean do not even need to be re-compiled, Because Java bytecode is standard and platform-independent. I am very satisfied with the fact that the JSP page of the experiment under NT is running in Linux without being blocked.
IX. Conclusion
To sum up, JSP is a powerful tool for building dynamic websites. Therefore, we recommend it to readers and wish you master JSP and develop excellent websites. ASP programmers can also give it a try. jsp also contains session, request, response/out and other objects.