First, what is JSP JSP (JavaServer Pages) advocated by Sun Microsystems, a dynamic web technology standard established together with many companies, and its URL is http://www.javasoft.com/products/jsp. Jet Java Programs (Scriptlet) and JSP Tag (TAG) in traditional web html files (* .htm, *. Html), the JSP page (* .jsp) is constituted. When the web server encounters a request to access the JSP page, perform the segment of the program, and then return the execution result in the HTML format to the customer. The program clip can operate the database, reordering the web, and send email, etc., which is the functions required to establish a dynamic website. All program operations are executed on the server side. Only the result of the client is transmitted to the client, the lowest requirements for the customer browser, can achieve no PLUGIN, no ActiveX, no java applet, no frame. This article will introduce the method of developing dynamic web pages using JSP technology, and will briefly analyze the difference between JSP technology and Microsoft's ASP technology. Second, how to install and start to experiment JSP technology, first need to establish a running environment, this process is quite simple: 1, download JDK at http://java.sun.com/jdk/ (Java 2 SDK, Standard Edition, v 1.2 .2). 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. Installing the Windows NT environment as an example, JDK installation is first running the downloaded JDK1_2_2_2-win.exe, then modify the system environment parameters, add [x:] / jdk1.2.2 / bin in the PATH parameter and add new Environmental parameter classpath = [x:] / jdk1.2.2 / lib / Tools.jar, where [x:] is a hard disk that installs JDK (C:, D:, etc.). JSWDK installation requires JSwDK1_0_1-win.zip with directory to the root directory (C: /, D: /, etc.), and then you can find /jswdk-1.0.1/ directory on your hard drive. If you don't want to keep JSWDK, you can delete this directory, no system files and registry legacy issues. More detailed installation process and the JDK under Solaris / Unix and Linux, the Tomcat installation can refer to the installation instructions in the obtained file package. 4. Starts the Windows NT environment as an example, execute StartServer.bat in /jswdk-1.0.1/ directory, you can start a web server that supports JSP web technology in JSWDK. In order not to conflict with existing web servers (such as IIS, PWS, etc.), JSWDK's web server uses an 8080 port. After typing http: // localhost: 8080 or http://127.0.0.1:8080, if you can see the JSWDK's welcome page, the JSP experiment environment has been built, and you can enter the next experiment. To turn off the web server, run StopServer.bat. Third, the document directory of the web server included in JSP simple example JSWDK is /jswdk-1.0.1/webpages in the default state, the main document is index.html and index.jsp.jsp in the default state. That is to say, HTTP: // localhost: 8080 is equal to access /jswdk-1.0.1/webpages/index.html.
Create a text file hi.jsp, save a text file hi.jsp, saved in /jswdk-1.0.1/webpages/ directory, its contents are as follows:
<% = msg%> < / h2> body> html> Type http: // localhost: 8080 / hi.jsp, the web server in JSWDK in JSWDK is enclosed in <% and%> in the JSP file in the address bar of the browser. The Java program statement, where output is output to the web page, the function <% = variable | expression%> is to output the value of variables or expressions in the Java scriptlet to the web page, and the results are shown in Figure 1. Figure 1 assigns the variable MSG to the Chinese string, outputs the Chinese string with <% =%>, or outputs the Chinese string in the English version NT4 and RedHat 6.1, and in Chinese NT 4.0 and Chinese 98, then there will be garbled. 4. Unified website interface JSP support server-side files contain, you can insert multiple other files in a JSP file to implement a unified website interface.
Modify the above Hi.jsp and save it as mypage.jsp: <% @ include file = "top.htm"%> <% string msg = "this JSP test."; Out.print ("Hello World!");%>
<% = msg%> h2> <% @ include file = "bot.htm"%> Designing the frame structure of the design website, such as FrontPage, Dreamweave, etc., will design a frame structure file Split into two parts, half of the top is saved as top.htm, and half of the following is stored as bot.htm, the code as shown below: My Home Title> head>
Type http: // localhost: 8080 / mypage.jsp in the browser's address bar, resulting in Figure 2. Figure 2 The interface of the website can be unified, and the designer can concentrate on handling the user login, connect the database, send Email, etc. on the function module. Each JSP file has the following structure: <% @ include file = "top.htm"%> <% // implement some function%> <% @ include file = "bot.htm"%> Maintenance website interface It is relatively easy to modify top.htm and bot.htm, you can affect all web pages. 5. Server parameter settings JSWDK Web server parameters Save in /jswdk-1.0.1/webserver.xml, open and edit this file with Windows Word board and edit this file. You can modify the default setting value. This section is primarily different for JSWDK, the setting method of Tomcat under Linux is slightly different. Jswdk default document directory is /jswdk-1.0.1/webpages, you can build subdirectories in this directory, such as /jswk-1.0.1/webpages/test, you can use http: // localhost / Test Access this directory, in order to enable this subdirectory to execute the JSP program, you must also join: service> section in Web Server.xml: and must establish /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. Complete these procedures can only notify the JSWDK's web server to perform the JSP program in http: // localhost / test. Sixth, one of the JavaBean JSP webpages is one of the functions of JavaBean technology to expand the program in the web page. JavaBean is a Java class (Class) that has a certain function or an object that has some function or handling a service by encapsulation properties and methods. JavaBean is organized into package for management. In fact, putting a set of JavaBeans in a certain directory, plus a Package for each class, this example is TEST. The directory TEST must be placed in the system environment classpath, and the system can find the JavaBean. JSWDK adds /jswk-1.0.1/webpages/web-inf/jsp/Beans/web-inf/jsp/Beans/, in the default state. When you build your own JavaBean and Package, it is not a simple method in this directory. Here is a simple JavaBean framework.
Create a text file helloworld.java with a text editor, and save it in the /jswdk-1.0.1/weBpages/web-inf/jsp/Beans/test directory, its contents are as follows: Package test; public class helloworld {public string name = "My first bean"; public string gethi () {return "Hello from" name;}} HelloWORLD.JAVA After editing, in DOS state, enter the directory /JSWDK-1.0.1/webpages/web-inf/ JSP / Beans /, compiled HelloWorld.java with JDK's Javac commands as follows: Javac HelloWorld.java Note that Java is case sensitive, in the program, the case in the compile command line cannot be written. Compiling success reveals a JavaBean. See how to use this Javabean in JSP. Create a text file hi-bean.jsp with a text editor, and save it in the /jswdk-1.0.1/webpages/test directory, its contents are as follows:
javabean test title> < / head> <% hellobean.name = "JSP "; OUT.PRINT (Hellobean.Gethi ());%> body> html> In the JSP page, use the syntax to create a JavaBean object and name Hellobean. The reader can see the settings from this simple example, get JavaBeaN properties, and call the JavaBean method. Type http: // localhost: 8080 / test / hi-bean.jsp in the address bar of the browser, resulting as shown in Figure 3. Note that if you modify and recompile the JavaBean program, you need to turn off and restart the result of the WEB server that will be modified after the JSWDK will be valid. If you only modify the JSP file, you don't have to restart the JSWDK's web server. Although this only completed a very simple JavaBean framework, you can design a variety of JavaBeans with this framework. For example, access data from JSP is usually implemented by JavaBean. 7. Database Connection Database Connections is the most important part of the dynamic website. The technology connected to the database in Java is JDBC (Java Database Connectivity). Many database systems have a JDBC driver, and the Java program is connected to the database through the JDBC driver, performing queries, extracting data, etc. Sun has also developed JDBC-ODBC Bridge. With this technology Java program, you can access databases with ODBC drivers. Most database systems have an ODBC driver, so Java programs can access such as Oracle, Sybase, MS SQL. Server and MS Access and other databases.
Here is how to implement a dynamic FAQ (FAQ (FAQ) website with Access. First create an Access database FAQ.mdb, where table FAQs have field ID (automatic incremental, and set to primary keyword), Subject (text, length 200), Answers. This table can store common issues and answers to programming knowledge, and then add the System DSN in the ODBC Datasource module of the Control Panel (Control Panel), name 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.
The content of FAQ.java is as follows: 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.executeQuery (sql);} catch (SQLException ex) { System.err.Println ("AQ.Execute Query:" EX.GetMessage ());} Return RS;}} After using the method introduced by the previous section, compiling FAQ.java, in /jswdk-1.0.1/webpages/test Create a JSP file FAQ.JSP in the directory, its contents are as follows:
My FAQ! < / Title> head>
This is my FAQ! b> p> <% @ page language = "java" import = "java.sql. *"%> <% resultset RS = Workm.executeQuery ("SELECT * FROM FAQS"); string tt; while (rs.next ()) {tt = rs.getstring ("Answer"); Out.Print ("
" rs.getstring ("Subject") " li>"); out.print ("
" " pre>");}}} rs.close ();%> In the browser's address bar Type http: // localhost: 8080 / test / faq.jsp, FAQ.jsp call JavaBean, read the content from the database and output, resulting in Figure 5. Show the space,
This article does not enumerate complex examples of JSP-JavaBean-JDBC / ODBC-database, readers can find and download them to database connection examples from the last recommended URL in this article. Eight, technical analysis Microsoft's ASP technology is also dynamic web development technologies. JSP and ASP are very similar to the form, and the ASP programmer recognizes <%%> and <% =%>. But in-depth exploration, you will find a lot of differences, the most important of which have the following three points: 1, JSP efficiency and security ASP is stored in the source code, to interpret how to run, each ASP web page call requires the source code Explanation, the operating efficiency is not high. In addition, the vulnerability of IIS has made many website source procedures, including the IIS, including websites developed by ASP, and the ASP program is all downloaded. JSP first compiled into a zona code before execution, the byte code is explained by Java Virtual Machine, the efficiency of the source code is high; the server is also available on the server, can Improve the access efficiency of the bytecode. The first call JSP web page may be slightly slow because it is compiled into cache, and it will be much better in the future. At the same time, the JSP source program is unlikely to be downloaded, especially the JavaBean program can completely put it in an universe directory. 2. Component's Component is more convenient for ASP to expand complex functions through COM, such as file uploading, email, and separating business processing or complex calculations into independently reusable modules. JSP has achieved the same functional expansion through JavaBean. In terms of development, COM development is far more complex and cumbersome than JavaBean, and it is not difficult to learn ASP, but learn to develop COM is not simple. JavaBean is more simple, and it can be seen from the above example of this article. It is easy to develop JavaBean. In terms of maintenance, COM must register on the server. If the COM program modified, you must re-register, or even shut down and restart. JavaBean does not need to register and put it in the directory contained in ClassPath. If Javabean has modified, JSWDK and Tomcat also need to turn off and re-run (but not shutdown), but developers have promised that they will do not need to turn off the server in future versions. In addition, JavaBean is complete OOP, which can easily establish a complete set of reusable object libraries, such as user rights control, email auto reply, and more. 3. The JSP adaptation platform is widely available only for NT and IIS. Although there is a CHILISoft plug-in to support ASP, the ASP itself is limited, and the combination of ASP COM must be expanded. It is very difficult to implement the COM under UNIX. JSP is different, almost all platforms support Java, JSP JavaBean can pass unimpeded under all platforms. NT IIS can support JSP through a plugin, 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. Since Apache is widely used in NT, UNIX, and Linux, JSP has a wider range of operational platforms. Although the NT operating system now accounts for a large market share, the advantages of UNIX in the server are still very large, and the new rise Linux is not small. Migrating from one platform to another platform, JSP and Javabean do not even recompile because Java bytecodes are standard with platform-independent.