aspose file tools*
The moose likes Servlets and the fly likes Some of you are experts this could be your next problem! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Some of you are experts this could be your next problem!" Watch "Some of you are experts this could be your next problem!" New topic
Author

Some of you are experts this could be your next problem!

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

My applet works just fine in IE5.5 but apparently wont update the session in Netscape or MSN. I see nothing wrong with the java code. I need a workaround it seems.
applet code:

servlet code:


SCJP
Visit my download page
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
You need to use the HttpURLConnection class instead of URLConnection - then you can
con.setRequestMethod("POST");
so the request will go to your doPost method.
Bill

------------------
author of:
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

Bill,
Thanks. You must have read my mind. I just tried that but it seemed to make things worse. I just rebooted so I will try it again. This is so frustrating. I get a error message in Netscape saying thread.suspend() was called. I got it before earlier in effort to finish this applet. When I used HttpURLConnection it quit working in IE as well. The java console says cannot open connection

Something is wrong with URL and URLConnection classes.
For openConnection() method of URL class, the API says:

If for the URL's protocol (such as HTTP or JAR), there exists a public,
specialized URLConnection subclass belonging to one of the following packages
or one of their subpackages: java.lang, java.io, java.util, java.net,
the connection returned will be of that subclass.
For example, for HTTP an HttpURLConnection will be returned, and for JAR a JarURLConnection will be returned.

However both URLConnection(which is what it actually returns)
and HttpURLConnection are abstract classes.
How can a method return an object of an abstract class?
Never mind, I have seen that before.
For URLConnection class, the API says:

1. The connection object is created by invoking the openConnection method on a URL.
2. The setup parameters and general request properties are manipulated.
3. The actual connection to the remote object is made, using the connect method.
4. The remote object becomes available. The header fields and the contents of the remote object can be accessed.

However it also says that connect() is an abstract method.
That is odd since I tried calling it and got no error.
and why does my applet work in IE but not in Netscape
if I never call connect()?
I tried using a HttpURLConnection so I could call its setRequestMethod("POST")
But that seems to make things worse!
I am starting to think that applet to servlet communication just doesnt work

[This message has been edited by Randall Twede (edited February 16, 2001).]
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

Here is my attempt at using HttpURLConnection:

It throws a runtime exception
HttpURLConnection has a constructor that takes a URL as an argument, but it is an abstract class.
I cant use GET because I am already using it to initialize total in my applet's start() method. I can write a seperate servlet to handle the session update, but that really rubs me the wrong way.
Perhaps if I write a service() method that calls doPost()?
But I think it would catch the GET request from my start() method as well.

[This message has been edited by Randall Twede (edited February 17, 2001).]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
I have been looking back at some of my old code and I think I told you wrong about specifically using that setRequestMethod - it looks like the kind of URLConnection you get depends on the URL, I find stuff that worked that looks like:
URL url = new URL( data );
URLConnection urlC = url.openConnection();
and I didn't have to set the request method.
Don't worry about the fact that some of these classes are abstract, just like with Graphics, the JVM generates a concrete class that is a descendent of URLConnection.
Anyhow, now I am mystified as to why your code does not work.
If you want to spy on exactly what is being transmitted, go to the site for my JSP/servlets book:
http://www.lanw.com/books/servletjsp/
You can download a GUI based spy utility and the chapter on debugging servlets as a PDF file.
Bill
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

Bill,
I will definately download those tool. As for now I am trying to work around it by having 2 seperate servlets that both use GET. I am doing something wrong though. I get no exception, but session doesnt get updated with either browser. I think it is something simple.
applet code:

servlet code

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

I think I am onto something. The line
HttpSession session = request.getSession(false);
suggests that the session is stored on the browser.
So this
session.putValue("balance", balance);
never happens.

I am not sure how to deal with it though.
And how did it work correctly in IE?
Perhaps the message I got from Netscape:
Thread.suspend() was called has something to do with it(I only looked for this when the Browser seemed to not work so I dont know if it always gave that error).

[This message has been edited by Randall Twede (edited February 17, 2001).]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
The session is stored on the servlet engine side, but the session ID code is store on the browser side as a cookie. It is this code that enables the servlet engine to recover the correct session.
I think the first debugging step I would take would be to put in some System.out statements in the servlet, so you can see for sure how far it is getting.
Exactly how do you test to see if the session is updated?
Incidently, JavaRanch has some well worked out applet-servlet communication utilities in the code barn - try http://javaranch.com/common.jsp
Bill
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

when you quit the applet you enter a "store" where you can spend the money. as soon as you put something in your shopping cart, your balance is displayed. Prior to the applet, people just entered how much they had from a form. So I know the problem is not elsewhere.
Paul clued me about the code barn. It and some similar code I found elsewhere helped me get as far as I did.
Well my latest attempt is worse. I guess I will reload the old ones that works with IE.
And yes some System.out's will tell me more than I know now.

[This message has been edited by Randall Twede (edited February 18, 2001).]
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

I went back to the original code(first post in this thread).
I found another example of applet posting to servlet in "core servlets and JSP". It said:

I added this line:
con.setRequestProperty("Content-Length", "4");
but it did no good.
I added some System.out.println's

In IE I get all five messages. In Netscape I get none of them.
Also Netscape seems to freeze up sometimes while loading the applet, other times it seems ok.

[This message has been edited by Randall Twede (edited February 19, 2001).]
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

Here is the complete applet code:

Netscape does do the GET in start() method but not the POST in quit() method.
[This message has been edited by Randall Twede (edited February 19, 2001).]
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

I tried writing a seperate servlet with a service() method, but it is the same result. It only works in IE.
applet code

servlet code
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
The only thing I can think to try is to give up on the one-way POST in which you write the data and close the connection without waiting for a reply. Maybe the way Netscape closes the connection causes an exception on the servlet end?? (Grasping at straws mode) I know that closing an output stream before reading the URLconnection input stream has caused me trouble in the past.
I would certainly enclose the service method in your example in a try-catch so it can log any exception - that is a must for debugging servlets.
Bill
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

Bill,
Thanks. I read somewhere that I dont really have to close the connection. So I will try that.
I left the output stream in applet code open. same problem. should the applet setDoInput(true);? Should it open an input stream? Perhaps it would be easier to figure out why my attempt at a GET didnt work(see earlier posting in this thread). Although I generally prefer using POST since the user cant bookmark the page using the url containing the parameters. Or maybe if I send a String instead of an int, and use getParameter somehow.
However, I am getting none of the System.out.println()'s so the service() method is not ever getting called.
con.setDoInput(true);
didnt help
I will try catching exceptions in the servlet and see if that reveals anything.

[This message has been edited by Randall Twede (edited February 22, 2001).]
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

Well that was interesting. I tried catching each exception seperately. I got a compile time exception saying that ServletException is never thrown in the body of the try statement
The bottom line is that the servlet is not throwing any exceptions.
Asif Anis
Greenhorn

Joined: Mar 02, 2001
Posts: 19
send me your email adde. and i'll send u another code for ur prob. which i wrote some 4 months ago
my adde
is
gikian@sify.com
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

I am still wanting help. I wrote to Asif, but he hasnt replied.
My first choice is to use doPost() in the same servlet that initializes total through doGet(). But if I have to use doGet() or service() in a seperate servlet I will.
Maybe I will have to send a String and use req.getParameter() intead of sending an int? I still cant see why it works in IE only
Asif Anis
Greenhorn

Joined: Mar 02, 2001
Posts: 19
hi,
i didnt get any of ur mail ..please mail me at pathan190@hotmail.com
waiting for ur mail ...
please write ur exact problem.
asif
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

I cant believe it is this hard to make this work in Netscape!
My latest attempt I tried sending a parameter pair instead of an int, and using req.getParameter() instead of opening a stream, but it still only works in IE.
applet code:

servlet code:
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
One of your post mentions the need to use connect(), and yet in your code I did not see the connect() statement. Or maybe I am missing something.
Have you or have you not used con.connect()? If not why? In the book examples I have seen connect() is always used after getting a URLConnection object and after calling all the "set" methods on the connection object, and before opening the stream. The API clearly says the connect opens a communications link to the resource referenced by this URL, if such a connection has not already been established.
You said you were reluctant to call connect() because it is an abstract method. But there is no problem with calling an abstract method because the JVM ultimately implements the method in a concrete subclass (which we are not bothered with). In fact calling an abstract method is very common idiom in java- An interface or superclass type variable can hold a reference to an object of a concrete class (runtime type) which implements the interface or extends the superclass. By dynamic dispatch, the method of the implementing concrete class will get called. For eg:-
interface Iface { void meth(); } //an abstract method
class IfaceImpl implements Iface {public void meth() {//code}}
Iface x=new IfaceImpl();
x.meth(); //calling an abstract method !!
Maybe IE is not giving problems without connect() because it is a non-standard implementation of JVM. Maybe Netscape is sticking to the standards.
Do try using con.connect() AFTER calling all 'set' method on the connection object and BEFORE opening the stream from the connection. I am very interested and would like to help further.

[This message has been edited by Rahul Rathore (edited March 17, 2001).]
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
I was wrong in thinking that connect() was necessary. Sorry.
Since the last post, I have read the source code of getOutputStream as implemented in the concrete class sun.net.www.protocol.http.HttpURLConnection and found that, the method getOutputStream() is implemented to include a call to connect(). So even if we do not use connect(), the call to con.getOutputStream() will implicitly call connect() also.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

Yeah I thought so. I think I tried using connect() anyway but it did no good. I read somewhere that Netscape requires URLEncoder.encode(). I am not using special characters but maybe thats it. I will try it.
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
Randall
I know this is belated.
But I was intensely curious to know how you solved your problem. Did URLEncoder.encode() solve it?
Victor Boveda
Greenhorn

Joined: May 22, 2001
Posts: 6
Try this. I've found than Netscape doesn't wait for the OutputStream to send data before closing it. So you should force it to wait.
Applet code:

Servlet code:

Hope this helps
toubou
Greenhorn

Joined: Jun 18, 2001
Posts: 1
Did you solved the problem?
I have similar problem now.
Program below doesnt throw any exception when I pull out LAN cable and freeze on Netscape only.
Im waiting someone help.
int GetCount( String path ) {

int rtn = 0;
try {
URL codebase = getCodeBase();
URL url = new URL(codebase, path);
URLConnection uc = url.openConnection();
uc.setDoInput(true);
uc.setDoOutput(false);
uc.setUseCaches(false);
System.out.println( "Pull out LAN cable Now." );
DataInputStream ois = new DataInputStream(uc.getInputStream());
rtn = ois.readInt();
ois.close();
} catch (MalformedURLException em) {
em.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

------------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Some of you are experts this could be your next problem!