Solved

Calling java servlet

Posted on 2001-08-15
8
252 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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 100 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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.

759 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

20 Experts available now in Live!

Get 1:1 Help Now