Using JSP to build a dynamic website sun Yiyi Du Fu
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. In the traditional web page HTML file (*. htm ,*. java program snippets (Scriptlet) and JSP tags are added to html to form a JSP webpage (*. 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. Install Windows Firewall, modify the system environment parameters, add [x:] jdk1.2.2in to the PATH parameter, and add the new environment parameter CLASSPATH = [x:] jdk1.2.2libools. 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 that supports 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.
3. Simple examples of JSP: callback and index. jsp. That is to say, access http: // localhost: 8080is equal to ipvjswdk-1.0.1webpagesindex.html. Use a text editor, such as Notepad (Notepad) in Windows, to create a text file hi. jsp, saved in the jswdk-1.0.1webpages directory, its content is as follows:
<Html>
<Head>
<Title> Hi-JSP experiment </title>
</Head>
<Body>
<%
String Msg = "This JSP test .";
Out. print ("Hello World! ");
%>
<H2> <% = Msg %> </Body>
</Html>
In the browser's address bar, type http: // localhost: 8080/hi. 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, that is, multiple other files can be inserted in a JSP file to implement a unified website interface. Modify hi. jsp and save it as mypage. jsp:
<% @ Include file = "top.htm" %>
<%
String Msg = "This JSP test .";
Out. print ("Hello World! ");
%>
<H2> <% = Msg %> <% @ Include file = "bot.htm" %>
Hidden, the bottom half is saved as bot.htm, the code is shown below:
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> my homepage </title>
</Head>
<Body>
<Table border = "0" width = "100%" cellpadding = "4" cellspacing = "0" align = "center">
<Tr>
<Td width = "100%" colspan = "2" bgcolor = "#837ED1" align = "center"> <font face = ""
Color = "# FFFF00" size = 5> homepage title </font>
</Td>
</Tr>
<Tr>
<Td bgcolor = "#837ED1" width = "15%" valign = "top" align = "center"> <br>
<Font color = "# FFFFFF"> options </font> <p> <font color = "# FFFFFF"> options </font> </p>
<P> <font color = "# FFFFFF"> options </font> </p>
<P> <font color = "# FFFFFF"> ...... </font> </p>
<P> </p>
</Td>
& Lt; td width = "85%" valign = "top" & gt;
Bytes ----------------------------------------------------------------------------------------------------------------
</Td>
</Tr>
</Table>
</Body>
</Html>
Enter http: // localhost: 8080/mypage. jsp in the address bar of the browser. Expected result 2 is displayed. Figure 2. In this way, the website interface can be unified, and designers can focus on the functional modules to process user logon, connect to the database, and send emails. Each JSP file has the following structure:
<% @ Include file = "top.htm" %>
<%
// Implement certain functions
%>
<% @ Include file = "bot.htm" %>
The boundaries of the website are also relatively easy. You only need to modify top.htmand bot.htm to influence all webpages.
V. Server parameter settings
The Web server parameters of JSWDK are saved in the jswdk-1.0.1webserver.xml, And you can modify the default settings by opening and editing the file with a Windows wordpad. This section focuses on JSWDK. The setting methods for Tomcat in Linux are slightly different.
The default document directory for JSWDK is a jswdk-1.0.1webpages, where subdirectories can be created, such as jswdk-1.0.1webpagesest, to access this directory with http: // localhost/test in the browser, to make this sub-directory executable JSP program, you must also. add the <Service> </Service> section in xml:
<WebApplication id = "test" mapping = "/test" docBase = "webpages/test"/>
Also, you must create a jswdk-1.0.1webpagesestWEB-INF directory and copy the following four files from the jswdk-1.0.1webpagesWEB-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. JSWDK adds the jswdk-1.0.1webpagesWEB-INFjspeans to CLASSPATH by default. 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.1webpagesWEB-INFjspeansest 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.1webpagesWEB-INFjspeans, 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.1webpagesest 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.