This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes can i use javascript  inside  the servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "can i use javascript  inside  the servlet" Watch "can i use javascript  inside  the servlet" New topic
Author

can i use javascript inside the servlet

ashwini hikulkarni
Greenhorn

Joined: Jun 16, 2008
Posts: 25
Hi all
Can i use javascript inside the servlet
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

As per my knowledge , you cant use JS inside servlet , because the servlets are server side java code, which run by servlet container and javaScript are script which executed by client side browser !!


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
ashwini hikulkarni
Greenhorn

Joined: Jun 16, 2008
Posts: 25
yes we can use java script inside the servlets but it will be very complicated

so what i think is to create an anathor file named as abc.js and then call the abc.js inside the servlet
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Originally posted by ashwini hikulkarni:
yes we can use java script inside the servlets but it will be very complicated


Actully the question you asked first was wrong (or may be the way you put it was wrong )! We cant use javascript inside the servlet code , but we can rather embed a javascript code using <script> tag into the response output stream .

Originally posted by ashwini hikulkarni:


so what i think is to create an anathor file named as abc.js and then call the abc.js inside the servlet


not clear !
Antonio Fornie
Ranch Hand

Joined: Aug 07, 2008
Posts: 117
In a Servlet as in a JSP you can place whatever you'd place (as a String to be sent in the response) in the final HTML page that is going to be formed from the Servlet or JSP. Of course for the servlet it will be only a String. In this String you can place HTML, Javascript or whatever you want and it will work in the navigator, but at Java level it's only a String.

Another different thing would be to use Java DOM to process an XML, or a HTML structure. Was it what you meant?

Hope I helped you.


I feel there must be far better ways to do what I do... that makes learning even funnier
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2242
    
    7

To my understanding, it is possible to put js inside servlet given they embed with <html> tags.

Another thing is there is the package javax.script in JDK6, which should allow you to work with js as if it is java code. (embedding the js inside some javax.script class's method)

Yet I'm sure the first approach is much much direct and easier to accomplish.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

Originally posted by K. Tsang:
Yet I'm sure the first approach is much much direct and easier to accomplish.
They aren't even remotely the same thing.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can i use javascript inside the servlet
 
Similar Threads
How to assign a javascript variable to java varisble?
Please Help! How to create Modal Dialog Box in Servlet.
using javascripts in servlet?
Encode java, decode javascript
calling a servlet from javascript function??