Building Dynamic Web Sites with JSP (3)

Source: Internet
Author: User
Tags command line getmessage modify odbc stmt access database
js| Dynamic Six, 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.1\webpages\web-inf\jsp\beans\ 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.1\webpages\web-inf\jsp\beans\test 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.1\webpages\web-inf\jsp\beans\, 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.1\webpages\test directory, which reads as follows:

<title> JavaBean Test </title>
<body>
<jsp:usebean id= "Hellobean" scope= "session" class= "Test.helloworld"/>
<%= Hellobean.gethi ()%>
<%
Hellobean.name = "JSP";
Out.print (Hellobean.gethi ());
%>
</body>


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.1\webpages\web-inf\jsp\beans\test directory. The contents of Faq.java are 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.executequery:" + ex.getmessage ());
}
Return RS;
}
}


After compiling Faq.java with the method described in the previous section, create a JSP file faq.jsp under the \jswdk-1.0.1\webpages\test directory, which reads as follows:

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> My FAQ! </title>
<body>
<p> <b> This is my faq! </b> </p>
<%@ page language= "java" import= "java.sql.*"%>
<jsp:usebean id= "workm" scope= "page" class= "Test.faq"/>
<%
ResultSet RS = Workm.executequery ("SELECT * from FAQs");
String tt;
while (Rs.next ()) {
tt = rs.getstring ("Answer");
Out.print ("<LI>" + rs.getstring ("Subject") + "</LI>");
Out.print ("<pre>" + TT + "</pre>");
}
Rs.close ();
%>


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.

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.