URL and servlets

Hi

I'm almost done with the biggest part of my project, so as soon as i can resolve this issue(i'm sure i'll have more, but this seems pretty big to me anyways, :)) i will be sitting pretty.

So i have an applet. This applet was designed to capture some data from a RS232 device(working perfectly). i have developed this all in its own project with its own jars and sorts(compiled under 1.3 for client requirement issues, hence the low compability).

I then have the rest of my project in which i embedd this applet(using a AJAX HTML object).

The problem is sending the captured data from the applet to the server(servlet).

While trying to come up with a solution to this, i stumbled across the URL and URLConnection class and i have been going through the tutorial offered here:

http://java.sun.com/docs/books/tutorial/networking/urls/urlInfo.html

I'm just having a hard time trying to understand how is it that the servlet can get the info from the applet.

My applet code isn't pretty and i don't even think it is right for the purpose that i want it for.

Basically i want the applet to write to the URL that it risides on and then i want the servlet to retrieve the URL with the variable i attach on the applet side.

Ok so here is my applet code,like i said this is working off the tutorial code....

String gogoTag =""+myTag[1];

try{

url =new URL(""+getCodeBase());

myCon = url.openConnection();

myCon.setDoOutput(true);

OutputStreamWriter out =new OutputStreamWriter(myCon.getOutputStream());

out.write("tag="+gogoTag);

out.close();

BufferedReader in =new BufferedReader(new

InputStreamReader(myCon.getInputStream()));

String decodedString;

while ((decodedString = in.readLine()) !=null){

System.out.println(decodedString);

}

in.close();

}catch (MalformedURLException e1){

e1.printStackTrace();

}catch (IOException e1){

e1.printStackTrace();

}

So when i run my applet the output in the JVM console is this

<html><head>

<meta name='gwt:module' content='net.impro.ajax.reports.admin.admin'>

<meta name='gwt:property' content='tag=18845418652'>

</head><body>

<iframe id='__gwt_historyFrame' style='width:0;height:0;border:0'></iframe>

<script language='javascript' src='gwt.js'></script>

</body></html>

So it does right what i want it to ut i don't think its to the URL, sigh :( lol

What or how do i write the info i want from the textbox of an applet to the servlet? Does the servlet also need to connect via a URLConnection object to get the variable or can i just use this.getThreadLocalRequest();

(My servlet extends RemoteServiceServlet which in turn extends HttpServlet) or do i just have to go do more reading on the URL and URLConnection class?

Any advice is appreciated

Thank you

:)

[3842 byte] By [monk3ya] at [2007-11-27 9:01:23]
# 1
Huh? Just make the applet submit an HTTP POST (you can use Apache's HttpClient). Why would the servlet need to do anything special?
CeciNEstPasUnProgrammeura at 2007-7-12 21:30:55 > top of Java-index,Java Essentials,Java Programming...
# 2
> Huh? Just make the applet submit an HTTP POST (you> can use Apache's HttpClient). You don't need that. HttpURLConnection works fine for post
cotton.ma at 2007-7-12 21:30:55 > top of Java-index,Java Essentials,Java Programming...
# 3
Thanks for the repliesI will continue with the HTTpConnection then:)
monk3ya at 2007-7-12 21:30:55 > top of Java-index,Java Essentials,Java Programming...
# 4

Hi

I have been at this all day and i'm still not able to get any results to the servlet, :(

My applet code now looks like this

url = new URL(getCodeBase(), "quicktag");

myCon = url.openConnection();

myCon.setDoOutput(true);

myCon.setDoInput(true);

myCon.setUseCaches (false);

myCon.setDefaultUseCaches (false);

myCon.setRequestProperty("Content-Type", "application/x-java-serialized-object");

myCon.connect();

OutputStreamWriter out = new

OutputStreamWriter(myCon.getOutputStream());

out.write("tag="+gogoTag);//"gogoTag is a string(Tag Number)"

out.close();

and i want my servlet to check for any new tag numbers that the user might have sent through every second so i have put it in a thread

Servlet code

if (!started) {

new Thread() {

public void run() {

while (true){

System.err.println("Checking...");

HttpServletRequest params = getThreadLocalRequest();

if (params != null) {

System.err.println("IN params");

Enumeration e;

try {

InputStream in = params.getInputStream();

ObjectInputStream inputFromApplet = new ObjectInputStream(in);

String echo = (String) inputFromApplet.readObject();

}

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

} catch (ClassNotFoundException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

try {

this.sleep(1000);

} catch (InterruptedException e1) {

break;

}

}

}

}.start();

started = true;

}

2 things that concern me is

1)in my applet code where i make the URL object

url = new URL(getCodeBase(), "quicktag");

is this right?My servlets name is QuicktagServlet

When i do a url.getPath()

i get this

/net.impro.ajax.reports.admin.admin/quicktag

2) Am i accessing the input stream correctly? and params

is always returning false(apart from params, :)).

I think my problem lies in both the applet and the servlet but i don't know where

Any advice as usual is very welcomed

Thank you

:)

monk3ya at 2007-7-12 21:30:55 > top of Java-index,Java Essentials,Java Programming...
# 5

Stictly speaking, if your data is not organized as a post request string, you should use a PUT method transaction instead. Occasionally gives problems with proxies, though.

ps: You might want to consider compressing your data with a GZIPOutputStream if there's a fair bit of it.

Message was edited by:

malcolmmc

malcolmmca at 2007-7-12 21:30:55 > top of Java-index,Java Essentials,Java Programming...