Upload
ilene-ford
View
216
Download
0
Embed Size (px)
Citation preview
Examples of Using Servlets and JSP
Representation and Management of Data on the Internet
Working with Xaln
Combining Servlets and XSL
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
<?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
<?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="." /> </TD></xsl:for-each>
</TR></xsl:template>
</xsl:stylesheet>
doc.xsl
TransformServlet
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();
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);}
// 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();
} }
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
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
{
{// 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])); }}
Using Beans in JSP
http://mangal:9999/dbi/jsp/dbconnect.jsp?login=yarok
http://mangal:9999/dbi/jsp/dbinsert.jsp?login=yarok
http://mangal:9999/dbi/jsp/dbinsertmore.jsp?login=yarok
The Bean Object
• The bean object is under the directory <tomcat_home>/lib/dbi
• The bean belongs to the package “dbi”
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;
public DBBean() { super(); } public String getLogin() {
return login; } public void setLogin(String login) {
this.login = login; }
public boolean connect()
throws ClassNotFoundException,SQLException { Class.forName(driver); connection = DriverManager.getConnection(url);
return true; } public void close() throws SQLException {
connection.close(); }
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; } }
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
First Page
<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; %>
<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>
<% 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(); %>
<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>
<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>
Second Page
<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; %>
<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>
<%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(); %>
<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>
<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>
Third Page
<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; %>
<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>
<%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(); %>
<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>
<%
db.close();
%> <BR><font color="red" size="6">Done</font> <P>
</body>
</HTML>