Solved

Calling java servlet

Posted on 2001-08-15
8
253 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cipher Configuration on Apache HTTPD 4 62
object oriented programming comparison 5 69
use lov values 2 49
Is there a simpler dropbox system? 10 18
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

786 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