You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Barry Jia <bj...@brightspark.com> on 2001/11/02 21:16:56 UTC
How to use Data source in *.jsp
Hi, All
I read the struts document, I config my data source in struts-config.xml,
In my action I can use the following code to get Data source and use it.
Right now I want to use it in my JSP scriplet, when I use it, it can not be
compiled as servlet is a undefined variable or class.
I want to know, in my Action, servlet is in which package ? how to use same
code in *.jsp file
Thanks!
DataSource dataSource = (DataSource)
servlet.getServletContext().getAttribute(Action.DATA_SOURCE_KEY);
conn = dataSource.getConnection();
... perform required functions as in the previous example ...
conn.close();
Re: How to use Data source in *.jsp
Posted by Mark Gordon <ma...@mcgordon.com>.
The datasource is an attribute in application scope. You can use the
pageContext to get this attribute.
You may be able to do something with a <bean:XXXX.> tag or a
<jsp:useBean /> tag to expose this attribute... but I could not figure
it out.
by the way.... you aren't going to put a business rule in a jsp page????
are you ???? :-)
I try to keep all my sql in a bean then call methods to enforce business
rules or pull data to be displayed in a page.
Scriptlets bad, taglibs good :-)
my $.02
-Mark
<%@ page import="javax.sql.DataSource,
org.apache.struts.action.Action,
java.sql.Connection,
java.sql.Statement,
java.sql.ResultSet" %>
<%
DataSource dataSource = (DataSource)
pageContext.getAttribute(Action.DATA_SOURCE_KEY,
PageContext.APPLICATION_SCOPE);
Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select bill_grp_code,
bill_grp_dsc from bill_grp");
/* DO SOMETHING WITH THE DATA */
resultSet.close();
statement.close();
connection.close();
%>
<html>
<body>
testing
</body>
</html>
Barry Jia wrote:
>Hi, All
>I read the struts document, I config my data source in struts-config.xml,
>In my action I can use the following code to get Data source and use it.
>Right now I want to use it in my JSP scriplet, when I use it, it can not be
>compiled as servlet is a undefined variable or class.
>I want to know, in my Action, servlet is in which package ? how to use same
>code in *.jsp file
>Thanks!
>
> DataSource dataSource = (DataSource)
> servlet.getServletContext().getAttribute(Action.DATA_SOURCE_KEY);
> conn = dataSource.getConnection();
> ... perform required functions as in the previous example ...
> conn.close();
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>