Using a DataSource from WebLogic in a JSP
By: Bruce W. Perry in JSP Tutorials on 2008-11-24
Example below imports the necessary classes at the top of the code using the page directive and its import attribute. Otherwise, this JSP accomplishes everything that the servlet of the prior recipe does, including the display of nearly identical output in the web browser.
Using a JSP scriptlet to access a WebLogic DataSource
<%@ page import="java.util.Hashtable,java.sql.*,javax.naming.*,javax.sql.*" %> <html> <head><title>Database Query in WebLogic</title></head> <body> <h2>Querying a database with a JSP in WebLogic</h2> <% Context env = null; DataSource pool = null; Hashtable ht = new Hashtable(); ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); ht.put(Context.PROVIDER_URL,"t3://localhost:7001"); env = new InitialContext(ht); //Lookup this DataSouce at the top level of the WebLogic JNDI tree pool = (DataSource) env.lookup ("oracle-8i-athletes"); String sql = "select * from athlete"; Connection conn = null; Statement stmt = null; ResultSet rs = null; ResultSetMetaData rsm = null; %> <table border='1'><tr> <% try{ //get a java.sql.Connection from the pool conn = pool.getConnection(); stmt = conn.createStatement();//create a java.sql.Statement //execute a SQL statement,generating a ResultSet rs = stmt.executeQuery(sql); rsm = rs.getMetaData(); int colCount = rsm.getColumnCount(); //print column names for (int i = 1; i <=colCount; ++i) { %> <th><%=rsm.getColumnName(i)%> </th> <% } %> </tr> <% while( rs.next()){ %> <tr> <% for (int i = 1; i <=colCount; ++i) { %> <td> <%= rs.getString(i) %> </td> <%}//for %> </tr> <%} //while } catch (Exception e) { throw new JspException(e.getMessage()); } finally { try{ stmt.close(); conn.close(); } catch (SQLException sqle){ } } %> </body> </html>
After making sure that you have properly configured the connection pool and DataSource in the WebLogic console, view this JSP's output by copying it to WebLogic's default web application, then request a URL in your browser that looks like this one: http://localhost:7001/sqlWeblogic.jsp.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
Comments