User Tools

Site Tools


classes:general:dbsamples:jsp_oracle

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
classes:general:dbsamples:jsp_oracle [2017/01/14 19:50] – [Connecting using a Bean] localadminclasses:general:dbsamples:jsp_oracle [2017/01/14 19:52] (current) – [The Class (Bean)] localadmin
Line 72: Line 72:
 <code> <code>
 package beans; package beans;
- 
  
 import java.io.*; import java.io.*;
Line 122: Line 121:
  
 Once compiled, this class needs to be placed in the folder: /home/username/public_html/WEB-INF/classes/<packagename>/ where '<packagename>' is the name of the package your class belongs to. In this case the package name is 'beans' Once compiled, this class needs to be placed in the folder: /home/username/public_html/WEB-INF/classes/<packagename>/ where '<packagename>' is the name of the package your class belongs to. In this case the package name is 'beans'
 +
 +==== The JSP page ====
 +
 +The JSP page will be placed in the public_html directory, and can be reached through the URL http://students.cs.ndsu.nodak.edu/~uuid/mypage.jsp
 +
 +To use the database functions defined in your bean, you need to use a special JSP directive. 
 +
 +You can import packages using the syntax:
 +<code>
 +<%@page import="java.util.*"%>
 +</code>
 +This imports java.util.*
 +
 +To import your bean, use the syntax:
 +<code>
 +<jsp:useBean id="dbcon" scope="request" class="beans.DBCon" />
 +</code>
 +
 +This defines an object called 'dbcon'. It has a scope of 'request', meaning that the dbcon object is the same throughout the request (options for scope include 'page', 'request', 'session'). The attribute 'class' tells the server which class to use for this object.
 +
 +This is similar to the java:
 +<code>
 +DBCon dbcon = new DBCon();
 +</code>
 +
 +in that it makes a new object for code the use. The difference is that jsp:useBean is used for JSP pages to load Beans.
 +
 +Here is a small JSP page that loads the DBCon bean and calls the function getRecords(). Note that we do not specify ANY database connection information here, that's taken care of in the DBCon bean. 
 +
 +<code>
 +<html>
 +<head></head>
 +<body>
 +
 +<%@page import="java.util.*"%>
 +<jsp:useBean id="dbcon" scope="request" class="beans.DBCon" />
 +
 +<%
 +        // Get the first item of each record by calling a function of the DBCon class
 +        ArrayList records = dbcon.getRecords();
 +
 +        // Go through each record and print
 +        for(int i = 0; i < records.size(); i++)
 +        {
 +                // Print out each item in the 'records' array
 +                out.println(" item: " + (String)records.get(i));
 +                out.println("<br/>");
 +        }
 +
 +%>
 +</body>
 +</html>
 +</code>
  
  
classes/general/dbsamples/jsp_oracle.1484445020.txt.gz · Last modified: 2017/01/14 19:50 by localadmin