Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7022031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:31:39+00:00 2026-05-27T23:31:39+00:00

When I write code in <% , I can use javax.naming , but when

  • 0

When I write code in <%, I can use javax.naming, but when I write code in <%!, I get an error that javax.naming does not exist.

some examples:

<% Javax.naming.InitialContext ic = new Javax.naming.InitialContext(); %> fine
<%! void foo() { Javax.naming.InitialContext ic2 = new Javax.naming.InitialContext(); } %> error

The exact error:

rptCountsRecon_jsp.java:22: package Javax.naming does not exist
    Javax.naming.Context init = new Javax.naming.InitialContext();

I am sortof new to Java and am very new to JSP.

EDIT: added the foo method to the second example, because it may help understand my issue. Also added the exact error from Tomcat.
EDIT 2: tried adding the prefix to Context, and got a similar error.

EDIT 3: Side question… The current code gives the following error:

    try
    {
        javax.naming.Context init = new javax.naming.InitialContext();
        Context ctx = (Context) init.lookup("java:comp/env");
        String jndiName = getServletContext().getInitParameter("jndiName");
        DataSource ds = (DataSource) ctx.lookup(jndiName);
        Connection dbConn = ds.getConnection();
        PreparedStatement pstmnt = dbConn.prepareStatement(sql);
        for(int i=0; i!=binds.size(); ++i)
            pstmnt.setString(i+1, binds.elementAt(i)); //binds index starts at 1 -_-

        return pstmnt.executeQuery();
    } catch (Exception e) {
        //out.println("A naming exception occured... I don't know how to get the info of it.");
        return null;
    }

rptCountsRecon_jsp.java:28: unreported exception java.sql.SQLException; must be caught or declared to be thrown
        Connection dbConn = ds.getConnection();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:31:39+00:00Added an answer on May 27, 2026 at 11:31 pm

    First of all this type code should be in a servlet, not in a JSP.

    Declaration (<%!):

    <%! 
        javax.naming.InitialContext ic2 = new javax.naming.InitialContext();
    %>
    

    ic2 is a member variable. And the reason that you can’t do ic2 = new javax.naming.InitialContext(); there is because the constructor throws javax.naming.NamingException.

    Imagine something as follows:

    public class JspServlet {
        //there is no way to handle that exception here
        javax.naming.InitialContext ic2 = new javax.naming.InitialContext();
        //...
    

    Scriptlet (<%):

    <% 
        javax.naming.InitialContext ic = new javax.naming.InitialContext(); 
    %>
    

    And the reason that it can be done there (within scriplet) is because everything in the scriplet goes inside a try block of the generated JSP service method.

    Imagine something as follows:

    public class JspServlet {
    
        public void _jspService(HttpServletRequest request, HttpServletResponse response)
                 throws ServletException, java.io.IOException {             
            try {
                javax.naming.InitialContext ic = new javax.naming.InitialContext(); 
            } catch (Exception e) { 
                //...
            }
    

    I think it will make more sense if you read more about JSP lifecycle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an easy way to write C code that can access its Git
Can anyone tell me how to write javascript code that removes everything after the
Today, I found out that you can write such code in C++ and compile
I've heard that the advantage of java is that people can write code, compile
If I am sat at work I can easily write some java code -
I need a little question.How can I repair that code so I can use
I have started to write app that can run on Google App Engine. But
When I write code in Python with exception handling I can write code like:
If I'm using a Hashtable , I can write code like this: object item
Using the C# compilers query comprehension features, you can write code like: var names

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.