Applet to servlet communication

Discussion in 'Java' started by Rui Pacheco, Oct 22, 2003.

  1. Rui Pacheco

    Rui Pacheco Guest

    Hello everyone

    I am writing an applet that connects to a database through a servlet.
    For that I need to open an URL connection to the servlet and send
    parameters to the servlet. I already do that, but the connection fails
    to reach the servlet.
    Both the servlet and the applet are on the same computer (my pc is the
    test machine). Although the servlet wasn't designed to work with HTML,
    if I do a GET to it through the browser's address bar I can see the
    init() method being evoked in the server logs. When I send the exact
    same string through the applet, nothing happens. No init(), no debug
    messages, no exceptions, nothing.


    Here's my applet code that handles the connection:

    String query_string = "?ano=" + URLEncoder.encode(
    getParameter("ano"), "UTF-8" ) + "&mes=" + URLEncoder.encode(
    getParameter("mes"), "UTF-8" ) + "&dia=" + URLEncoder.encode(
    getParameter("dia"), "UTF-8" ) + "&user=" + URLEncoder.encode(
    getParameter("utilizador"), "UTF-8" );

    //Write the new found data to the servlet
    URL url = new URL( server + query_string );
    URLConnection urlConnection = url.openConnection();

    //inform the connection that we will send output and accept input
    urlConnection.setDoOutput(true);

    //Don't use a cached version of URL connection.
    urlConnection.setUseCaches (false);
    urlConnection.setDefaultUseCaches (false);



    My servlet is a standard servlet with doGet and doPost. Inside the
    doGet I call a processRequest( response, reqiest ). Inside the process
    request I have this:
    System.out.println( request.getParameter("ano") );



    Has anyone seen anything like this before? I any help would be most
    appreciated.
    Thank you very much
    Rui Pacheco
     
    Rui Pacheco, Oct 22, 2003
    #1
    1. Advertising

  2. Something similar to this happened to me a while ago.
    I solved the problem by reading from the input stream of the URL connection.
    This example below is sending parameters via "post". I your case, I'm not
    sure you have to
    setDoOutput to true you are doing a GET.

    Hope this help,

    -Fredeic

    URLconn.setDoOutput(true);
    PrintWriter out = new PrintWriter( URLconn.getOutputStream() );
    out.println(arguments);
    out.close();
    BufferedReader in = new BufferedReader(new
    InputStreamReader(URLconn.getInputStream()));
    StringBuffer inputLine = new StringBuffer();
    String temp;
    while ((temp = in.readLine()) != null) {
    inputLine.append(temp);
    }
    in.close();

    "Rui Pacheco" <> wrote in message
    news:...
    > Hello everyone
    >
    > I am writing an applet that connects to a database through a servlet.
    > For that I need to open an URL connection to the servlet and send
    > parameters to the servlet. I already do that, but the connection fails
    > to reach the servlet.
    > Both the servlet and the applet are on the same computer (my pc is the
    > test machine). Although the servlet wasn't designed to work with HTML,
    > if I do a GET to it through the browser's address bar I can see the
    > init() method being evoked in the server logs. When I send the exact
    > same string through the applet, nothing happens. No init(), no debug
    > messages, no exceptions, nothing.
    >
    >
    > Here's my applet code that handles the connection:
    >
    > String query_string = "?ano=" + URLEncoder.encode(
    > getParameter("ano"), "UTF-8" ) + "&mes=" + URLEncoder.encode(
    > getParameter("mes"), "UTF-8" ) + "&dia=" + URLEncoder.encode(
    > getParameter("dia"), "UTF-8" ) + "&user=" + URLEncoder.encode(
    > getParameter("utilizador"), "UTF-8" );
    >
    > //Write the new found data to the servlet
    > URL url = new URL( server + query_string );
    > URLConnection urlConnection = url.openConnection();
    >
    > //inform the connection that we will send output and accept input
    > urlConnection.setDoOutput(true);
    >
    > //Don't use a cached version of URL connection.
    > urlConnection.setUseCaches (false);
    > urlConnection.setDefaultUseCaches (false);
    >
    >
    >
    > My servlet is a standard servlet with doGet and doPost. Inside the
    > doGet I call a processRequest( response, reqiest ). Inside the process
    > request I have this:
    > System.out.println( request.getParameter("ano") );
    >
    >
    >
    > Has anyone seen anything like this before? I any help would be most
    > appreciated.
    > Thank you very much
    > Rui Pacheco
     
    Frederic Pepin, Oct 22, 2003
    #2
    1. Advertising

  3. Rui Pacheco

    Ike Guest

    I believe in your URL you need the server to be "localhost" //Ike

    "Rui Pacheco" <> wrote in message
    news:...
    > Hello everyone
    >
    > I am writing an applet that connects to a database through a servlet.
    > For that I need to open an URL connection to the servlet and send
    > parameters to the servlet. I already do that, but the connection fails
    > to reach the servlet.
    > Both the servlet and the applet are on the same computer (my pc is the
    > test machine). Although the servlet wasn't designed to work with HTML,
    > if I do a GET to it through the browser's address bar I can see the
    > init() method being evoked in the server logs. When I send the exact
    > same string through the applet, nothing happens. No init(), no debug
    > messages, no exceptions, nothing.
    >
    >
    > Here's my applet code that handles the connection:
    >
    > String query_string = "?ano=" + URLEncoder.encode(
    > getParameter("ano"), "UTF-8" ) + "&mes=" + URLEncoder.encode(
    > getParameter("mes"), "UTF-8" ) + "&dia=" + URLEncoder.encode(
    > getParameter("dia"), "UTF-8" ) + "&user=" + URLEncoder.encode(
    > getParameter("utilizador"), "UTF-8" );
    >
    > //Write the new found data to the servlet
    > URL url = new URL( server + query_string );
    > URLConnection urlConnection = url.openConnection();
    >
    > //inform the connection that we will send output and accept input
    > urlConnection.setDoOutput(true);
    >
    > //Don't use a cached version of URL connection.
    > urlConnection.setUseCaches (false);
    > urlConnection.setDefaultUseCaches (false);
    >
    >
    >
    > My servlet is a standard servlet with doGet and doPost. Inside the
    > doGet I call a processRequest( response, reqiest ). Inside the process
    > request I have this:
    > System.out.println( request.getParameter("ano") );
    >
    >
    >
    > Has anyone seen anything like this before? I any help would be most
    > appreciated.
    > Thank you very much
    > Rui Pacheco
     
    Ike, Oct 23, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mustafa Aydin

    Applet <==> Servlet Communication

    Mustafa Aydin, Nov 18, 2003, in forum: Java
    Replies:
    7
    Views:
    4,268
    Nigel Wade
    Nov 20, 2003
  2. Ng Wee Peng
    Replies:
    1
    Views:
    4,774
    Dave Miller
    Apr 1, 2004
  3. Troy
    Replies:
    4
    Views:
    4,227
    Nigel Wade
    Jul 19, 2004
  4. shahram
    Replies:
    3
    Views:
    792
    shahram
    Sep 20, 2004
  5. MattC
    Replies:
    11
    Views:
    4,613
    HalcyonWild
    Oct 14, 2005
Loading...

Share This Page