?
Solved

Calling java servlet

Posted on 2001-08-15
8
Medium Priority
?
270 Views
Last Modified: 2013-11-24
Hi,

   Could you please tell me  is there any way to call java servlet from a non-browser (without http protocol) client ??

I mean without using web browser,is there anyway to call java servlet ?

Baskar
0
Comment
Question by:abas
8 Comments
 
LVL 15

Expert Comment

by:a.marsh
ID: 6387814
Yes - servletrunner - it comes with the JSDK - you can run it from your local machine.

Check out:

http://www.devdaily.com/java/edu/pj/pj010025/

http://rts.ncst.ernet.in/resources/java/tutorial/servlets/servletrunner/

:o)

Ant
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6387817
Sorry - I misread your question - I thought you were referring to a server not browser. :oP

It is possible to "run" a servlet from the command line - you just use the main method as you would a normal java program, and then do whatever you need to do to test the servlet.

Ant
0
 
LVL 6

Expert Comment

by:kotan
ID: 6388168
You can add this to your main program to test servlet.

URL url = new URL("http://servlet");
URLConnection urlConn = url.openConnection();

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:biged040597
ID: 6388972
you can telnet to port 80 ,
assuming your webserver is running on myhost.mydomain.com:80
and your servlet is /servlet/HelloWorld

then try

telnet myhost.mydomain.com 80
GET /servlet/HelloWorld HTTP/1.1
[ENTER]
[ENTER]

0
 
LVL 3

Expert Comment

by:dnoelpp
ID: 6391638
As for telnet, don't get confused when you type you don't see anything, because the webserver doesn't do character echo. Just type blindly the command "GET /servlet/HelloWorld HTTP/1.1" and hit enter
twice. Then telnet shows you the response in raw data.
0
 

Accepted Solution

by:
harshita earned 200 total points
ID: 6420262
Hi please llook at my code. It tells you how to call servlet without using browser.

import java.net.*;
import java.util.*;
import java.io.*;

public class Hello {
    public static void main(String args[]) {

        ObjectInputStream is;
        URL url;
        String  uri =
          "http://3.131.202.109/servlet/HelloServlet";
        HashMap hash = new HashMap();    
        try {
            //calling the servlet by passing params
            url = new URL("http://3.131.202.109/provence/servlet/HelloServlet?name=MyName&age=25");
           
            // open input stream and read the hashmap
            // returned by the servlet
           
            is = new ObjectInputStream(url.openStream());
            hash = (HashMap) is.readObject();
           
            // print it out
            System.out.println(hash);
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }
}

Hope this answers your question.


Thanks & Regards,
Murthy
A.Murthy@gecapital.com
0
 
LVL 3

Expert Comment

by:dnoelpp
ID: 6421045
From my book "core Servlets and JavaServer Pages" the author presented a web client to test servlets and JSPs without using a browser.

Just go to the link and look for "WebClient.java". This class needs some more helper classes.

WebClient opens a window, lets you submit a request with all headers and shows you the response with all headers. Very impressive!

http://archive.coreservlets.com/Chapter2.html

There is the contrary example, too: The EchoServer. This is a small utility to test what gets sended to a server when you submit a form or the like.

Cheers!
0
 
LVL 3

Expert Comment

by:dnoelpp
ID: 6512054
Why did you give harshita a low grade C? This is not fair.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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:
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.
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

850 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