Get your CodeRanch badge!*
The moose likes Servlets and the fly likes Google chrome, glassfish local server and servlet being called twice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Google chrome, glassfish local server and servlet being called twice" Watch "Google chrome, glassfish local server and servlet being called twice" New topic
Author

Google chrome, glassfish local server and servlet being called twice

Paul.J Wong
Greenhorn

Joined: Apr 25, 2013
Posts: 7

Hi everyone,

I have been studying for JPA and struggled with a bug which made me very confused and later I have found out that it was Chrome caused the program run incorrectly.

I recently switched from Firefox to Chrome. And when I use glassfish admin console in Chrome, whenever after I deployed the app and try to open http://<mymachinename>:8080/myapp/runservlet, I found out the servlet (I don't include code here as it is not specific but generally every servlet dealing with JPA and javadb) will be called TWICE.

Normally if behave correctly, after persisted a entity and auto generate ID, I should get one entity in the database. However, I always get another duplicate in the database. For example, I have a employee named John Smith and I should have a row in Employee table with auto-gen'ed ID 1 and John Smith, but instead somehow, I got ID 1/John Smith and ID 2/John Smith.

At first I thought it was a bug in my code and I tried for a long time to find out but no success. Then I tried same code in IE, the same code behaves correctly no matter how many times I try!

And I tried to run different code and I found it is a general problem not the specific program which I found the problem first with.

Then I found out more:
If I open the app in Chrome at the following address the problem will be gone:
-https://<mymachinename>:8181/myapp/runservlet
-http://localhost:8080/myapp/runservlet
-https://localhost:8181/myapp/runservlet

Do anyone know why app running by Chrome is behaved this way and how to fix it?

Thanks in advance.

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Check in your server.xml if the port 8080 redirects to 8081. Looks like there is some ambiguity in resolution of the ports.


~ Mansukh
Paul.J Wong
Greenhorn

Joined: Apr 25, 2013
Posts: 7
Thanks for the reply.

I checked Glassfish settings and couldn't find any port redirect configs. And should it be a Chrome specific problem rather then Glassfish since I can not reproduce the problem in IE or Firefox?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

I would need to sit down myself and check this Paul. I can't say from the other side of the globe. Yes, different browers do behave differently for different scripting languages. I am not sure though that the problem that you describe is related to the browser. It could be or there could be a different reason to it. These type of issues need a lot of hit and trial, careful observation and experience to home in on. For now , I suggest you continue with FireFox or IE.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9324
    
109

Google Chrome has "Developer Tools" which you can use to track the requests being sent to the server. It will also show you the HTTP response code and such. On the server side I think you can enable "access logs" - I don't know how it's done in GlassFish. Those access logs should ideally show you the requests to the server and the response codes. Either or both of these should probably give you an hint on what's going on. In fact, you could even compare the server side access logs with IE and Chrome and see if there's any difference.

[My Blog] [JavaRanch Journal]
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Yes Jai. There would certainly be a plugin or a built in tool to monitor requests made. That would be of great help. Just check this portal Paul. It just might help. Install this and monitor the requests like Jai suggested.
Paul.J Wong
Greenhorn

Joined: Apr 25, 2013
Posts: 7
Thank you.

I will continue to try the suggested.

But there is a little progress: I found the request header from Chrome and IE are different by one line, in Chrome there is "Cache-Control: max-age=0" but not in IE. And if I config setting to disable cache in Chrome developer tools, everything works fine.

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

That explains it Paul. Cache-Control max-age setting instructs the client about whether it should re-validate every request again or pick it up from the cache as per the max age setting. Read this.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2712
    
    5

Paul.J Wong wrote:I found the request header from Chrome and IE are different by one line, in Chrome there is "Cache-Control: max-age=0" but not in IE. And if I config setting to disable cache in Chrome developer tools, everything works fine.

From your question, the servlet is hit twice per request. I don't understand how cache-control affects the first request. If Chrome is making one request to render the page and another to cache it, then that would be a bug.


SCJP, SCWCD.
|Asking Good Questions|
Paul.J Wong
Greenhorn

Joined: Apr 25, 2013
Posts: 7
Let me post codes, they are fairly simple. Two entities, one stateless bean and one servlet. In Chrome only, from line 14 and 15 of the servlet everything works, but when hit line 17 to 20, there will be 8 PrintJobs records in the database so basically every line of 17 to 20 caused a printjob entities persisted twice.







Paul.J Wong
Greenhorn

Joined: Apr 25, 2013
Posts: 7
Mansukhdeep Thind wrote:That explains it Paul. Cache-Control max-age setting instructs the client about whether it should re-validate every request again or pick it up from the cache as per the max age setting. Read this.


I have Chrome in all its default settings, and I disabled all extensions.

But I found that from line 14 and 15 of the servlet above everything works but 17 to 20 cause entities to persisted twice, those two portion of codes belong to same doGet method.

So strange.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Google chrome, glassfish local server and servlet being called twice
 
Similar Threads
Is there any danger to exposing an RMI port/server to the internet?
How to configure tomcat - 8080 for HTTP & 8181 for HTTPS
Help to find the error.
JPA - getSingleResult Error
Tomcat 404 error