Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

jndi in servlets  RSS feed

 
Sekhar Chand
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai friends,
I got one small problem in my program.
I configured jndi name through console and i have commit the changes.
then server.xml was changed, those configuration effected the server.xml some more tags inserted into server.xml automatically.
those tags is like below

<ResourceParams name="UserDatabase">
<parameter>
<name>factory</name>
<value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
</parameter>
<parameter>
<name>pathname</name>
<value>conf/tomcat-users.xml</value>
</parameter>
</ResourceParams>
<ResourceParams name="ratna">
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>password</name>
<value></value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://192.168.1.34:8080/jndi</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
<parameter>
<name>username</name>
<value>admin</value>
</parameter>
</ResourceParams>
</GlobalNamingResources>

So like that i have connected to database through jndi,
My servlet program is like below.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.sql.*;
import java.sql.*;
import javax.naming.*;
public class MyExample extends HttpServlet
{
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
try
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
int k= Integer.parseInt(req.getParameter("size"));
InitialContext in_context = new InitialContext();
Context context = (Context)in_context.lookup("java:/comp/env");
DataSource ds = (DataSource)context.lookup("ratna");
Connection con = ds.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT ID,EMPLOYEE_ID,EMAIL FROM EMPLOYEE LIMIT "+k);

I am getting javax.naming.NameNotException.
I dont know why i am getting that one, i have searched a lot in gopogle but i didnt get the correct solution.

Thanks in advance
ratna
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!