Solved

call java program from a servlet

Posted on 2003-11-14
6
887 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 196
javap not working 8 46
servlet example issue 6 40
Adding multiple JVM environments to RedHat 6 7 23
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

803 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