JSP (JavaServer pages) is a Dynamic Web page technology standard, which is initiated by Sun Microsystems Company, and is built by many companies, and its Web site is http://www.javasoft.com/products/jsp. Adding Java program Fragments (scriptlet) and JSP tags (tag) to the traditional Web page HTML file (*.htm,*.html) forms the JSP Web page (*.jsp). When a Web server encounters 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, and send emails, and so on, which is the functionality required to build dynamic Web sites. All programs operate on the server side, the network sent to the client is only the result of the minimum requirements for customer browsers, can be achieved without plugin, no ActiveX, no Java applets, even without frame.
This article will introduce the method of using JSP technology to develop dynamic Web pages, and will briefly analyze the differences between JSP technology and Microsoft's ASP technology.
Second, how to install and start
In order to experiment JSP technology, first need to establish the running 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 http://jakarta.apache.org/.
3, installation
In the case of the Windows NT environment, the JDK is installed to run the downloaded Jdk1_2_2-win.exe first, then modify the system environment parameters, add [x:]jdk1.2.2in, and add new environment parameters to the Path parameter classpath=[x:] Jdk1.2.2lib Ools.jar, where [x:] is the hard drive character (c:, D:, and so on) where the JDK is installed. The JSWDK installation only needs to release the Jswdk1_0_1-win.zip directory to the hard disk root (c:, D:, and so on), and then you can find the jswdk-1.0.1 directory on your hard disk. If you do not want to keep JSWDK in the future, you can delete this directory, without any system files and registry legacy problems. More detailed installation procedures, as well as installation of JDK and Tomcat under Solaris/unix and Linux, can refer to the installation instructions in the downloaded packages.
4, start
Taking the Windows NT environment as an example, executing startserver.bat in the jswdk-1.0.1 directory enables a Web server in JSWDK that supports JSP web technology. To not conflict with existing Web servers (such as IIS, PWS, and so on), the JSWDK Web server uses port 8080. In the browser's address bar type http://localhost:8080 or http://127.0.0.1:8080, if you can see the JSWDK Welcome page on the JSP experimental environment has been completed, you can enter the next experiment. To close the Web server, run Stopserver.bat.
Three, the JSP simple example
The document directory for the Web server contained in JSWDK is jswdk-1.0.1webpages by default, and the main document is index.html and index.jsp by default. That is to say, access to http://localhost:8080 equals access jswdk-1.0.1webpagesindex.html.
With a text editor, such as Notepad in Windows, create a text file hi.jsp, saved in the Jswdk-1.0.1webpages directory, which reads as follows:
<TITLE>HI-JSP Experiment </title>
<body>
<%
String MSG = "This JSP test.";
Out.print ("Hello world!");
%>
</body>
Typing the Web server in Http://localhost:8080/hi.jsp,JSWDK in the address bar of the browser executes the Java program statements enclosed in 〈% and%> in the JSP file, where Out.print is outputting the text to the Web page, the statement 〈% = Variable | The function of an expression%> is to output the value of a variable or expression in a Java scriptlet to a Web page.
The variable msg assignment is a Chinese string, with 〈%=%> output, or Out.print output Chinese character string, then in English version NT4 and Redhat 6.1 under the normal test results, and in the Chinese NT 4.0 and Chinese 98, it will appear garbled.
Four, the unified website interface
JSP supports server-side file inclusion, that is, you can insert multiple other files in a JSP file to achieve a unified web interface. Modify the above hi.jsp and save as mypage.jsp:
<%@ include file= "top.htm"%>
<%
String MSG = "This JSP test.";
Out.print ("Hello world!");
%>
<%@ include file= "bot.htm"%>
With the visual HTML editor, such as FrontPage, Dreamweave, and other design Web site framework structure, the design of the frame structure file divided into two parts, the upper half of the above save as top.htm, the following half saved as bot.htm, the code as follows:
Type http://localhost:8080/mypage.jsp in the address bar of the browser.
So the interface of the site can be unified, and designers can focus on the function module to deal with user login, connect the database, send email and so on. Each JSP file has the following structure:
<%@ include file= "top.htm"%>
<%
Implement some features
%>
<%@ include file= "bot.htm"%>
Maintaining the site's interface is also relatively easy, as long as you modify top.htm and bot.htm, you can affect all pages.
Five, server parameter setting
The JSWDK Web server parameter is saved in Jswdk-1.0.1webserver.xml, and the default setting value can be modified by opening and editing the file with Windows WordPad. This section focuses on a slightly different setting for Tomcat under Jswdk,linux.
JSWDK The default document directory is Jswdk-1.0.1webpages, where you can create subdirectories, such as Jswdk-1.0.1webpages est, to access the directory in the browser http://localhost/test. In order to enable this subdirectory to execute the JSP program, it must also be added to the 〈service>〈/service> section in Webserver.xml:
<webapplication id= "test" mapping= "/test" docbase= "Webpages/test"/> and must also establish a jswdk-1.0.1webpages Estweb-inf directory, and copy it from the Jswdk-1.0.1webpagesweb-inf directory. The following four files: Mappings.properties, Mime.properties, Servlets.properties and Webapp.properties. Complete these procedures to notify the JSWDK Web server of executing JSP programs in Http://localhost/test.
VI. JavaBean
One of the attractions of JSP Web pages is the ability to combine JavaBean technology to expand the functionality of programs in a Web page. JavaBean is a Java class (class) that encapsulates properties and methods to become an object that has some functionality or that deals with a business. JavaBean is organized into package (packets) for management, in effect, is to put a group of JavaBean together in a certain directory, the definition of each class plus package So-and-So, in this case, test. Directory test must be placed in the directory contained in the system environment classpath, the system can find the JavaBean. JSWDK adds Jswdk-1.0.1webpagesweb-infjspeans to Classpath in default state. It is also an easy way to set up your own JavaBean and package in this directory.
A simple JavaBean framework is described below. Create a text file Helloworld.java with a text editor and save it in the Jswdk-1.0.1webpagesweb-infjspeans est directory, which reads as follows:
Package test;
public class HelloWorld {
Public String name = ' My name ';
Public String Gethi ()
{
Return "Hello from" + name;
}
}
Helloworld.java edited, in DOS, into the directory Jswdk-1.0.1webpagesweb-infjspeans, with JDK javac command compiled Helloworld.java as follows:
Javac Helloworld.java
Note that Java is case-sensitive, and in a program, the letters in the compilation command line cannot be written in a wrong case. The success of compilation means that a JavaBean is established. Here's how to use this JavaBean in a JSP. Create a text file hi-bean.jsp with a text editor and save it in the Jswdk-1.0.1webpages est directory, which reads as follows:
In the JSP Web page, use the <jsp:usebean .../> syntax to create the JavaBean object and name it Hellobean. The reader can see from this simple example the setting, getting the JavaBean property, and invoking the JavaBean method. Type http://localhost:8080/test/hi-bean.jsp in the address bar of the browser, and get the results as shown in Figure 3. Note that if you modify and recompile the JavaBean program, the results of the modified Web server that need to shut down and restart the JSWDK will not be effective. If you only modify the JSP file, you do not have to restart the JSWDK Web server.
Although, this only completes a very simple JavaBean framework, but follow this framework can design a variety of javabean. For example, accessing data from a JSP is usually done through JavaBean.
Vii. Database Connection
A database connection is the most important part of a dynamic web site, and the technology for connecting to the database in Java is JDBC (Java Database Connectivity). Many database systems have JDBC drivers, and Java programs connect to the database through the JDBC driver, executing queries, fetching data, and so on. Sun also developed JDBC-ODBC Bridge, which uses Java programs to access databases with ODBC drivers, and most database systems now have ODBC drivers, so Java programs can access Oracle, Sybase, MS SQL Databases such as server and MS Access. Here's how to implement a Dynamic FAQ (FAQ and Answer) Web site with access. First, create an Access database Faq.mdb, where the table FAQs has a field ID (AutoIncrement, and set as the primary key), subject (type, length 200), Answers (memo type). This table can contain some common questions and answers about programming knowledge,
Then, add system DSN to the ODBC DataSource module in Control Panel, name the FAQ, and point to Faq.mdb. Create a JavaBean, named Faq.java, and save it in the Jswdk-1.0.1webpagesweb-infjspeans est directory. The contents of Faq.java are as follows:
After compiling Faq.java with the method described in the previous section, create a JSP file faq.jsp under the jswdk-1.0.1webpages est directory, which reads as follows:
Type http://localhost:8080/test/faq.jsp,faq.jsp call JavaBean in the address bar of the browser, read the content from the database and output it.
Limited to space, this article cannot enumerate the complex examples of the jsp-javabean-jdbc/odbc-database, which readers can find and download to the database connection paradigm from the URLs recommended at the end of this article.
VIII. Technical Analysis
Microsoft's ASP technology is also a dynamic web development technology. JSP and ASP form very similar, ASP programmer at a glance can recognize 〈%%> and 〈%=%>. But there are a lot of differences that can be found in deep exploration, the main one being the following three points:
1, JSP efficiency and security is higher
ASP to the source form storage, to explain the way to run, each ASP Web page calls need to explain the source code, running efficiency is not high. In addition, the IIS vulnerability has caused many web site source program exposure, including the author of the previous use of ASP developed Web site, the ASP program has been downloaded to the people.
JSP before execution is compiled into the bytecode (byte code), bytecode by the Java Virtual machine (Java Machine) Interpretation of the execution, more efficient than the source interpretation, the server also has bytecode cache mechanism, can improve the access efficiency of bytecode. The first call to the JSP Web page may be a bit slow because it is compiled into a cache and is much faster later. At the same time, the JSP source program is not likely to be downloaded, especially the JavaBean program can be placed in the wrong directory.
2, the JSP component (Component) way is more convenient
ASP extends complex functions through COM, such as file uploads, sending emails, and separating business processing or complex computing into independent reusable modules. JSP achieves the same function expansion through JavaBean.
In the development aspect, the development of COM is far more complex and tedious than JavaBean, it is not difficult to learn ASP, but it is not easy to learn to develop COM. And JavaBean is much simpler, from the above examples in this article can see that the development of JavaBean is very convenient.
In terms of maintenance, COM must be registered on the server, and if the COM program is modified, it must be registered or even shut down and restarted. JavaBean do not need to register, put in the classpath contained in the directory on the line. If JavaBean is modified, then JSWDK and Tomcat now need to shut down and rerun (but not shut down), but the developer has promised to do so in later versions without shutting down the server.
In addition, JavaBean is a complete oop, can easily set up a reusable object library for different business processing functions, such as user Rights control, email automatic reply and so on.
3, the JSP adapts the platform to be more extensive
ASP is currently available only for NT and IIS. Although there are chilisoft Plug-ins under UNIX to support ASP, the ASP itself has limited functionality and must be augmented by a combination of asp+com, which is difficult to implement under UNIX.
JSP is different, almost all platforms are supported Java,jsp+javabean can be all over the platform to the unimpeded. NT under IIS can support 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 has been able to support JSP. Because Apache is widely used on NT, UNIX, and Linux, JSP has a wider operating platform. Although the NT operating system now occupies a large market share, but the advantages of UNIX in the server is still very large, and the new rise of Linux is not small.
Porting from one platform to another, JSP and JavaBean don't even have to recompile because Java bytecode is standard and platform-independent. The author will be in the NT under the experiment JSP page intact to get Linux under the run up, feel very satisfied.
Ix. Conclusion
To sum up, JSP is a dynamic site to build a sharp weapon, so recommended to the reader, wish you master JSP, to develop excellent website. ASP programmer also Please try, JSP also has session, request, response/out and other objects, try how to personally.
Attached text
Here is an important reference for JSP, interested readers can read further.
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.