Gopinath G
Gopinath G

Reputation: 97

Is possible to call servlet from applet

I am not familiar with java and applets, so any one please let me know the possibilities for the following my questing.

  1. I would like to call the Servlet from applet.. is this possible?

  2. If the 1st one is possible can we store the Servlet output like XML data or string in the applet variable?

  3. If the 2nd one is possible, then can get that that variable value using JavaScript or J Query?

If possible please give me the simple example.

Thanks in advance.

Upvotes: 2

Views: 2475

Answers (3)

Satyendra
Satyendra

Reputation: 1705

Moving in the flow of your question,

  1. You may call the servlet from your applet:

    • Construct the url that will hit your servlet.
    • Use java.net.URLConnection object to hold the connection from your applet
      URLConnection con = urlToServlet.openConnection()
    • 'con.setDoOutput(true)' => Application intends to write data to the URL connection.
    • Use the input and output streams to communicate with the Servlet.
      con.getInputStream() and con.getOutputStream()

    [Note: Don't forget to close all the connections and streams]

  2. Now, use the data you obtained from the InputStream, in what so ever form you want.

  3. Its extreamly simple, use this code:
    In Applet:
    public String getYourString(){ return responseFromServlet;}
    In Javascript:
    var jsResp = document.name_of_your_applet.getYourString();

Hope, you've got your answers!

Upvotes: 0

One : yes you can call the servlet from applet making http calls

step 1 : make a http call to your servlet

step 2 : make your servlet return XML response

step 3 : parse xml response

using this program you can make a call to your servlet

package com.hussain;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class callServlet {

    public static void main(String[] args) 
    {
        String servletResponse = callServlet.sendRequest("http://gdata.youtube.com/feeds/base/videos?max-results=10&start-//index=1&alt=json&orderby=published&author=astrobixweb");
        callServlet.parseFromXMLResponse(servletResponse);
    }
    public static String sendRequest(String url) {
        String result = "";
        try {
            HttpClient client = new DefaultHttpClient();
            HttpParams httpParameters = client.getParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
            HttpConnectionParams.setSoTimeout(httpParameters, 5000);
            HttpConnectionParams.setTcpNoDelay(httpParameters, true);
            HttpGet request = new HttpGet();
            request.setURI(new URI(url));
            HttpResponse response = client.execute(request);
            InputStream ips = response.getEntity().getContent();
            BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));
            StringBuilder sb = new StringBuilder();
            String s;
            while (true) {
                s = buf.readLine();
                if (s == null || s.length() == 0)
                    break;
                sb.append(s);
            }
            buf.close();
            ips.close();
            result = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    public static void parseFromXMLResponse(String respo) 
    {
        // parse your XML response here
    }   
}

Upvotes: 1

mavroprovato
mavroprovato

Reputation: 8352

  1. Yes you can. The servlet exposes a URL, which you can get with the help of the URLConnection class.
  2. Again you can do this, see here on how you can use the URL connection.
  3. You can do that too, create an applet to get the applet field, and look here on how you can invoke the method.

But all these sound awfully complicated. Why don't you tell us what you are trying to achieve, maybe there is a simpler way to do things.

Upvotes: 1

Related Questions