JSP technology generates Dynamic Web page _jsp programming

Source: Internet
Author: User
Tags getmessage stmt server memory

With the development of Web technology, Web content is from static pages to content-rich dynamic pages. Dynamic page generation is a challenge for the vast majority of web developers. There are many ways to try to solve this problem, such as plug-in technology and server-side APIs, but one problem is that these methods are targeted at a particular Web server, such as the ASP technology provided by Microsoft for its IIS and personal Web server.
The most popular way to generate dynamic Web pages is CGI, PHP and JavaServer page (JSP) technology. Where CGI uses access to other applications to retrieve information and returns to the browser, CGI programs are usually developed in C or Perl languages, and JSP is implemented dynamically by compiling JSP components into a Java servlet on the server side. The following are a few ways to compare JSP with traditional CGI features:
Portability:
Most Web servers support CGI interfaces, but CGI programs do not operate across platforms; Java servlet programs have the advantage of Java programs that run on any platform, and most Web and application servers support Java and servlet APIs.
Performance on:
In a traditional CGI environment, every client request for a CGI program causes the server to generate a new process for loading and executing CGI programs. A large number of concurrent requests greatly degrade their performance; The JSP does not have this limitation, each program is loaded once and resides in the server memory to request service later. In addition, JSP is much easier to write and control in multithreading than CGI.
Development and release:
Because Java Servelt has all the benefits of Java and is relatively easy to develop, Java simplifies handling of exceptions; its object-oriented nature makes it easy for developers to collaborate. JSP technology from the following aspects to accelerate the development of Dynamic Web site:
1 The development of static pages and dynamic content generation separate
JSP in the use of HTML or XML tags to plan, design the layout and style of Web pages, while using JSP tags to achieve dynamic content, the generation of dynamic content is encapsulated to run on the server side; This page layout and style can be edited and debugged independently without affecting dynamic content generation.
2 Reuse of components
Most JSP pages perform complex requirements by reusing platform-independent component-javabeans. These components can be reused among developers.
3 to simplify page development by marking
Tags are provided through JSPs, and developers can easily use the beans component to set and access their properties. The JSP also allows the user to customize the tag and accept tags developed by the third party, making it easy to use the functional components provided by the third party.
After understanding the characteristics of JSP, then talk about the installation and development of JSP examples. (operating system takes Nt4.0 as an example)
To run the JSP page, you need to install JDK and JSWDK. Install the JDK first and make sure that Javac and Java are working properly after the JDK installation is complete. Then install JSWDK, in fact, simply extract the JSWDK to a directory. In order for the JSWDK to function properly, it is also necessary to modify the environment variable classpath as follows:
If you use JDK1.1, you do not need to modify classpath, and if you use JDK1.2, set the environment variable java_home to point to the JDK installation path.
Once you have done this, you can start the Java webserver.
Executes the Startserver.bat file in the JSWDK directory to start Java server, which uses a default port of 8080. If the Web server is new and needs to be restarted, you need to stop the Web server that was started, and the execution stopserver.bat can stop it.
When JavaServer is started, if normal, enter the address of the browser: http://localhost:8080/can display the page describing the Java Webserver page. In the JSWDK directory there is also a examples directory, which is the JSWDK of a Web application, a Web application contains JSPs, servlets,html files, images and other resources. We can also create a new Web application to put our own relevant files into it. Let's analyze how to create a new Web application.
To create a new Web application, you need a new directory under the JSWDK installation directory, such as creating a Web application called MyWeb, whose directory structure is as follows:



Jswdk_inatall MyWeb Web-inf
Servlets
JSP Beans
Webapp.properties
Servlets.properties
Mime.properties
Mappings.properties
Where: Jswdk_install represents the installation directory of JSWDK, four files with properties suffix are property files and can be copied from Jswkd_install/web-inf. Modify the Webserver.xml file in the Jswdk_install directory and add the following line to map the MyApp to the corresponding mapping: <webapplication id= "Mybase1" mapping= "/myweb" docbase= "MyWeb"/>
Modify the Startserver.bat file at the same time, add the Myweb/web-info/jsp/beans directory to the corresponding environment variable beanjars, or you can do so by adding the path to the CLASSPATH environment variable.
To add a JSP file, you can put the file in the MyWeb directory or its next level directory, to join the servlet, simply place the compiled servlet in the Servlets, and the compiled Java beans in the beans directory.
Note that after you modify the beans or servlet, you need to restart the Web Server for the modifications to take effect.
After you understand these rules, create the first JSP file myfirst.jsp, which reads as follows:
<body>
<% Out.println ("This is the my I-i-i-JSP file"); %>
</body>
Place the file in the Jswkd_install\myweb directory, and if the Web server is started, enter it in the browser address:
http://localhost:8080/myweb/myfirst.jsp
We can see the results of the execution. After completing the first simple JSP file, we can write a Java beans and call the beans in the JSP file.
Java Bean source program Helloworld.java contents are 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 successful compilation, the generated bytecode file Helloworld.class is placed in the Myweb/web-inf/jsp/beans directory;
In the following JSP file test.jsp The helloworld,test.jsp content is invoked as follows:
<title> Jsp and Java Beans </title>
<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>
Place the JSP file in the Jswdk_install\myweb\ directory
Restart the Web Server and enter in the browser address:
http://localhost:8080/myweb/test.jsp
can display the results of execution;
Notice that in the test.jsp
<jsp:usebean id= "Hellobean" scope= "session" class= "HelloWorld"/> The scope = "Sessions" indicates that the object can be created in the same conversation Other page references. If we can reference the object created in test.jsp in aftertest.jsp, the aftertest.jsp content 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, or an exception will occur.
Here's a look at the Java Beans example used to access data in a JSP. The database I am using is Oracle8, the database connection string created by Sql*net is Begpinter, the database server runs on a machine named Begpinterserver, and the following is the contents of Jspjdbc.java:
You are 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.executequery (sql);
}catch (SQLException E1) {
System.err.println ("Error:" + e1.getmessage ());
}
return rset;
}
}
After compiling, put the Jspjdbc.class file into the Myweb\web-inf\jsp\beans directory. In the following JSP file, invoke the beans,jspdb.jsp content as follows:
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title>database in Jsp</title>
<body>



<%@ page language= "java" import= "java.sql.*,oracle.jdbc.driver.*" errorpage= "errorpage.jsp"%>
<jsp:usebean id= "jspdatabase" scope= "page" class= "Jspjdbc"/>
<%
ResultSet RSet = Jspdatabase.executequery ("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>
The errorpage.jsp content used to display the exception is:
<body bgcolor= "Red" >
<%@ page iserrorpage= "true"%>
</body>
Restart the Web server to make the newly created Java beans effective and, if the connection to the data server is normal, enter in the browser address
http://localhost:8080/myweb/jspdb.jsp
The query results are displayed.
Through the above introduction, I believe that we have some understanding of JSP. To learn more about JSP technology, you can access the following sites:
http://java.sun.com/products/jsp
Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.