0
votes

I have below 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>
I'm getting the below error: Error:javax.naming.NoInitialContextException: Failed to create InitialContext using factory specified in hashtable {java.naming.provider.url=iiop://localhost:2809, java.naming.factory.initial=com.ibm.websphere.naming.WsnInitialContextFactory} [Root exception is java.lang.NullPointerException]

I'm using JDK1.7. After addiing the 2 JARS I'm getting below error Error:javax.naming.ConfigurationException: Name space accessor for the java: name space has not been set. Possible cause is that the user is specifying a java: URL name in a JNDI Context method call but is not running in a J2EE client or server environment.

Starting preview server on port 8080

Modules: customTabFromKb_1 (/customTabFromKb_1) proxy (/proxy)

NMSV0307E: A java: URL name was used, but Naming was not configured to handle java: URL names. The likely cause is a user in error attempting to specify a java: URL name in a non-J2EE client or server environment. Throwing ConfigurationException. javax.naming.ConfigurationException: Name space accessor for the java: name space has not been set. Possible cause is that the user is specifying a java: URL name in a JNDI Context method call but is not running in a J2EE client or server environment. at com.ibm.ws.naming.java.javaURLContextFactory.isNameSpaceAccessable(javaURLContextFactory.java:109) at com.ibm.ws.naming.urlbase.UrlContextFactory.getObjectInstance(UrlContextFactory.java:85) at javax.naming.spi.NamingManager.getURLContext(NamingManager.java:660) at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:422) at javax.naming.InitialContext.lookup(InitialContext.java:436) at org.apache.jsp.Test_jsp._jspService(Test_jsp.java:162) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97) at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418) at org.mortbay.jetty.handler.HandlerList.handle(HandlerList.java:49) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:326) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:924) at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549) at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:228) at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582) NMSV0307E: A java: URL name was used, but Naming was not configured to handle java: URL names. The likely cause is a user in error attempting to specify a java: URL name in a non-J2EE client or server environment. Throwing ConfigurationException. javax.naming.ConfigurationException: Name space accessor for the java: name space has not been set. Possible cause is that the user is specifying a java: URL name in a JNDI Context method call but is not running in a J2EE client or server environment. at com.ibm.ws.naming.java.javaURLContextFactory.isNameSpaceAccessable(javaURLContextFactory.java:109) at com.ibm.ws.naming.urlbase.UrlContextFactory.getObjectInstance(UrlContextFactory.java:85) at javax.naming.spi.NamingManager.getURLContext(NamingManager.java:660) at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:422) at javax.naming.InitialContext.lookup(InitialContext.java:436) at org.apache.jsp.Test_jsp._jspService(Test_jsp.java:162) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97) at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418) at org.mortbay.jetty.handler.HandlerList.handle(HandlerList.java:49) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:326) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:924) at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549) at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:228) at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

Please help me in this regard!

1
You should avoid java code in JSP. Please refer the link :stackoverflow.com/questions/3177733/…Ankit Tripathi
Thank you. I want to know that, in my java class also, I'm getting the same error while trying to get the connection using JNDI lookup. please help .user3607647
Could you please re-post your code then?Ankit Tripathi
Could you please paste full error stack trace as well?Ankit Tripathi
Hi, After Added those 2 JARS. I'm facing different issue. I have updated the question with full error stack traceuser3607647

1 Answers

0
votes

Your code is unreadable and not maintainable as well.The use of scriptlets(<%..%>) in JSP is highly discouraged.You should instead use JSTL(Java standard tag library) which is the new convention for evaluation and presentation of business objects after processing the business logic.Having said this you should never keep business logic in JSP.JSPs only serve the purpose of presentation for the client.You should probably adopt MVC(Model View Controller) design pattern to remove the dependency between business logic and presentation.

More information can be found here

However coming to your original problem,I think the jar containing class(com.ibm.websphere.naming.WsnInitialContextFactory) is not in classpath of your project. You may need to add client jar in classpath.