错误:javax.naming.NoInitialContextException:无法使用哈希表

时间:2016-05-26 13:17:00

标签: java jsp initial-context

我在JSP下面。



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*"%>
<%@page import="javax.sql.*"%>
<%@page import="java.util.*"%>
<%@page import="javax.naming.*"%>
<%@page import="java.io.*" %>

<html>
<head>
<%!
	class LinkedProperties extends Properties {

		private final LinkedHashSet<Object> keys = new LinkedHashSet<Object>();

		public Enumeration<Object> keys() {
			return Collections.<Object>enumeration(keys);
		}

		public Object put(Object key, Object value) {
			keys.add(key);
			return super.put(key, value);
		}
	}

	public String getData(ResultSet rs, String tableName) throws Exception {
		String ret ="<div class='tname'>"+tableName+"</div>";
		ret+="<table border=1>";
		ret+="<tr>";
		ResultSetMetaData md = rs.getMetaData() ;
		for( int i = 1; i <= md.getColumnCount(); i++ ) {
			if(md.getColumnLabel(i).equals("UPDATE_DATE") || md.getColumnLabel(i).equals("Update Date")) {
				ret = ret + "<th class='upd'>"+ md.getColumnLabel(i) + "</th>";		
			} else if(md.getColumnLabel(i).equals("ASSIGNED_TO") || md.getColumnLabel(i).equals("Assigned To")) {
				ret = ret + "<th class='upd'>"+ md.getColumnLabel(i) + "</th>";		
			} else if(md.getColumnLabel(i).equals("UPDATED_BY") || md.getColumnLabel(i).equals("Updated By")) {
				ret = ret + "<th class='upd'>"+ md.getColumnLabel(i) + "</th>";		
			} else {
	         	ret = ret + "<th>"+ md.getColumnLabel(i) + "</th>";		
	        }	
        } 
        ret+="</tr>";
		int cnt = 0;
		while( rs.next() )
        {
			cnt++;
			ret+="<tr>";
			for( int i = 1; i <= md.getColumnCount(); i++ )
			{
				if(rs.getString(i) == null) {
					ret = ret + "<td>&nbsp;</td>";		
				} else {
					ret = ret + "<td>"+ rs.getString(i) + "</td>";		
				}
			}
			ret+="</tr>";
         }		
		if(cnt == 0) return "";
        ret+="</table><BR/>";
        return ret;
	}
%>
<%!
	public String execute(Connection conn, String str, String tableType)  throws Exception {
		Statement stmt = null;
		ResultSet rset = null;
		try {
			stmt = conn.createStatement();
			rset = stmt.executeQuery(str);
			return getData(rset, tableType);
		} catch (Exception exp) {
			throw exp;
		} finally {
			try { rset.close(); } catch (Exception ignore) {}
			try { stmt.close(); } catch (Exception ignore) {}
		}
	}
%>
 
<%
String key, val;
String sql = "";
String ret = "";
Connection conn = null;
try {
	
	String typ = "role";//request.getParameter("t");
	if(typ==null) throw new Exception("Type is blank");
	if(!typ.equals("role") && !typ.equals("produit")) {
		throw new Exception("Invalid Type");
	}
	
	InputStream stream = application.getResourceAsStream("/tasksdisplay.properties");
	LinkedProperties props = new LinkedProperties();
	props.load(stream);

	LinkedHashMap<String, String> mapSQL = new LinkedHashMap<String, String>();
	
	String dbname = "mdmtrg01-CMX_ORS";//request.getParameter("d");
	if(dbname == null) {
		dbname = "mdmtrg01-CMX_ORS";
	}
	
	String dbJNDIName = "java:jdbc/siperian-mdmtrg01-cmx_ors-ds";//System.getProperty(dbname);
	if(dbJNDIName == null) {
		throw new Exception("Invalid database name. Must set dbname in JBoss startup");
	}
	
	Hashtable env = new Hashtable(); 
env.put(Context.INITIAL_CONTEXT_FACTORY,
     "com.ibm.websphere.naming.WsnInitialContextFactory");
env.put(Context.PROVIDER_URL, "iiop://mdma302p.corpads.local:30305");
Context initialContext = new InitialContext(env); 
	
	Context aContext = new InitialContext(env);
	DataSource aDataSource = (DataSource)aContext.lookup("java:jdbc/siperian-mdmtrg01-cmx_ors-ds");
	//DataSource aDataSource = (DataSource)aContext.lookup(dbJNDIName);
	conn = (Connection)(aDataSource.getConnection());

	for (Map.Entry<String,String> entry : mapSQL.entrySet()) {
		key = entry.getKey();
		sql = entry.getValue();
		ret = execute(conn, sql, key);
		%>
			<%=ret%> 
		<%
	}
%>

<%
} catch (Exception exp) {
	exp.printStackTrace();
	String err = exp.toString();
	err += "<BR/>sql##E:"+sql;
%>
	Error:<%=err%>
<%	
} finally {
	try { conn.close(); } catch (Exception ignore) {}
}
%>

<style>
.tname {
	color: red;
	font-size: 16px;
	font-weight:bold;
}

table{
	font-family: Verdana;
    font-size: 12px;
    font-weight: normal;
    text-align: center;	
}

table th {
	color: #225F8E;
}

table td {
	background-color: #DBFCF5;
}

a {
	color: blue;
}

.upd {
	background-color: yellow;
}

</style>

</head>
</html>
&#13;
&#13;
&#13; 我收到以下错误: 错误:javax.naming.NoInitialContextException:无法使用散列表中指定的工厂创建InitialContext {java.naming.provider.url = iiop:// localhost:2809,java.naming.factory.initial = com.ibm.websphere.naming。 WsnInitialContextFactory} [根异常是java.lang.NullPointerException]

我正在使用JDK1.7。 添加2 JARS后,我得到以下错误 错误:javax.naming.ConfigurationException:尚未设置java:name空间的名称空间访问器。可能的原因是用户在JNDI Context方法调用中指定了java:URL名称,但未在J2EE客户端或服务器环境中运行。

在端口8080上启动预览服务器

模块:   customTabFromKb_1(/ customTabFromKb_1)   代理(/ proxy)

NMSV0307E:使用了java:URL名称,但未将Naming配置为处理java:URL名称。可能的原因是用户在非J2EE客户端或服务器环境中尝试指定java:URL名称时出错。抛出ConfigurationException。 javax.naming.ConfigurationException:尚未设置java:name空间的名称空间访问器。可能的原因是用户在JNDI Context方法调用中指定了java:URL名称,但未在J2EE客户端或服务器环境中运行。     在com.ibm.ws.naming.java.javaURLContextFactory.isNameSpaceAccessable(javaURLContextFactory.java:109)     at com.ibm.ws.naming.urlbase.UrlContextFactory.getObjectInstance(UrlContextFactory.java:85)     在javax.naming.spi.NamingManager.getURLContext(NamingManager.java:660)     在javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:422)     在javax.naming.InitialContext.lookup(InitialContext.java:436)     在org.apache.jsp.Test_jsp._jspService(Test_jsp.java:162)     在org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:820)     在org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)     在org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)     在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:820)     在org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)     在org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)     在org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)     在org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)     在org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)     在org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)     在org.mortbay.jetty.handler.HandlerList.handle(HandlerList.java:49)     在org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)     在org.mortbay.jetty.Server.handle(Server.java:326)     在org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)     at org.mortbay.jetty.HttpConnection $ RequestHandler.headerComplete(HttpConnection.java:924)     在org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)     在org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)     在org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)     在org.mortbay.jetty.bio.SocketConnector $ Connection.run(SocketConnector.java:228)     在org.mortbay.thread.QueuedThreadPool $ PoolThread.run(QueuedThreadPool.java:582) NMSV0307E:使用了java:URL名称,但未将Naming配置为处理java:URL名称。可能的原因是用户在非J2EE客户端或服务器环境中尝试指定java:URL名称时出错。抛出ConfigurationException。 javax.naming.ConfigurationException:尚未设置java:name空间的名称空间访问器。可能的原因是用户在JNDI Context方法调用中指定了java:URL名称,但未在J2EE客户端或服务器环境中运行。     在com.ibm.ws.naming.java.javaURLContextFactory.isNameSpaceAccessable(javaURLContextFactory.java:109)     at com.ibm.ws.naming.urlbase.UrlContextFactory.getObjectInstance(UrlContextFactory.java:85)     在javax.naming.spi.NamingManager.getURLContext(NamingManager.java:660)     在javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:422)     在javax.naming.InitialContext.lookup(InitialContext.java:436)     在org.apache.jsp.Test_jsp._jspService(Test_jsp.java:162)     在org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:820)     在org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)     在org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)     在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:820)     在org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)     在org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)     在org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)     在org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)     在org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)     在org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)     在org.mortbay.jetty.handler.HandlerList.handle(HandlerList.java:49)     在org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)     在org.mortbay.jetty.Server.handle(Server.java:326)     在org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)     at org.mortbay.jetty.HttpConnection $ RequestHandler.headerComplete(HttpConnection.java:924)     在org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)     在org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)     在org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)     在org.mortbay.jetty.bio.SocketConnector $ Connection.run(SocketConnector.java:228)     在org.mortbay.thread.QueuedThreadPool $ PoolThread.run(QueuedThreadPool.java:582)

请在这方面帮助我!

1 个答案:

答案 0 :(得分:0)

您的代码不可读且无法维护。强烈建议不要在JSP中使用scriptlet(&lt;%..%&gt;)。您应该使用JSTL(Java标准标记库),这是新的约定。在处理业务逻辑之后评估和呈现业务对象。有了这样的说法,你永远不应该在JSP中保留业务逻辑.JSP只服务于客户端的呈现目的。你应该采用MVC(模型视图控制器)设计模式来删除业务逻辑和表示之间的依赖关系。

可以找到更多信息here

但是出现原始问题,我认为包含类(com.ibm.websphere.naming.WsnInitialContextFactory)的jar不在项目的类路径中。您可能需要在类路径中添加客户端jar。