examples of using servlets and jsp representation and management of data on the internet

41
Examples of Using Servlets and JSP Representation and Management of Data on the Internet

Upload: ilene-ford

Post on 28-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

Examples of Using Servlets and JSP

Representation and Management of Data on the Internet

Page 2: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

Working with Xaln

Combining Servlets and XSL

Page 3: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

Working With XSL

• Add to the CLASSPATH:– setenv CLASSPATH ${CLASSPATH}:/usr/local/java/apache/xerces/xerces.jar

– setenv CLASSPATH ${CLASSPATH}:/usr/local/java/apache/xalan/xalan.jar

You can download from the Web newer versions ofXalan and Xerces

You can download from the Web newer versions ofXalan and Xerces

Page 4: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

<?xml version="1.0"?><ROWSET><ROW><Subject>Java</Subject><Title>Hello Java</Title><Price>19$</Price></ROW><ROW><Subject>XML</Subject><Title>XML for Beginners</Title><Price>12$</Price></ROW>

<ROW><Subject>XSL</Subject><Title>XSL in a Nut Shell</Title><Price>34$</Price></ROW><ROW><Subject>JSP</Subject><Title>Introduction to JSP</Title><Price>17$</Price></ROW><ROW><Subject>JavaScript</Subject><Title>A Path to JavaScript</Title><Price>33$</Price></ROW></ROWSET>

doc.xml

Page 5: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

<?xml version="1.0"?><xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" /><xsl:template match="/"><HTML><HEAD><TITLE>A dbi Example</TITLE></HEAD><BODY bgcolor="F0F0F0" text="550000"> <CENTER><H1>Books List</H1></CENTER> <TABLE BORDER="2" ALIGN="center"> <TR><TH></TH>

<xsl:for-each select="ROWSET/ROW[1]/*"><TH><xsl:value-of select="name()" /></TH>

</xsl:for-each> </TR> <xsl:apply-templates select="ROWSET" /> </TABLE></BODY></HTML></xsl:template>

<xsl:template match="ROW"> <TR><TD><xsl:number /></TD>

<xsl:for-each select="*"> <TD><xsl:value-of select="." />&#160;</TD></xsl:for-each>

</TR></xsl:template>

</xsl:stylesheet>

doc.xsl

Page 6: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

TransformServlet

Page 7: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import org.apache.xalan.xslt.*;

public class TransformServlet extends HttpServlet { // Respond to HTTP GET requests from browsers. public void doGet (HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

// Set content type for HTML.response.setContentType("text/html; charset=UTF-8");// Output goes to the response PrintWriter.PrintWriter out = response.getWriter();

Page 8: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

try{ XSLTProcessor processor =

XSLTProcessorFactory.getProcessor(); // Get the XML input document and the stylesheet, // both in the servlet engine document directory. XSLTInputSource xmlSource =

new XSLTInputSource(new java.net.URL("http://www.cs.huji.ac.il/~yarok/doc.xml").openStream());

XSLTInputSource xslSource = new XSLTInputSource(new java.net.URL("http://www.cs.huji.ac.il/~yarok/doc.xsl").openStream());

// Generate the result. XSLTResultTarget target =

new XSLTResultTarget(out);

// Creating the output processor.process(xmlSource, xslSource, target);}

Page 9: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

// If an Exception occurs, //return the error to the client.catch (Exception e) {

out.write(e.getMessage());e.printStackTrace(out);

}// Close the PrintWriter.out.close();

} }

Page 10: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

Creating Results to a File

• A More Simple Example (not a servlet)– Gets input from files where file names given as

parameters– Write output to a file whose name given as a

parameter

Page 11: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

import org.apache.xalan.xslt.XSLTProcessorFactory;import org.apache.xalan.xslt.XSLTInputSource;import org.apache.xalan.xslt.XSLTResultTarget;import org.apache.xalan.xslt.XSLTProcessor;

/** * Simple sample code to show how * to run the XSL processor * from the API. */public class TransformExample{ public static void main(String[] args)

throws java.io.IOException, java.net.MalformedURLException, org.xml.sax.SAXException

{

Page 12: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

{// Have the XSLTProcessorFactory // obtain a interface to a// new XSLTProcessor object.XSLTProcessor processor =

XSLTProcessorFactory.getProcessor();

processor.process(new XSLTInputSource(args[0]), new XSLTInputSource(args[1]), new XSLTResultTarget(args[2])); }}

Page 13: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

Using Beans in JSP

Page 14: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

http://mangal:9999/dbi/jsp/dbconnect.jsp?login=yarok

Page 15: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

http://mangal:9999/dbi/jsp/dbinsert.jsp?login=yarok

Page 16: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

http://mangal:9999/dbi/jsp/dbinsertmore.jsp?login=yarok

Page 17: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

The Bean Object

• The bean object is under the directory <tomcat_home>/lib/dbi

• The bean belongs to the package “dbi”

Page 18: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

package dbi;

import java.sql.*; import java.io.*;

/** * Example of using Java Beans in JSP pages */

public class DBBean { String login = null; String url = "jdbc:oracle:thin:"+ login + "/" +

login + "@sol4:1521:stud"; String driver = "oracle.jdbc.driver.OracleDriver"; private Connection connection;

Page 19: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

public DBBean() { super(); } public String getLogin() {

return login; } public void setLogin(String login) {

this.login = login; }

Page 20: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

public boolean connect()

throws ClassNotFoundException,SQLException { Class.forName(driver); connection = DriverManager.getConnection(url);

return true; } public void close() throws SQLException {

connection.close(); }

Page 21: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

public ResultSet poseQuery(String sql)

throws SQLException {

Statement s = connection.createStatement(); ResultSet rs = s.executeQuery(sql); return rs; } public int applyUpdate(String sql)

throws SQLException {

Statement s = connection.createStatement(); int r = s.executeUpdate(sql); return r; } }

Page 22: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

The JSP Pages

• There are three JSP pages

• The first page opens the connection

• The third page closes the connection

• All three pages using the same bean

Page 23: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

First Page

Page 24: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

<HTML> <HEAD><TITLE>Example of using beans in a JSP – Connect</TITLE></HEAD> <BODY>

<%@ page language="Java" import="java.sql.*" %>

<jsp:useBean id="db" scope="session" class="dbi.DBBean" />

<jsp:setProperty name="db" property="login" param="login" />

<%! ResultSet rs = null ; ResultSetMetaData rsmd = null ; int numColumns ; int i; %>

Page 25: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

<center> <h2> Created the Table </h2> (in the database of the user <jsp:getProperty name="db" property="login"/>)<hr> <br><br> <font color="blue" size="4"><table border=1>

Page 26: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

<% db.connect();

try { db.applyUpdate("DROP TABLE test");} catch (Exception e) {}

try { i = db.applyUpdate("CREATE TABLE test

(name varchar2(10), id number(6))"); rs = db.poseQuery("SELECT * FROM test"); } catch(SQLException e) { throw new ServletException

("Error in the query", e); }

rsmd = rs.getMetaData(); numColumns = rsmd.getColumnCount(); %>

Page 27: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

<TR><% for (int column=1; column <= numColumns; column++) { %> <TD><%= rsmd.getColumnName(column) %> </TD> <% } %>

</TR>

<% while(rs.next()) { %> <TR><TD><%= rs.getString("name") %></TD> <TD><%= rs.getString("id") %></TD></TR> <% } %> </table> </font>

Page 28: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

<P><font color="red" size="6">Created the Tables</font> <P>

<a href="dbinsert.jsp?login=<jsp:getProperty name="db" property="login"/>">Next</a></body>

</HTML>

Page 29: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

Second Page

Page 30: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

<HTML> <HEAD><TITLE>Example of using beans in a JSP – Insert</TITLE></HEAD> <BODY>

<%@ page language="Java" import="java.sql.*" %>

<jsp:useBean id="db" scope="session" class="dbi.DBBean" />

<jsp:setProperty name="db" property="login" param="login" />

<%! ResultSet rs = null ; ResultSetMetaData rsmd = null ; int numColumns ; int i; %>

Page 31: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

<center> <h2> Inserting Two Rows </h2> (in the database of the user <jsp:getProperty name="db" property="login"/>)<hr> <br><br> <font color="blue" size="4"><table border=1>

Page 32: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

<%try{ i = db.applyUpdate(

"INSERT INTO test VALUES ('aaa', 123)"); i = db.applyUpdate(

"INSERT INTO test VALUES ('bbb', 456)"); rs = db.poseQuery("SELECT * FROM test"); } catch(SQLException e) { throw new ServletException

("Error in the query", e); }

rsmd = rs.getMetaData(); numColumns = rsmd.getColumnCount(); %>

Page 33: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

<TR><% for (int column=1; column <= numColumns; column++) { %> <TD><%= rsmd.getColumnName(column) %> </TD> <% } %>

</TR>

<% while(rs.next()) { %> <TR><TD><%= rs.getString("name") %></TD> <TD><%= rs.getString("id") %></TD></TR> <% } %> </table> </font>

Page 34: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

<P><font color="red" size="6">After Insertion</font> <P>

<a href="dbinsertmore.jsp?login=<jsp:getProperty name="db" property="login"/>">Next</a></body>

</HTML>

Page 35: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

Third Page

Page 36: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

<HTML> <HEAD><TITLE>Example of using beans in a JSP – Insert</TITLE></HEAD> <BODY>

<%@ page language="Java" import="java.sql.*" %>

<jsp:useBean id="db" scope="session" class="dbi.DBBean" />

<jsp:setProperty name="db" property="login" param="login" />

<%! ResultSet rs = null ; ResultSetMetaData rsmd = null ; int numColumns ; int i; %>

Page 37: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

<center> <h2> Inserting Two More Rows </h2> (in the database of the user <jsp:getProperty name="db" property="login"/>)<hr> <br><br> <font color="blue" size="4"><table border=1>

Page 38: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

<%try{ i = db.applyUpdate(

"INSERT INTO test VALUES (‘ccc', 789)"); i = db.applyUpdate(

"INSERT INTO test VALUES (‘ddd', 911)"); rs = db.poseQuery("SELECT * FROM test"); } catch(SQLException e) { throw new ServletException

("Error in the query", e); }

rsmd = rs.getMetaData(); numColumns = rsmd.getColumnCount(); %>

Page 39: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

<TR><% for (int column=1; column <= numColumns; column++) { %> <TD><%= rsmd.getColumnName(column) %> </TD> <% } %>

</TR>

<% while(rs.next()) { %> <TR><TD><%= rs.getString("name") %></TD> <TD><%= rs.getString("id") %></TD></TR> <% } %> </table> </font>

Page 40: Examples of Using Servlets and JSP Representation and Management of Data on the Internet

<%

db.close();

%> <BR><font color="red" size="6">Done</font> <P>

</body>

</HTML>

Page 41: Examples of Using Servlets and JSP Representation and Management of Data on the Internet