Results 1 to 4 of 4
  1. #1
    akbjavauser is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default How to Call servlet through Java client (from main)

    Can some please provide the details:
    How to invoke Java servlet through Java Client program.
    I have doPost(req,res) in servlet.
    Any example...

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    Using HttpClient?
    Just a possibility.

  3. #3
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    Can you describe your real problem ?

    What is app about and what server should do and what client should do?

    Have you written that servlet?

    Do you know how to use servlet container?

  4. #4
    akbjavauser is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default

    Got it.I have a servlet which runs under Tomact.
    What I am looking is write a standalone program to call the servlet.

    Looks like I got the solution and is working...

    public static void main(String[] args) {

    try {
    System.out.println("Connecting to Servlet");

    URL url = new URL("http://localhost/servlet/YourservletServlet");

    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    BufferedWriter out =
    new BufferedWriter( new OutputStreamWriter( conn.getOutputStream() ) );
    out.write("username=name\r\n");
    out.flush();
    out.close();
    BufferedReader in =
    new BufferedReader( new InputStreamReader( conn.getInputStream() ) );

    String response;
    while ( (response = in.readLine()) != null ) {
    System.out.println( response );
    }
    in.close();
    }
    catch ( MalformedURLException ex ) {
    System.out.println("########"+ex);
    ex.printStackTrace();
    }
    catch ( IOException ex ) {
    System.out.println("@@@@@@@@@@@@@@"+ex);
    ex.printStackTrace();
    }catch(Exception e){
    System.out.println("$$$$$$$$$$$$$$"+e);
    e.printStackTrace();
    }
    }

Similar Threads

  1. how to call servlet from the very first jsp page
    By R O C K Y in forum Advanced Java
    Replies: 6
    Last Post: 07-04-2012, 04:20 PM
  2. Rewrite as a function so it can call from main.
    By thangli in forum New To Java
    Replies: 2
    Last Post: 11-30-2008, 06:26 AM
  3. using a link to call servlet
    By sijokunnappilly in forum Java Servlet
    Replies: 3
    Last Post: 11-12-2008, 02:22 PM
  4. Replies: 1
    Last Post: 08-07-2007, 06:16 AM
  5. How to call JSP inside Servlet
    By Swamipsn in forum Advanced Java
    Replies: 0
    Last Post: 07-31-2007, 12:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •