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();

[1279 byte] By [wbrackena] at [2007-9-19]
# 1
1. Check your java console for any exception2. If u r using applet viewer still check for exception.Use applet.getCodeBase() for ur URL constructor.
JayaseelanLa at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hi,

There is nothing wrong with your code.

Instead of directly creating URL object from the string, try like this

// modify the line one with the following code and check out

URLcodebase= getCodeBase(); // instead of hardcoding, get the codebase

String servletPath= "/Licence/SnapshotServlet";

URLurl= new URL(codebase, servletPath);

If it doesn't work, come back with the error log or with more inputs.

Thanks,

Sanath Kumar

sanathkpa at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 3

I'm already using the getCodeBase() convention. It doesn't make a difference. Also, once it gives me an error, I cannot run my applet again. IE just freezes every time I try to run it again. The only way I can get my Applet to try again is to REBOOT!!! Also, the Applet won't even run in Netscape, so I can't even go to another browser to test. Netscape tries to load it, but freezes and on the status bar it says "createWindow", and it's just frozen at that point.

I have got to nail this problem down. Is this a bug?!?!

wbrackena at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 4

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!

wbrackena at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 5

Hi,

Would you mind listing the code written in the methods of SnapshotApplet.

sendImageToServlet();

actionPerformed();

dps.license.webcam.SnapshotApplet.sendImageToServlet(SnapshotApplet.java:194)dps.license.webcam.SnapshotApplet.actionPerformed(SnapshotApplet.java:148)

Thanks,

Sanath Kumar

sanathkpa at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 6

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();

wbrackena at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 7
Everything is working fine now. Thanks for the help, although I'm not sure what the origin of my problem was. Oh well, the dukes will be distributed evenly I guess.Thanks guys,Will
wbrackena at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 8
Hi *,I am facing exactly the same problem. Any inputs?TIA,Anup
AnupDarvatkara at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 9

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

saravanan_k83a at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...