对于如何使用给定的JNDI名称连接到Websphere数据源?感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍webspherejndi数据源配置,并为您提供关于(转)WebSphere的web
对于如何使用给定的JNDI名称连接到Websphere数据源?感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍websphere jndi数据源配置,并为您提供关于(转)WebSphere的web工程中怎么获取数据源、android – 如何使用Webservices连接到远程数据库?、asp.net – 远程连接到WebDev.WebServer.exe、HTML – 如何将我的CSS连接到存储在WEB-INF文件夹中的JSP文件?的Websphere/JSP的有用信息。
本文目录一览:- 如何使用给定的JNDI名称连接到Websphere数据源?(websphere jndi数据源配置)
- (转)WebSphere的web工程中怎么获取数据源
- android – 如何使用Webservices连接到远程数据库?
- asp.net – 远程连接到WebDev.WebServer.exe
- HTML – 如何将我的CSS连接到存储在WEB-INF文件夹中的JSP文件?的Websphere/JSP
如何使用给定的JNDI名称连接到Websphere数据源?(websphere jndi数据源配置)
我正在使用Websphere Portal 7.0并使用RAD
8.0创建一个portlet。我的portlet试图建立到远程服务器的db2连接。我在本地编写了一个Java程序来与服务器建立基本的JDBC连接,并从表中获取记录。代码工作正常;但是,当我将代码以及db2jcc4.jar添加到我的portlet时,连接不起作用。我正在使用基本的:
Connection connection = DriverManager.getConnection("jdbc:db2://server:port/db:user=user;password=pw;");
我认为使用Websphere数据源是正确的方法。我知道数据源的JNDI名称,但是我找不到关于如何建立连接的清晰示例。几个示例使用了一个DataSource类(我在其中键入了它,这似乎不是来自本机Java包,所以我在这里使用什么导入?)与Context结合使用。我遇到过类似的代码:
Context ctx = new InitialContext();ctx.lookup("jdbc/xxxx");
…有人可以帮我分解一下吗?
编辑1
我已经按照列出的答案更新了代码。我真的觉得我越来越近了。这是我的getConnection()方法:
private Connection getConnection() throws SQLException { javax.naming.InitialContext ctx = null; javax.sql.DataSource ds = null; System.out.println("Attempting connection..." + DateUtil.now() ); try { ctx = new javax.naming.InitialContext(); ds = (javax.sql.DataSource) ctx.lookup("java:comp/env/jdbc/db"); connection = ds.getConnection(); } catch (NamingException e) { System.out.println("peformanceappraisalstatus: COULDN''T CREATE CONNECTION!"); e.printStackTrace(); } System.out.println("connection: " + connection.getClass().getName() + " at " + DateUtil.now()); return connection;}
我的整个web.xml文件如下所示:
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>PeformanceAppraisalStatus</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <resource-ref> <description> Datasource connection to Db</description> <res-ref-name>jdbc/db</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> <res-sharing-scope>Shareable</res-sharing-scope> </resource-ref></web-app>
我看到一个错误,该错误描述了你们在告诉我Websphere应该提示我做的事情,但没有这样做:
SRVE0169I: Loading Web Module: PeformanceAppraisalStatus.[8/23/11 18:08:02:166 CDT] 00000009 InjectionProc E CWNEN0044E: A resource reference binding could not be found for the jdbc/db resource reference, defined for the PeformanceAppraisalStatus component.[8/23/11 18:08:02:169 CDT] 00000009 InjectionEngi E CWNEN0011E: The injection engine failed to process bindings for the metadata.
是的,我知道我将整个应用程序的性能误认为是性能。
解
我离我很近。这是使所有内容都正确的缺失位:
web.xml:<resource-ref> <description> Datasource connection to db</description> <res-ref-name>jdbc/db</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> <res-sharing-scope>Shareable</res-sharing-scope> <mapped-name>jdbc/db</mapped-name> </resource-ref>ibm-web-bnd.xml:<?xml version="1.0" encoding="UTF-8"?><web-bnd xmlns="http://websphere.ibm.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd" version="1.0"> <virtual-host name="default_host" /> <resource-ref name="jdbc/db" binding-name="jdbc/mydatasource" /></web-bnd>
看来ibm-web-bnd.xml文件处理了Websphere中项目资源名称和数据源之间的绑定。添加行后:
<resource-ref name="jdbc/db" binding-name="jdbc/mydatasource" />
Websphere Portal似乎很安逸。我的代码正在运行,现在正在连接到数据库。
答案1
小编典典您需要在应用程序中定义 资源引用 ,然后在部署期间将该逻辑资源引用映射到物理资源(数据源)。
在您的中web.xml
,添加以下配置(适当地修改名称和属性):
<resource-ref> <description>Resource reference to my database</description> <res-ref-name>jdbc/MyDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> <res-sharing-scope>Shareable</res-sharing-scope></resource-ref>
然后,在应用程序部署期间,WAS将提示您将该资源引用(jdbc/MyDB
)映射到您在WAS中创建的数据源。
在您的代码中,您可以获得与示例中所示类似的数据源。但是,用于查找它的JNDI名称实际上应该是您定义的资源引用的名称(res-ref-name
),而不是物理数据源的JNDI名称。另外,您需要在res-ref-name前面加上应用程序命名上下文(java:comp/env/
)。
Context ctx = new InitialContext();DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/MyDB");
(转)WebSphere的web工程中怎么获取数据源
原文:http://aguu125.iteye.com/blog/1694313
https://blog.csdn.net/bigtree_3721/article/details/44900325-------JNDI之java:comp/env
was配置数据源和tomcat是不同的。tomcat只需要配置tomcat 的service.xml或者content.xml,然后 WEB程序就不需要配置了。但是was不同.was 除了在控制台配置数据源后,还需要在web.xml 和WEB-IBN.XML中配置
websphere 下获取jndi,有两种方式:java:comp/env/cas与jdbc/cas。 A.lookup("java:comp/env/cas")与lockup("jdbc/cas")在websphere中都可以使用。两者的差别在于,java:comp/env/cas是websphere建议使用的方式 。
如果你当前的线程属于websphere的线程,建议使用java:comp/env/cas的方式,否则was的控制台将报出警告。
在web程序中,要实现通过java:comp/env/cas的方式来获得jndi必须在web.xm和ibm-web-bnd.xmi文件里分别添加
web.xml:
-
<resource-ref id="ResourceRef_1129470735234">
-
<res-ref-name>cas_ase</res-ref-name>
-
<res-type>java.sql.DataSource</res-type>
-
<res-auth>Container</res-auth>
-
<res-sharing-scope>Shareable</res-sharing-scope>
-
</resource-ref>
ibm-web-bnd.xmi:
-
<resRefBindings xmi:id="ResourceRefBinding_1129470735234"
-
jndiName="jdbc/cas_ase">
-
<bindingResourceRef href="WEB-INF/web.xml#ResourceRef_1129470735234" />
-
</resRefBindings>
以上两段配置的意思是告诉web容器的上下文环境,将应用映射到的jndi资源。然后就可以通过lookup(“java:comp/env/cas/jdbc/cas_ase”)名来获得数据源。
然而,如果你当前执行的线程不在was的容器内,比如说你通过web容器的线程新起了一个子线程,那么该线程将不在容器的上下文内,通过lookup(“java:comp/env/cas/jdbc/cas_ase”)名来获得数据源
将报错,这个时候你只能使用通用的获取jndi资源的方式,就是通过lookup(“jdbc/cas”)来实现。“jdbc/cas”为你在was的资源设定的jndi名
java代码:
-
package com;
-
-
import java.sql.Connection;
-
import java.util.Hashtable;
-
-
import javax.naming.Context;
-
import javax.naming.InitialContext;
-
import javax.naming.NamingException;
-
import javax.sql.DataSource;
-
-
public class conn {
-
-
public static void main(String[] args) throws Exception {
-
InitialContext initialContext = getInitialContext();
-
javax.sql.DataSource ds = (DataSource) initialContext.lookup( "jdbc/cas_ase");
-
Connection cn = ds.getConnection();
-
if (cn != null){
-
System.out.println( "Connection ok");
-
}
-
}
-
-
/* 因为此类不在Websphere服务器内部运行,所以需要配置环境变量,否则是可以省略的 */
-
public static InitialContext getInitialContext() throws NamingException {
-
Hashtable env = new Hashtable();
-
-
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
-
env.put(Context.PROVIDER_URL, "iiop://localhost:2809"); // iiop是协议
-
InitialContext context = new InitialContext(env);
-
-
return context;
-
}
-
-
}
关于JNDI可以看:http://blog.csdn.net/lan861698789/article/details/26402935
关于web.xml中参数的解释:
resource-ref元素用于指定对外部资源的servlet引用的声明。
<!ELEMENT resource-ref (description?, res-ref-name,
-
res-type, res-auth, res-sharing-scope?)>
-
<!ELEMENT description (#PCDATA)>
-
<!ELEMENT res-ref-name (#PCDATA)>
-
<!ELEMENT res-type (#PCDATA)>
-
<!ELEMENT res-auth (#PCDATA)>
-
<!ELEMENT res-sharing-scope (#PCDATA)>
resource-ref子元素的描述如下:
● res-ref-name是资源工厂引用名的名称。该名称是一个与java:comp/env上下文相对应的JNDI名称,并且在整个Web应用中必须是惟一的。
● res-auth表明:servlet代码通过编程注册到资源管理器,或者是容器将代表servlet注册到资源管理器。该元素的值必须为Application或Container。
● res-sharing-scope表明:是否可以共享通过给定资源管理器连接工厂引用获得的连接。该元素的值必须为Shareable(默认值)或Unshareable。
android – 如何使用Webservices连接到远程数据库?
我的Android APP必须连接到Web服务,并且Web服务将执行逻辑操作以从D.B.获取必要的数据.并将它们发送给我.
好的,Web服务不是问题,我有一个朋友会这样做(我不知道Web服务),但我不知道我如何与Web服务建立联系,我也不知道如何必须是Web服务的功能.
我需要做选择,给我多行信息,并选择,然后我必须发送数据到Webservice(“选择和插入”的参数),我简单地调用webservice的函数与正常参数?或者它比这更难?我已经搜索了教程,告诉我在谷歌上这样做,但我找不到一个很好的教程,说明如何做到这一点…
有人能给我一点帮助吗?例如,一个很好的教程,初学者将android连接到远程数据库与webservices?
谢谢
解决方法
关于如何连接到Web服务,只需提供您要连接的URL并传递参数. String urlstr =“www.yoursite.com/api.PHP?parameter1=”parameter1“& parameter2 =”parameter2;
URL updateURL = new URL(urlstr); URLConnection conn = updateURL.openConnection(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(100); int current = 0; while((current = bis.read()) != -1){ baf.append((byte)current); } String html = new String(baf.toByteArray());
this链接可能会清楚地说明您如何在应用程序中使用互联网数据.
asp.net – 远程连接到WebDev.WebServer.exe
>运行IE8.
>需要在IE6下测试网站.
>由IE8安装破坏的MultipleIE6安装(不能输入文本框,是的,我删除缓存,我重新注册了dll的).
>创建运行IE6的VPC.
>无法连接到主机WebDev.WebServer.exe.
有没有办法配置WebDev.WebServer.exe,以便它将接受远程连接?
解决方法
示例配置:
>配置privoxy以监听IP
可以从您的VM路由的地址
(例如192.168.1.1:8118).您可以在主机操作系统的looback上放置IP地址,并与客户端操作系统一起使用NAT.
>在虚拟机中配置您的浏览器以使用
192.168.1.1:8118代表所有连接,包括localhost.
>在webdev.webserver中启动你的应用程序
>使用您的VM浏览器访问与主机操作系统上的浏览器相同的URL(例如http:// localhost:3254)
从webdev.webserver的角度来看,请求将来自于127.0.0.1上的privoxy,并将乐意为他们提供服务.
UPDATE这几天,我正在使用fiddler2. fiddler在Tools>中有一个选项选项>连接到“允许远程计算机连接”.但也要注意,IISExpress可以配置为接受远程连接.
HTML – 如何将我的CSS连接到存储在WEB-INF文件夹中的JSP文件?的Websphere/JSP
<link rel="stylesheet" href = "css/styles.css">
我没有运气让我的CSS显示……
我错过了什么?
解决方法
其中一种方法是为这些资源使用域相对路径,即以/开头.您可以使用${pageContext.request.contextpath}动态内联当前webapp的上下文路径.
<link rel="stylesheet" href="${pageContext.request.contextpath}/css/styles.css">
这将最终生成的HTML输出如下:
<link rel="stylesheet" href="/yourcontextpath/css/styles.css">
这样浏览器就可以正确下载它们.
也可以看看:
> Browser can’t access/find relative resources like CSS,images and links when calling a Servlet which forwards to a JSP
今天关于如何使用给定的JNDI名称连接到Websphere数据源?和websphere jndi数据源配置的介绍到此结束,谢谢您的阅读,有关(转)WebSphere的web工程中怎么获取数据源、android – 如何使用Webservices连接到远程数据库?、asp.net – 远程连接到WebDev.WebServer.exe、HTML – 如何将我的CSS连接到存储在WEB-INF文件夹中的JSP文件?的Websphere/JSP等更多相关知识的信息可以在本站进行查询。
本文标签: