Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Calling a servlet from java program...

Posted on 2002-03-27
5
Medium Priority
?
799 Views
Last Modified: 2013-11-24
hi,
I'm using JRUN 3.0 as my app. server, I have class called DBManager which will handle transaction to database server. My servlet can work correctly with this class but when i call this class from my java program, it'won't work...

The error occured in my contructor which is look like below.

public DBManager(String disDsn) throws Exception {
  dataSourceName = disDsn;
  initialcontext = new InitialContext();
  disDataSource = (DataSource)initialcontext.lookup       ("java:comp/env/jdbc/" + disDsn);
}

when i init this class from my java program, i got this error message :
<<Need to specify class name in environment or system property, or as an applet parameter, or in an appication resource file:  java.naming.factory.initial>>

please guide.. FYI, i'm new in java...:-)

thanks for your time..
0
Comment
Question by:pleasure
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 1

Expert Comment

by:pepenieto
ID: 6898664
When you create a new InitialContext from outside your application server, you must pass as parameter a hashtable with the parameters:
  - Context.PROVIDER_URL (connection url to your app server)
  - Context.SECURITY_PRINCIPAL (login to your app server)
  - Context.SECURITY_CREDENTIALS (password to your app server)
  - Context.INITIAL_CONTEXT_FACTORY (the class of your app server that implements the context factory)
0
 

Author Comment

by:pleasure
ID: 6898705
hi pepenieto,
thanks for your response, as i said before..i'm new in java..can u explain more about your solution...:) or is there any link for me so that i can study about it...

thanks again..
0
 
LVL 1

Accepted Solution

by:
pepenieto earned 150 total points
ID: 6898732
Hi pleasure,

When you access to JNDI, you need a InitialContext (like root) to look for something. When you create a new InitialContext under your application server (jrun?), you don´t need to pass any parameter.

But think that you want to look for something in your jndi tree from another machine (like web server or applet, f.e). You must specify where is the application server machine (PROVIDER_URL), what are your credentials (SECURITY_PRINCIPAL & SECURITY_CREDENTIALS) and what´s the class for creating your initial context (INITIAL_CONTEXT_FACTORY).

This way, you must include in your classpath (on in your jar file if is an applet) the context factory class. You can find it in the .jar of jrun (I suppose).

You can find documentation for jrun in the next url:

http://www.macromedia.com/v1/documents/jr31/migration.pdf

Go to the "Migration EJB clients" section.

There is an example:

Properties properties = new Properties();
// Define remote properties in initial context
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"allaire.ejipt.ContextFactory");
properties.setProperty(Context.PROVIDER_URL,
"ejipt://remotehost:2773");
String user_name = "client";
String password = "client";
// Define username and password (hard-wired in this example)
properties.setProperty(Context.SECURITY_PRINCIPAL, user_name);
properties.setProperty(Context.SECURITY_CREDENTIALS, password);
// Debugging messages
Context ctx = new InitialContext(properties);

Good luck!
0
 

Author Comment

by:pleasure
ID: 6901047
hi pepenieto,
i'm trying out your solution.. let you know the outcome later... thanks anyway...
0
 

Author Comment

by:pleasure
ID: 6901343
hi,
i try another approach to call servlet..
i found this code from other question posted here...
The code is as follow..
/*****************************/
ObjectInputStream is;
URL url;
HashMap hash = new HashMap();    
try {
 url = new URL("http://myserv/myApp/servlet/HelloServlet");
 URLConnection urlConn = url.openConnection();
 is = new ObjectInputStream(url.openStream());
 hash = (HashMap) is.readObject();
 System.out.println(hash);
} catch (Exception e) {
 e.printStackTrace(System.err);
}
/*****************************/

but when i run this java program, i got error
<<
java.io.StreamCorruptedException: InputStream does not contain a serialized object
        at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
        at java.io.ObjectInputStream.<init>(Unknown Source)
        at SDataExtraction.main(SDataExtraction.java:59)
>>

can anyone tell me why?? thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question