Tomcat 5 5 Download current version of Tomcat

  • Slides: 23
Download presentation
Tomcat 5. 5 • Download current version of Tomcat from: http: //jakarta. apache. or

Tomcat 5. 5 • Download current version of Tomcat from: http: //jakarta. apache. or g/tomcat/ • Install it in: • C: Program FilesApache Software FoundationTomcat 5. 5 • Test it: • http: //localhost: 8080/ • Directory structure bin common ROOT 1 webapps lib … …

An simple JSP example: test. jsp <html> <%java. util. Date date=new java. util. Date();

An simple JSP example: test. jsp <html> <%java. util. Date date=new java. util. Date(); %> <head> A Test of JSP </head> <body> <p><b> Welcome! Today is: </b></p> <%=date. get. Year()+1900%>. <%=date. get. Month()+1%>. <%=date. get. Date()%> </body> </html> 2

An simple JSP example (con’t) Edit it using an editor, e. g. Word. Pad.

An simple JSP example (con’t) Edit it using an editor, e. g. Word. Pad. Save it in: C: Program FilesApache Software FoundationTomcat 5. 5webappsROOTtest. jsp Start Tomcat: C: Program FilesApache Software FoundationTomcat 5. 5bintomcat 5 Open a Web browser and go to: http: //localhost: 8080/test. jsp 3

Running JSP Page Request no JSP Servlet Current? • JSP compilation process: parse, compile,

Running JSP Page Request no JSP Servlet Current? • JSP compilation process: parse, compile, load and execute Parse JSP and Create Java Source File yes JSP Servlet in Memory? no yes Load the JSP servlet Execute the JSP servlet 4 JSP Page Response Compile Java Source File

Figure 14 -6: General. Table. jsp <!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4.

Figure 14 -6: General. Table. jsp <!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN"> <%@ page import="java. sql. *" %> <HTML> <HEAD> <TITLE> Table Display Using JDBC and My. SQL</TITLE> <META NAME="author" CONTENT="Oscar LIN"> <META NAME="Keywords" CONTENT="JSP, JDBC, Database Access. "> <META NAME="description" CONTENT="an example of displaying a table using JSP. "> <LINK REL=STYLESHEET HREF="JSP-Style. css" TYPE="text/css"> </HEAD> <BODY> <H 2> Database Access Example</H 2> <%String var. Table. Name= request. get. Parameter("Table"); var. Table. Name = var. Table. Name. to. Upper. Case(); %> <H 3> Showing Data from My. SQL Database db 1</H 3> 5

<% try { // Load JDBC driver Class. for. Name("sun. jdbc. odbc. Jdbc. Odbc.

<% try { // Load JDBC driver Class. for. Name("sun. jdbc. odbc. Jdbc. Odbc. Driver"). new. Instance(); String conn. String="jdbc: odbc: test"; System. out. println("Trying connection with "+conn. String); Connection conn = Driver. Manager. get. Connection(conn. String); Statement stmt = conn. create. Statement(); String var. SQL = "SELECT * FROM " + var. Table. Name; Result. Set rs = stmt. execute. Query(var. SQL); Result. Set. Meta. Data rs. Meta = rs. get. Meta. Data(); %> <TABLE BORDER=1 BGCOLOR=#ffffff CELLSPACE=5><FONT FACE="Arial" COLOR=#000000> <CAPTION><B> <%=var. Table. Name%> </B></CAPTION></FONT> <THEAD> 6

<TR> <% String var. Col. Names = ""; int var. Col. Count = rs.

<TR> <% String var. Col. Names = ""; int var. Col. Count = rs. Meta. get. Column. Count(); for (int col =1; col <= var. Col. Count; col++) { %> <TH BGCOLOR=#c 0 c 0 c 0 BORDERCOLOR=#000000 ><FONT SIZE=2 FACE="Arial" COLOR=#000000> <%=rs. Meta. get. Column. Name(col) %> </FONT>  </TH> <% }%> </TR> </THEAD> <TBODY> <% while (rs. next()) { %> <TR VALIGN=TOP> <% for (int col=1; col<=var. Col. Count; col++) { %> <TD BORDERCOLOR=#c 0 c 0 c 0 ><FONT SIZE=2 FACE="Arial" COLOR=#000000 > <%=rs. get. String(col) %> <BR> </FONT> </TD> <% } } 7

//clean up rs. close(); stmt. close(); conn. close(); } catch (Class. Not. Found. Exception

//clean up rs. close(); stmt. close(); conn. close(); } catch (Class. Not. Found. Exception e) { out. println("Driver Exception " + e); } %> </TR> </TBODY> <TFOOT></TFOOT> </TABLE> </BODY> </HTML> Example: Run it! 8

Java Bean Model 1. 9

Java Bean Model 1. 9

JSP invoking Java Classes (Java Beans) • Important and Useful: – It separates the

JSP invoking Java Classes (Java Beans) • Important and Useful: – It separates the tasks of writing program logic from generating HTML; – It Reduces the complexity of managing a Web site. • Java Bean – A properly mannered Java class; – A Java class that has three properties: • there are no public instance variables; • All persistent values xxx are accessed using methods named getxxx and setxxx • Bean classes must either have no constructors or it must have one explicitly defined zeroargument constructor 10

An example using Java Bean Save it into: C: Program FilesApache Software FoundationTomcat 5.

An example using Java Bean Save it into: C: Program FilesApache Software FoundationTomcat 5. 5webappsROOTWEB-INFclasses Compile it: >javac Customer. Insert. Bean. java We get Customer. Insert. Bean. class Edit New. Customer. html Save it into: C: Program FilesApache Software FoundationTomcat 5. 5webappsROOT Edit Customer. Insert. Using. Bean. jsp Save it into C: Program FilesApache Software FoundationTomcat 5. 5webappsROOT Start Tomcat C: Program FilesApache Software FoundationTomcat 5. 5bintomcat 5 11

An Example using Java Bean and JDBC JSP: Display the result set retrieved from

An Example using Java Bean and JDBC JSP: Display the result set retrieved from Table Testinfo in the database db 1 Use the Java Bean DBUtil_My. SQL. class Java: Load the JDBC driver Set up the connection Get the Result. Set 12

An example using Java Bean and My. SQL // A Java Bean source code:

An example using Java Bean and My. SQL // A Java Bean source code: DBUtil_My. SQL. java package lin; import java. io. *; import java. sql. *; It is necessary to use a package, Otherwise something will go wrong. public class DBUtil_My. SQL { // For My. SQL String s. DBDriver="com. mysql. jdbc. Driver"; String s. Conn. Str="jdbc: mysql: //localhost: 3307/"+"db 1"+"? user=root"; // for MS ACCESS //String s. DBDriver="sun. jdbc. odbc. Jdbc. Odbc. Driver"; //String s. Conn. Str="jdbc: odbc: test"; Connection conn = null; Result. Set rs = null; public DBUtil_My. SQL() { try { //java. sql. Driver. Manager. register. Driver(); Class. for. Name(s. DBDriver); } catch(java. lang. Class. Not. Found. Exception e) { System. err. println("test. JDBC(): " + e. get. Message()); 13 } }

An example using Java Bean and My. SQL public Result. Set execute. Query(String test.

An example using Java Bean and My. SQL public Result. Set execute. Query(String test. JDBC) { rs = null; try { conn=Driver. Manager. get. Connection(s. Conn. Str); Statement stmt = conn. create. Statement(); rs=stmt. execute. Query(test. JDBC); } catch(SQLException ex) { System. err. println("aq. execute. Query: "+ex. get. Message()); } return rs; } } Save it in: webappsROOTWEB-INFclassesDBUtil_My. SQL. java >javac –d DBUtil_My. SQL. java Copy the file we got from the compilation DBUtil_My. SQL. class in C: lin to C: Program FilesApache Software FoundationTomcat 5. 5webappsROOTWEBINFclasseslin 14

test. DB_My. SQL. jsp <html> <head> <meta http-equiv="Content-Type" content="text/html; <% charset=gb 2312"> while (rs.

test. DB_My. SQL. jsp <html> <head> <meta http-equiv="Content-Type" content="text/html; <% charset=gb 2312"> while (rs. next()) <title> MY FAQ</title> { str_name=rs. get. String("name"); </head> str_gender = rs. get. String("gender"); <body> out. println(" name is: " + str_name); <p><b> This is my name! </b> </p> out. println(" gender is: " + <%@ page language="java" import ="java. sql. *" page. Encoding="gb 2312" str_gender); %> %> <p> </p> <% <jsp: use. Bean id='work. M' class="lin. DBUtil_My. SQL" /> } <jsp: set. Property name="work. M" property="*" /> } RS. close(); %> <% </body> Result. Set RS = work. M. execute. Query("SELECT * </html> FROM testinfo"); while (RS. next()) { Result. Set rs = work. M. execute. Query("SELECT * Save it to: FROM testinfo"); C: Program Files String str_name, str_gender; Apache Software Foundation out. println("Get data from a database: : "); Tomcat 5. 5webappsROOT %> <p> </p> Go to: 15 http: //localhost: 8080/test. DB_My. SQL. jsp

My. SQL • Downloading My. SQL – An open-source DBMS product that runs on

My. SQL • Downloading My. SQL – An open-source DBMS product that runs on Unix, Linux, and Windows • Provides quick and efficient query handling • Allows you to create users, databases, tables, auto incrementing fields, etc. – The My. SQL open-source driver may be downloaded from: http: //worldserver. com/mm. mysql 16

 • Get and Install a Recent Version • Download the 4. 0 and/or

• Get and Install a Recent Version • Download the 4. 0 and/or 4. 1 versions of the My. SQL win 32 distribution. • Find the downloaded file, unzip it and start the setup program: • By referring to http: //www. analysisandsolutions. com/co de/mybasic. htm • Install it to C: Program Filesmysql 40 • Start it: C: Program Filesmysql 40bin 17

– Limitations • While My. SQL is extremely inexpensive and provides many capabilities, it

– Limitations • While My. SQL is extremely inexpensive and provides many capabilities, it is not as powerful as commercial products, such as Oracle and SQL Server – Using My SQL • My. SQL commands: use, show, describe 18

Using My. SQL • • >mysql –P 3307 –u root mysql >Show databases; >s.

Using My. SQL • • >mysql –P 3307 –u root mysql >Show databases; >s. How databases; (OK) >Use db 1; >Show tables; >Describe testinfo; >Select * from testinfo; Or • >q • Edit sql. txt • >bin>mysql –P 3307 –u root mysql <C: wansql. txt 19

My. SQL Connector/J – The latest version of the driver is My. SQL Connector/J

My. SQL Connector/J – The latest version of the driver is My. SQL Connector/J (formerly MM. My. SQL driver) • is a Java driver that converts JDBC calls into the network protocol used by the My. SQL database. • is a Type 4 JDBC driver and has a complete JDBC feature set that supports the capabilities of My. SQL. • Download the driver from • http: //dev. mysql. com/downloads/connector/j/3. 1. ht ml • Unzip it to get the jar file: mysql-connector-java 3. 0. 16 -ga-bin. jar • copy it to a public place: • …Tomcat 5. 5commonlibmysql-connector-java-3. 0. 16 -ga-bin. jar 20

– JDBC connections • JDBC connection to My. SQL is different from other user

– JDBC connections • JDBC connection to My. SQL is different from other user connections – Same machine: via socket – Different machines: via TCP/IP 21

– A work-around allows you to grant access to the database@’%’ for the specific

– A work-around allows you to grant access to the database@’%’ for the specific account » The wildcard % states that the database may be accessed from any host » Alternatively, you may supply the specific IP address for your local machine –this provides better security – Concurrency control • Limited support: read/write locks – Backup and recovery • Limited support: database/table saving, log files 22

Summary • JDBC – – JDBC driver types Java Servlet and Applets JDBC architecture

Summary • JDBC – – JDBC driver types Java Servlet and Applets JDBC architecture and components Applications of JDBC • JSP (Java Server Pages) – JSP, Servlets and Apache Tomcat • My. SQL – My. SQL and JDBC Connections; Concurrency control, and backup and recovery • Questions? 23