Solved

call java program from a servlet

Posted on 2003-11-14
6
883 Views
Last Modified: 2013-11-24
Hi,
   I am trying to learn how to call a java program from a servlet.
I have a file in unix /tmp/names which contains all the name.


I have one html file which ask for a name as input  and then call the servlet.
The servlet basically has the name variable.  How can I call
another java class to check to see if this name is in the file /tmp/names?


thanks in advance.
0
Comment
Question by:aauser
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9752033
Same way you'd call another class from an application. Create an instance and call it's method.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9752066
If you've got read perms on the file, just open it from the servlet, iterate it, and look for the name
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9752085
If you've got read perms on the file, just open it from the servlet, iterate it, and look for the name
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:aauser
ID: 9752213
thanks.
says I have a class call output1:
public class output1 {
     public void output() {
         System.out.println ("The name is ");
}
}



In my servlet, I have line like this:
output1 out = new output1();
out.output();


and when I ran the servlet, I don't see anything on the browser.

0
 
LVL 92

Expert Comment

by:objects
ID: 9752692
stdout does not go to the browser.
You need to write to the output stream of the servlet response for it to be sent to the browser:

PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream()));
out.println("The name is");
0
 
LVL 1

Accepted Solution

by:
vishalbhando earned 125 total points
ID: 9753143
You can send out variable from your servlet / jsp to the Class as :
... class output1{
          public void output(){
                output(System.out);
          }
          public void output(PrintStream out){
                output(new PrintWriter(out));
          }
          public void output(PrintWriter out){
                 if(out==null) throw new IllegalArgumentException("out can't be null");
                 out.println(...);
          }

         p s v main(String args[]){
                output1 op=new output1();
                op.output();
         }
}

in servlet call as
output1 op=new output1();
op.output(response.getOutputStream());
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now