Applet not invoking Servlet - Using URLConnection, nothing happens?!?!?!
10 Duke Dollars for this one:
The Applet is invoked, and works fine. The applet takes a picture from my webcam, converts the data to an int[]. I then want to send the data to the OutputStream to the Servlet. However, I can't even get the Servlet to call service() or doPost() or anything. I have no idea why the Servlet isn't called. If I call the Servlet from the web browser it works, so it's there, believe me.
Here is the code:
// Assume we have int data[] and is valid
URL url =new URL("http://localhost:8080/License/SnapshotServlet");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
conn.setRequestProperty("Content-Type","application/binary");
DPSObjectOutputStream oos =new DPSObjectOutputStream(conn.getOutputStream());
oos.writeObject(data);
oos.flush();
oos.close();
The server error is above in my first posting. Here is the error from the Java console:
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2150)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2619)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:726)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:251)
at dps.license.webcam.SnapshotApplet.sendImageToServlet(SnapshotApplet.java:194)
at dps.license.webcam.SnapshotApplet.actionPerformed(SnapshotApplet.java:148)
at java.awt.Button.processActionEvent(Button.java:381)
at java.awt.Button.processEvent(Button.java:350)
at java.awt.Component.dispatchEventImpl(Component.java:3526)
at java.awt.Component.dispatchEvent(Component.java:3367)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:445)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:144)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:130)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:98)
Done!
URL url = new URL(getCodeBase(), "/License/SnapshotServlet");
System.out.println("Trying to connect to URL - " + url);
URLConnection con = url.openConnection();
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type", "application/x-java-serialized-object");
OutputStream os = con.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(icon);
InputStream is = con.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ObjectInputStream ois = new ObjectInputStream(bis);
String respStr = (String) ois.readObject();
System.out.println("The response is: " + respStr);
oos.flush();
oos.close();
ois.close();
hi
i have the same problem u faced .iam not able to write to servlet from applet.
that servlet is not all invoked .tell me your solution please.
client code:
import java.io.*;
import java.applet.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AppletWrite
{
JFrame frame ;
TextArea text;
Button but;
URL url;
URLConnection con;
public AppletWrite()
{
String servlet = "http://localhost:8080/examples/servlet/ReadAppToser" + "?"
+ URLEncoder.encode("filename") + "="
+ URLEncoder.encode("DisplayImage.java");
text =new TextArea();
but=new Button("Submit");
but.addActionListener(new lis());
frame = new JFrame("Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// add the applet to the frame
Container content= frame.getContentPane();
content.setLayout(new BorderLayout());
content.add(text,BorderLayout.CENTER);
content.add(but,BorderLayout.SOUTH);
int height = 300;
int width = 450;
// pack the frame to get correct insets
frame.pack();
Insets fI = frame.getInsets();
frame.setSize(width + fI.right + fI.left, height + fI.top + fI.bottom);
// center the frame on screen
Dimension sD = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((sD.width - width)/2, (sD.height - height)/2);
// make the frame visible
frame.setVisible(true);
try
{
url=new URL(servlet);
con=url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
// con.setRequestProperty("Content-Type", "application/octet-stream");
}
catch(Exception e)
{
System.out.print("error"+e.toString());
e.printStackTrace();
}
}
class lis implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
try{
File file=new File("e:/swing/text1.txt");
BufferedInputStream inp=new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream buf=new BufferedOutputStream(con.getOutputStream());
int i;
System.out.println("reading and writing");
while((i=inp.read())!=-1)
{
char ch=(char)i;
text.append(String.valueOf(ch));
buf.write(i);
}
System.out.println("finish writing");
}
catch(NotSerializableException se){text.append("error");}
catch(Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
text.append("\nerror in reading\n");
text.append(e.toString());}
}
}
public static void main(String args[])
{
AppletWrite apw=new AppletWrite();
}
}
servlet code:
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import com.sun.corba.se.internal.core.Response;
import java.net.*;
public class ReadAppToser extends HttpServlet //implements Serializable
{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
{
BufferedInputStream inTest = null;
System.out.print("Reading\n");
try{
String filename=request.getParameter("filename");
System.out.println(filename);
File f=new File("e:/swing/write.txt");
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(f));
inTest=new BufferedInputStream(request.getInputStream());
int c;
System.out.print("Reading\n");
while((c=inTest.read())!=-1)
{
out.write(c);
System.out.print((char)c);
}
System.out.print("\nfinish writing");
/*ObjectOutputStream outToApplet;
outToApplet=new ObjectOutputStream(response.getOutputStream());
outToApplet.writeObject(new FileInputStream(f) );
outToApplet.flush();
outToApplet.close();*/
}
// catch(NotSerializableException se){System.out.println("error in reading");}
catch(Exception e)
{System.out.println("error in servlet");
e.printStackTrace();
}
}
}
thanks