SOURCE CODE:
ECHOSERVLET:
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class EchoServlet extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
try
{
response.setContentType("application/x-java-serialized-object");
InputStream in=request.getInputStream();
ObjectInputStream inputFromApplet=new ObjectInputStream(in);
String echo=(String)inputFromApplet.readObject();
OutputStream outstr=response.getOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(outstr);
oos.writeObject(echo);
oos.flush();
oos.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
ECHOAPPLET:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class EchoApplet extends Applet {
private TextField inputField=new TextField();
private TextField outputField=new TextField();
private TextArea exceptionArea=new TextArea();
public void init()
{
setLayout(new GridBagLayout());
Label title=new Label("Echo Applet",Label.CENTER);
title.setFont(new Font("SansSerif",Font.BOLD,14));
GridBagConstraints c=new GridBagConstraints();
c.gridwidth=GridBagConstraints.REMAINDER;
c.weightx=1.0;
c.fill=GridBagConstraints.HORIZONTAL;
c.insets=new Insets(5,5,5,5);
add(title,c);
c=new GridBagConstraints();
c.anchor=GridBagConstraints.EAST;
add(new Label("Input:",Label.RIGHT),c);
c=new GridBagConstraints();
c.fill=GridBagConstraints.HORIZONTAL;
c.weightx=1.0;
add(inputField,c);
Button sendButton=new Button("Send");
c=new GridBagConstraints();
c.gridwidth=GridBagConstraints.REMAINDER;
add(sendButton,c);
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onSendData();
}
});
c=new GridBagConstraints();
c.anchor=GridBagConstraints.EAST;
add(new Label("Output:",Label.RIGHT),c);
c=new GridBagConstraints();
c.gridwidth=GridBagConstraints.REMAINDER;
c.fill=GridBagConstraints.HORIZONTAL;
c.weightx=1.0;
add(outputField,c);
outputField.setEditable(false);
c=new GridBagConstraints();
c.anchor=GridBagConstraints.EAST;
add(new Label("Exception:",Label.RIGHT),c);
c=new GridBagConstraints();
c.gridwidth=GridBagConstraints.REMAINDER;
c.weighty=1.0;
c.fill=GridBagConstraints.BOTH;
add(exceptionArea,c);
exceptionArea.setEditable(false);
}
private URLConnection getServletConnection() throws MalformedURLException,IOException
{
URL urlServlet=new URL("http://localhost:8080/servlet/EchoServlet");
URLConnection con=urlServlet.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Content-Type","application/x-java-serialized-object");
return con;
}
private void onSendData()
{
try
{
String input=inputField.getText();
URLConnection con=getServletConnection();
OutputStream outstream=con.getOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(outstream);
oos.writeObject(input);
oos.flush();
oos.close();
InputStream instr=con.getInputStream();
ObjectInputStream inputFromServlet=new ObjectInputStream(instr);
String result=(String)inputFromServlet.readObject();
inputFromServlet.close();
instr.close();
outputField.setText(result);
}
catch(Exception ex)
{
ex.printStackTrace();
exceptionArea.setText(ex.toString());
}
}
}
<html><applet code="EchoApplet.java" height=600 width=600>
</applet></html>
No comments:
Post a Comment