• Post Reply Bookmark Topic Watch Topic
  • New Topic

applet to servlet comm made easy  RSS feed

 
chetan nain
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i have used http tunneling for applet-servlet communication for one of my projects. have included fully functional code for the same here. see the following points:
1. three files-- AppletToServlet.java (the applet), Intermediate.java (the intermediate class whose objects are passed from applet to servlet and vice versa) and ServletToApplet.java (the servlet)
2. i have used JavaWebServer2.0
3. place AppletToServlet.class in the public_html directory
place Intermediate.class in the public_html directory and the servlets directory
place ServletToApplet.class in the servlets directory
4. i have also included the html code (ats.html) to be placed in the public_html directory
here is the code for AppletToServlet.java
/////////////////////////////////////////
import java.io.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.net.*;
/* <applet code="AppletToServlet" width="300" height="300"></applet> */
public class AppletToServlet extends Applet implements ActionListener {
TextField toSend;
TextField toGet;
Label l1;
Label l2;
Button send;
Intermediate s;
String value;
ObjectInputStream is;
ObjectOutputStream os;
public void init(){
toSend=new TextField(10);
add(toSend);
toGet=new TextField(10);
add(toGet);
l1=new Label("value sent");
l2=new Label("value received");
add(l1);
add(l2);
send=new Button("Click to send to servlet");
send.addActionListener(this);
add(send);
validate();
s=new Intermediate();
}
public void actionPerformed(ActionEvent e){
value=toSend.getText();
sendToServlet();
}

public void sendToServlet(){
try
{
URL url=new URL("http://localhost:8080"+"/servlet/ServletToApplet");
URLConnection con=url.openConnection();
s.setFname(value);
writeStuff(con,s);
s=new Intermediate();
Intermediate p=readStuff(con);
if(p!=null){
value=p.getFname();
toGet.setText("value:"+value);
validate();
}
}
catch(Exception e){
System.out.println(e);
}
}
public void writeStuff(URLConnection connection, Intermediate value){
try{
connection.setUseCaches(false);
connection.setRequestProperty("CONTENT_TYPE","application/octet-stream");
connection.setDoInput(true);
connection.setDoOutput(true);
os=new ObjectOutputStream(connection.getOutputStream());
os.writeObject(value);
os.flush();
os.close();
}
catch(Exception y){}
}
public Intermediate readStuff(URLConnection connection){
Intermediate s=null;
try{
is=new ObjectInputStream(connection.getInputStream());
s=(Intermediate)is.readObject();
is.close();
}
catch(Exception e){}
return s;
}
}
//////////////////////////////////
here is the code for ServletToApplet.java

import java.util.*;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class ServletToApplet extends HttpServlet
{
String valueGotFromApplet;
public void init(ServletConfig config) throws ServletException
{
System.out.println("Servlet entered");
super.init();
}


public void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException
{
try
{
System.out.println("service entered");

ObjectInputStream ois=new ObjectInputStream(request.getInputStream());
Intermediate ss=(Intermediate)ois.readObject();

valueGotFromApplet=ss.getFname();
System.out.println(valueGotFromApplet);
response.setContentType("application/octet-stream");
ObjectOutputStream oos=new ObjectOutputStream(response.getOutputStream());
oos.writeObject(ss);
oos.flush();
oos.close();
}
catch(Exception e){System.out.println(e);}
}
public String getServletInfo()
{
return "yo momma";
}
}
////////////////////////////////////
here is the code for Intermediate.java
import java.io.*;
public class Intermediate implements Serializable{
String fname;
public String getFname(){
return fname;
}
public void setFname(String s){
fname=s;
}
}
////////////////////////////////////////
here is the code for ats.html
<html>
<body>
<applet code="AppletToServlet" width="200" height="200"></applet>
</body></html>
////////////////////////////////////////
hope this helps.
regds,
chetan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!