Solved

call java program from a servlet

Posted on 2003-11-14
6
898 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
[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
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

724 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