Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

APPLET TO SERVLET AND SERVLET TO APPLET

10 views
Skip to first unread message

Mehmet F. Erten

unread,
Mar 6, 2007, 6:38:38 PM3/6/07
to
Through the exampls I found from google search, I succeeded to communicate
in between my applet and my servlet.
(no more exception and/or messages which are related to the java security
issues)
But regardless how I modifed my applet sent data in my servlet, I keep
reading same data from my servlet which is whatever my applet sents to the
servlet.
Thanks for any tip.
private void onSendData() {

try {

URLConnection con = getServletConnection();

// get input data for sending

// send data to the servlet

OutputStream outstream = con.getOutputStream();

ObjectOutputStream oos = new ObjectOutputStream(outstream);

oos.writeObject(jdbTextField1.getText());

oos.flush();

oos.close();

// receive result from servlet

InputStream instr = con.getInputStream();

ObjectInputStream inputFromServlet = new ObjectInputStream(instr);

String result = (String) inputFromServlet.readObject();

System.out.println("result: " + result);

inputFromServlet.close();

instr.close();

// show result

jdbTextField1.setText(result);

}

catch (Exception ex) {

ex.printStackTrace();

exceptionArea.setText(ex.toString());

}

}

// here does not matter what I write, it does not affect above applet. It
reads its own input (again maybe from the servlet)

// I don't know how I can verify that my applet is actually sending and
reading from my servlet through the above code.

public void doPost(

HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

try {

response.setContentType("application/x-java-serialized-object");

// read a String-object from applet

// instead of a String-object, you can transmit any object, which

// is known to the servlet and to the applet

echo = (String) new
ObjectInputStream(request.getInputStream()).readObject();

// echo it to the applet

ObjectOutputStream oos = new
ObjectOutputStream(response.getOutputStream());

oos.writeObject("mehmet" + echo);

oos.flush();

oos.close();

}

catch (Exception e) {

e.printStackTrace();

}

}


Arthur Ore

unread,
Mar 7, 2007, 3:36:09 PM3/7/07
to
"Mehmet F. Erten" <mehmet...@mblsoft.com> wrote in message
news:45edfb6b$1...@newsgroups.borland.com...

> Through the exampls I found from google search, I succeeded to communicate
> in between my applet and my servlet.
> (no more exception and/or messages which are related to the java security
> issues)
> But regardless how I modifed my applet sent data in my servlet, I keep
> reading same data from my servlet which is whatever my applet sents to the
> servlet.
> Thanks for any tip.

Hi Mehmet,

I took some code from a working example I wrote a good while ago and tried
to make it match as closely as possible what you are trying to achieve.

I seem to recall that I found the following article very useful
http://www.j-nine.com/pubs/applet2servlet/Applet2Servlet.html

I hopes this helps

Arth

// APPLET
try
{
URL postServlet = new URL(getCodeBase(),servletURL);
URLConnection servletConnection = postServlet.openConnection();

servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);
servletConnection.setUseCaches(false);
servletConnection.setDefaultUseCaches(false);
servletConnection.setRequestProperty("Content-Type","application/octet-stream");

ObjectOutputStream out = new
ObjectOutputStream(servletConnection.getOutputStream());

out.writeObject( (Object) inputTextField.getText() );
out.flush();
out.close();

ObjectInputStream inputFromServlet = new
ObjectInputStream(servletConnection.getInputStream());

outputTextField.setText( (String) inputFromServlet.readObject() );

inputFromServlet.close();

}
catch (Exception ee)
{
ee.printStackTrace();
}

// SERVLET -------------------------------
public void doPost(HttpServletRequest request, HttpServletResponse
response)
{

try {

ObjectInputStream inputFromApplet = new
ObjectInputStream(request.getInputStream());
String fromApplet = null;
fromApplet = (String) inputFromApplet.readObject();
inputFromApplet.close();

System.out.println("Received from applet " + fromApplet);

ObjectOutputStream outputToApplet;
outputToApplet = new ObjectOutputStream(response.getOutputStream());
outputToApplet.writeObject(fromApplet + " added by servlet");

outputToApplet.flush();
outputToApplet.close();
}
catch (Exception ex) {
ex.printStackTrace();
}

}


Mehmet F. Erten

unread,
Mar 7, 2007, 6:35:29 PM3/7/07
to
(I intented to reply to the group but looks like I sent it to Arthur only).

I got JBuilder code with the excellent GUI with very functional Applet to
Servlet connection thanks to the examples from WEB.
Now I will go and add QueryDataSet to work with Interbase through Servlet
from my Applet.

"Arthur Ore" <arthur_dot_ore@nospamthx_zen_dot_co.uk> wrote in message
news:45ef...@newsgroups.borland.com...

0 new messages