Hi all...........
I am developing small application of an applet which sends string to Servlet and vice versa. but i am getting above exception.
I am herewith posting my code.
Can anyone tell me What could be wrong in my code.
Code For applet1.java
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class applet1 extends Applet implements ActionListener
{
TextField text_send,text_receive;
Button send;
TextArea exception;
Label label1,label2;
public void init(){
setBackground(Color.gray);
label1=new Label("Enter Your Message : ");
add(label1);
text_send = new TextField(20);
add(text_send);
send = new Button("Send");
add(send);
send.addActionListener(this);
label2=new Label("You Have Entered : ");
add(label2);
text_receive=new TextField(20);
add(text_receive);
exception = new TextArea(20,30);
add(exception);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==send)
{
Onsend();
}
}
// ------------------Get connection to the servlet through URLConnection Interface ------------------------------
private URLConnection getServletConnection() throws MalformedURLException, IOException
{
URL getservlet = new URL("URL OF SERVER/Servlet.java");
URLConnection con = getservlet.openConnection();
//........................ Setting Internet caching parameters......................................
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty(
"Content-Type",
"application/x-java-serialized-object");
return con;
}
public void Onsend()
{
try
{
String input = text_send.getText();
URLConnection con=getServletConnection();
OutputStream out=con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
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();
// show result
text_receive.setText(result);
}
catch(Exception ex)
{
ex.printStackTrace();
exception.setText(ex.toString());
}
}
}
Code for Servlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Servlet extends HttpServlet{
public void doPost(HttpServletRequest request,
HttpServletResponse response)throws ServletException,IOException{
PrintWriter out = response.getWriter();
try
{
response.setContentType("application/x-java-serialized-object");
InputStream in=request.getInputStream();
ObjectInputStream inputFromApplet=new ObjectInputStream(in);
String echo = (String) inputFromApplet.readObject();
//out.println(echo);
OutputStream outstr = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstr);
oos.writeObject(echo);
oos.flush();
oos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}