jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes Calling Servlet with Javascript ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling Servlet with Javascript ?" Watch "Calling Servlet with Javascript ?" New topic
Author

Calling Servlet with Javascript ?

jhon masco
Ranch Hand

Joined: May 13, 2010
Posts: 93
hi friends
I would want to know if call servlet from javascript is good practice or no?
I always call servlet from other servlet (mvc) but now in my job i must modify an application , tshi application call servlet from javascript!
that not look good!
Can someone give me an opinion?

Thanks
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

There are many fine applications that use Ajax/Javascript to make calls to a Servlet. What in particular do find unappealing in the way your application is doing it? Can you post an example?


Junilu - [How to Ask Questions] [How to Answer Questions]
jhon masco
Ranch Hand

Joined: May 13, 2010
Posts: 93
Junilu Lacar wrote:There are many fine applications that use Ajax/Javascript to make calls to a Servlet. What in particular do find unappealing in the way your application is doing it? Can you post an example?


I just was curious. I guess is not good practice for security. Or i am wrong?
Here a line example:

where 'ProgramLauncher4' is a servlet.

Thanks for the reply.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18563
    
    8

jhon masco wrote:I just was curious. I guess is not good practice for security. Or i am wrong?


I don't know. What do you consider insecure about that code?

(There's plenty of places in web applications where you can do insecure things; I'm just asking which of the long list of insecure things applies in this case.)
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16054
    
  21

Actually, you cannot call a servlet from another servlet. You can only forward, unless you just break into the servlet's processing methods by main brute force.

You definitely don't "call" a servlet from JavaScript. Since JavaScript runs on the client and the servlet runs on the server, the client can only make an HTTP(S) request that invokes a servlet's processing method.

The biggest security risks in a J2EE application don't come so much from whether requests were made directly from a browser or via AJAX, but from whether or not the webapp was designed secure to begin with. Which is the main reason I make so much noise about not writing your own security system if the J2EE-standard one can be used instead.


Customer surveys are for companies who didn't pay proper attention to begin with.
jhon masco
Ranch Hand

Joined: May 13, 2010
Posts: 93
Thanks for all the replies.
Then i would conclude that is not bad use javascript or ajax for call servlet (redirect).
Yes Tim, you are right when you say than the most important is the web application design.
Particularly i dont like when i can see the code of any file (.js.jsp.php, etc) from of my web browser.!
That is this case. I can access to the javascript files from the browser. That is a design problem definitively.!

Thanks again.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

jhon masco wrote:I can access to the javascript files from the browser. That is a design problem definitively.!

Again you are making assertions without any basis whatsoever in fact. Why is this a "design problem"?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Shivom Shukla
Ranch Hand

Joined: Jul 05, 2012
Posts: 77

I would want to know if call servlet from javascript is good practice or no?


Yes it is a good practice because by using javascript you can enrich the user experience... rather than using flash content(which can be annoying at times)...
Moreover you cannot call a servlet without using some kind of inter-laying code, example : javascript or ajax ....
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

Shivom Shukla wrote:
Moreover you cannot call a servlet without using some kind of inter-laying code, example : javascript or ajax ....

That is not correct. A servlet can be the target of a form or a link.
Pandian Raman
Greenhorn

Joined: Jun 21, 2011
Posts: 12
Jhon,

I dont know why you are feeling this is not good pratice, but for your information, GMAIL is popular because most of the functionalities are implemented by using AJAX(calling servlet using java script).

Thanks & Regards

Pandian R
jhon masco
Ranch Hand

Joined: May 13, 2010
Posts: 93
Sorry fro delay.
Thanks all for the answers. You win!!!
From now I will try to use other ways(javascript, ajax, etc) to call servlets.

Thank you.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calling Servlet with Javascript ?