Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Servlets and the fly likes JSP & Javascript? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "JSP & Javascript?" Watch "JSP & Javascript?" New topic

JSP & Javascript?

PVS Praveen

Joined: Dec 15, 2000
Posts: 1
Is it possible to access JSP Code within a javascript function.?
If yes,can anyone enlighten me please...?
Vikas Lamba

Joined: Mar 20, 2001
Posts: 4
Don't know a clean way but the following workaround might help.
In the JSP page, write the code for a Java Script function as follows:
out.println("<SCRIPT LANGUAGE=\"Javascript\"");
out.println("javascriptVariable1 = "+(String)request.getAttribute("JavaVariable"));
function javascriptFunction1() {
if (javascriptVariable1 == 'Test') {
<% out.println(""); %>
What the above code does is to allow u to set the value of a Javascript variable from a Java object lying in the JSP request.

Vikas Lamba

Vikas Lamba
Raghav Sam
Ranch Hand

Joined: Apr 12, 2001
Posts: 412
<script language="JavaScript">
function displayItems() {
newWindow ="ShowItems.jsp?categoryID=<%= CategoryMaster.getCategoryID(category) %>", "displayItems", "titlebar,scrollbars,WIDTH=500,HEIGHT=600,resizable");
Suppose you want to display the items of a particular category in a separate pop-up window, you can use the above script. The script will get executed on clicking a button called "Display Items". The category id is passed to the "ShowItems.jsp" page as a request parameter.

[This message has been edited by Raghav Sam (edited July 06, 2001).]

Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.<br />- Dr. Seuss
Jayson Falkner
Ranch Hand

Joined: May 07, 2001
Posts: 57
If you mean run a JSP function via JavaScript on the client-side. No, check out this FAQ.
JSP and JavaScript have completely different runtimes. However, it is perfectly valid to generate JavaScript code using JSP as demonstrated above.

Jayson Falkner<br /><br />Author of <a href="" target="_blank" rel="nofollow">Servlets and JavaServer Pages; the J2EE Web Tier</a>
I agree. Here's the link:
subject: JSP & Javascript?
It's not a secret anymore!