Download presentation
Presentation is loading. Please wait.
Published byIlene Ford Modified over 9 years ago
1
Examples of Using Servlets and JSP Representation and Management of Data on the Internet
2
Working with Xaln Combining Servlets and XSL
3
Working With XSL Add to the CLASSPATH: –setenv CLASSPATH ${CLASSPATH}:/usr/local/java/apa che/xerces/xerces.jar –setenv CLASSPATH ${CLASSPATH}:/usr/local/java/apa che/xalan/xalan.jar You can download from the Web newer versions of Xalan and Xerces You can download from the Web newer versions of Xalan and Xerces
4
Java Hello Java 19$ XML XML for Beginners 12$ XSL XSL in a Nut Shell 34$ JSP Introduction to JSP 17$ JavaScript A Path to JavaScript 33$ doc.xml
5
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> A dbi Example Books List doc.xsl
6
TransformServlet
7
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();
8
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); }
9
// 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(); }
10
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
11
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 {
12
// 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])); }
13
Using Beans in JSP
14
http://mangal:9999/dbi/jsp/dbconnect.jsp?login=yarok
15
http://mangal:9999/dbi/jsp/dbinsert.jsp?login=yarok
16
http://mangal:9999/dbi/jsp/dbinsertmore.jsp?login=yarok
17
The Bean Object The bean object is under the directory /lib/dbi The bean belongs to the package “dbi”
18
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;
19
public DBBean() { super(); } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; }
20
public boolean connect() throws ClassNotFoundException,SQLException { Class.forName(driver); connection = DriverManager.getConnection(url); return true; } public void close() throws SQLException { connection.close(); }
21
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; } }
22
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
23
First Page
24
Example of using beans in a JSP – Connect <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; %>
25
Created the Table (in the database of the user )
26
<% 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(); %>
27
<% for (int column=1; column <= numColumns; column++) { %> <% } %> <% while(rs.next()) { %> <% } %>
28
Created the Tables <a href="dbinsert.jsp?login= ">Next
29
Second Page
30
Example of using beans in a JSP – Insert <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; %>
31
Inserting Two Rows (in the database of the user )
32
<% 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(); %>
33
<% for (int column=1; column <= numColumns; column++) { %> <% } %> <% while(rs.next()) { %> <% } %>
34
After Insertion <a href="dbinsertmore.jsp?login= ">Next
35
Third Page
36
Example of using beans in a JSP – Insert <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; %>
37
Inserting Two More Rows (in the database of the user )
38
<% 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(); %>
39
<% for (int column=1; column <= numColumns; column++) { %> <% } %> <% while(rs.next()) { %> <% } %>
40
<% db.close(); %> Done
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.