With the development of WEB technology, WEB content ranges from static pages to dynamic pages with rich content. Dynamic page generation is a challenge for WEB developers. There are many ways to solve this problem, such as plug-in technology and server-based APIs. But one problem is that these methods are for a specific web server, for example, Microsoft's ASP technology only targets its IIS and Personal web servers.
Currently, CGI, PHP, and JavaServer Page (JSP) technologies are widely used to generate dynamic web pages. Among them, CGI obtains information by accessing other applications and returns it to the browser. CGI programs are usually developed in C or PERL; jsp implements dynamic content by compiling the Jsp component into a Java Servlet running on the server. The following describes the features of Jsp and traditional CGI:
Portability:
Most WEB servers support CGI interfaces, but CGI programs cannot run across platforms. Java Servlet programs have the advantages of JAVA programs and can run on any platform, most WEB and application servers support JAVA and servlet APIs.
Performance:
In the traditional CGI Environment, each client request to the CGI program generates a new process on the server to load and execute the CGI program. A large number of parallel requests greatly reduce its performance. JSP does not have this limitation. Each program is loaded once and resident in the server memory to serve future requests. In addition, JSP is much easier to write and control multiple threads than CGI.
Development and release:
Java Servelt has all the advantages of JAVA, and it is relatively easy to develop. java simplifies Exception Handling. Its object-oriented feature makes developer collaboration a simple task. JSP Technology accelerates dynamic website development from the following aspects:
1) Separate static page development from dynamic content generation
JSP uses HTML or XML tags to plan and design the layout and style of web pages. JSP tags are used to implement dynamic content, the dynamic content is encapsulated and run on the server. In this way, the page layout and style can be edited and debugged separately without affecting the dynamic content generation.
2) Component Reuse
Most JSP pages reuse platform-independent components-javaBeans to meet complex requirements. These components can be reused by developers.
3) Simplify page development by marking
Through JSP, developers can easily use the beans component to set and access their attributes. At the same time, JSP also allows users to customize tags and accept tags developed by third parties to facilitate the use of functional components provided by third parties.
After learning about the features of JSP, let's talk about the installation and Development of JSP instances. (The operating system uses Nt4.0 as an example)
Jdk and jswdk must be installed to run JSP pages. Install jdk first. After jdk is installed, ensure that javac and java work properly. Then install jswdk. In fact, you only need to extract jswdk to a directory. To make jswdk run properly, you also need to modify the environment variable CLASSPATH as follows:
If JDK is used, you do not need to modify CLASSPATH. If JDK is used, set the environment variable JAVA_HOME to point to the JDK installation path.
After completing the preceding steps, you can start Java WebServer.
Run the startserver. bat file in the jswdk directory to start Java Server. The default port used by the Web server is 8080. If the Web server needs to be restarted after it is updated, stop the started Web server and run stopserver. bat to stop it.
After the assumerver is started, if it is normal, enter http: // localhost: 8080/in the browser address to display the Page about Java Webserver Page. There is also an examples directory under the jswdk Directory, which is a self-contained web application in jswdk. A web application contains resources such as jsps, servlets, html files, and images. We can also create a new web application to put relevant files in it. Let's analyze how to create a new web application.
To create a new web application, you must create a directory under the jswdk installation directory. For example, to create a web application named myweb, the directory structure is as follows:
Jswdk_inatall myweb web-inf
Servlets
Jsp beans
Webapp. properties
Servlets. properties
Mime. properties
Mappings. properties
Here: jswdk_install indicates the jswdk installation directory. Four files suffixed with properties are attribute files, which can be copied from jswkd_install/web-inf. Modify the webserver under the jswdk_install directory. add the following line to the xml file to map the myapp: <WebApplication id = "mybase1" mapping = "/myweb" docBase = "myweb"/>
Modify the startserver. bat file and add the myweb/web-info/jsp/beans directory to the corresponding environment variable beanJars. You can also add this path to the CLASSPATH environment variable.
To add a jsp file, you can put the file in the myweb directory or its lower-level directory. To add the servlet, you only need to put the compiled servlet into the servlets; put the compiled java beans in the beans directory.
Note that you need to restart the Web Server to make the modification take effect after you modify the beans or servlet.
After learning about these rules, we will create the first JSP file myfirst. jsp as follows:
<Html>
<Head> <title> My first Jsp file <titile>
</Head>
<Body>
<% Out. println ("this is my first jsp file"); %>
</Body>
</Html>
Place the file in the jswkd_install \ myweb directory. If the Web server has been started, enter:
Http: /localhost: 8080/myweb/myfirst. jsp
We can see the execution result. After completing the first simple jsp file, we can compile a java beans and call the beans in the jsp file.
The content of java bean source code HelloWorld. java is as follows:
Public class helloWorld {
Public String name;
Public boolean setHello (String name ){
This. name = new String (name );
}
Public String sayHello ()
{
Return name;
}
}
Compile the file with JDK:
Javac helloWorld. java
After compilation, put the generated bytecode file HelloWorld. class in the myweb/web-inf/jsp/beans directory;
Call helloWorld in the following jsp file test. jsp. The content of test. jsp is as follows:
<Html>
<Head>
<Title> Jsp and java bean </title>
</Head>
<Body>
<Jsp: useBean id = "helloBean" scope = "session" class = "HelloWorld"/>
<%
String hello = "this is a bean test ";
HelloBean. setHello (hello );
Out. println (helloBean. sayHello () + "<br> ");
%>
</Body>
</Html>
Place the jsp file in the jswdk_install \ myweb \ directory.
Restart the web Server and enter:
Http: /localhost: 8080/myweb/test. jsp
The execution result is displayed;
Note that in test. jsp
<Jsp: useBean id = "helloBean" scope = "session" class = "HelloWorld"/> scope = "session" indicates that the object can be in the same session after being created). For example, we can reference the object created in test. jsp in aftertest. jsp. The content of aftertest. jsp contains the following code:
<%
HelloWorld rebean = (helloWorld) session. getValue ("helloBean ");
Out. println ("bean used in aftertest. jsp" + rebean. sayHello ());
%>
Note that the object to be referenced must have been created; otherwise, an exception occurs.
The following shows an example of using java beans to access data in jsp. The database I use is oracle8. The connection string created through SQL * Net is begpinter. The database server runs on the machine named begpinterserver. The following content of JspJdbc. java:
// You need to import the java. SQL package to use JDBC
Import java. SQL .*;
Import oracle. jdbc. driver .*;
Public class JspJdbc
{
Connection conn = null;
Public ResultSet rset = null;
Public JdbcCheckup (){
// Load the Oracle JDBC driver
Try {
DriverManager. registerDriver (new oracle. jdbc. driver. OracleDriver ());
} Catch (SQLException e1 ){
System. err. println ("executeQuery:" + e1.getMessage ());
}
}
Public ResultSet executeQuery (String SQL ){
Rset = null;
Try {
Conn = DriverManager. getConnection
("Jdbc: oracle: thin: @ bgpinterserver: 1521: bgpinter", "SCOTT", "TIGER ");
Statement stmt = conn. createStatement ();
Rset = stmt.exe cuteQuery (SQL );
} Catch (SQLException e1 ){
System. err. println ("error:" + e1.getMessage ());
}
Return rset;
}
}
After compilation, place the JspJdbc. class file in the myweb \ web-inf \ jsp \ beans directory. Call beans in the following jsp file. The content of jspdb. jsp is as follows:
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> Database in Jsp </title>
</Head>
<Body>
<% @ Page language = "java" import = "java. SQL. *, oracle. jdbc. driver. *" errorPage = "errorpage. jsp" %>
<Jsp: useBean id = "jspdatabase" scope = "page" class = "JspJdbc"/>
<%
ResultSet rset = jspdatabase.exe cuteQuery ("SELECT * FROM emp ");
Out. println ("<table> <tr> <th> number </th> <th> name </th> </tr> ");
While (reset. next ()){
Out. println ("<tr> <td>" + rset. getInt ("eptno") + "</td> ");
Out. println ("<td>" + rset. getString ("enameeptno") + "</td> </tr> ");
}
Rest. close ();
Out. println ("</table> ");
%>
</Body>
</Html>
The error page. jsp used to display exceptions is as follows:
<Html>
<Body bgcolor = "red">
<% @ Page isErrorPage = "true" %>
<H1> The exception <% = exception. getMessage () %>
</Body>
</Html>
Restart the Web server to make the newly created java beans take effect. If the connection to the data server is normal, enter
Http: // localhost: 8080/myweb/jspdb. jsp
The query result is displayed.
Through the above introduction, I believe you know something about Jsp. For more information about Jsp technology, visit the following sites:
Http://java.sun.com/products/jsp