variable input to javascript function parameter within a JSP page
What I want to do is pass a different string into the .mask function. In pseudo code it would look something along the lines of:
String passedParam= someString if(test): passedParam = "someotherString" jQuery(function($)< $("#product").mask(passedParam,); >);
Being new to both jsp and javascript I do not know the correct way to translate this pseudo code into actual working code.
It would be more clear if you don’t use a mask variable alongside a mask method. BTW, is the mask variable defined in JSP and further on used in the JavaScript script?
3 Answers 3
You can use taglibs/EL in JSP to print a string as if it’s JavaScript code. You know, JSP runs at webserver machine, produces HTML/CSS/JS and sends it to the webbrowser which in turn starts to run JS code. You should write server side code accordingly that its HTML/CSS/JS output looks right when you’re doing View Source in webbrowser.
Your pseudocode is a bit ambiguous, but I bet that you’re looking for something like this:
(don’t pay attention to syntax highlighting, the code is correct, the highlighter doesn’t recognize EL)
The $<> thing is EL (Expression Language). It will be processed when JSP runs. It should work in template text as per Servlet 2.4/JSP 2.0 (which is already over 5 years old). The ?: is the well known conditional operator. If the expression evaluates true, then the part after ? will be assigned/printed (in this case the literal string «someotherString» ), otherwise the part after the : (in this case the scoped variable mask ).
This will end up in the webbrowser as
depending on the boolean outcome of test .
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
I have a form in JSP. I have to populate it based on the request object (from the servlet). How do I use Java Script for accessing request object attributes or if you can suggest me any other better way to populate form dynamically?
5 Answers 5
You need to realize that Java/JSP is merely a HTML/CSS/JS code producer. So all you need to do is to just let JSP print the Java variable as if it is a JavaScript variable and that the generated HTML/JS code output is syntactically valid.
Provided that the Java variable is available in the EL scope by $ , here are several examples how to print it:
Imagine that the Java variable has the value «bar» , then JSP will ultimately generate this HTML which you can verify by rightclick, View Source in the webbrowser:
Do note that those singlequotes are thus mandatory in order to represent a string typed variable in JS. If you have used var foo = $; instead, then it would print var foo = bar; , which may end up in «bar is undefined» errors in when you attempt to access it further down in JS code (you can see JS errors in JS console of browser’s web developer toolset which you can open by pressing F12 in Chrome/FireFox23+/IE9+). Also note that if the variable represents a number or a boolean, which doesn’t need to be quoted, then it will just work fine.
If the variable happens to originate from user-controlled input, then keep in mind to take into account XSS attack holes and JS escaping. Near the bottom of our EL wiki page you can find an example how to create a custom EL function which escapes a Java variable for safe usage in JS.
If the variable is a bit more complex, e.g. a Java bean, or a list thereof, or a map, then you can use one of the many available JSON libraries to convert the Java object to a JSON string. Here’s an example assuming Gson.
String someObjectAsJson = new Gson().toJson(someObject);
Note that this way you don’t need to print it as a quoted string anymore.
See also:
JSP Variable in Javascript
JSP Variable in Javascript helps you to pass jsp variable to javascript.
JSP Variable in Javascript
JSP Variable in Javascript
JSP Variable in Javascript helps you to pass jsp variable to javascript. The JavaScript include a function that takes the defined variable from the jsp expression and display the message from variable using alert box.
Understand with Example
In this section, we are going to pass the jsp variable to javascript. You can see in the given example that we have create a function access() which accept the defined variable from the jsp expression «HelloWorld». The expression is used to insert the jsp variable values directly into the output. This will display the value of string variable using the alertbox on loading the function.
Here is the code of passJspvariable.jsp
|
Output will be displayed as:
Tutorials
- Creating a String
- Introduction to JSP
- Reading Request Information
- Introduction to the JSP Java Server Pages
- JSP FUNDAMENTALS
- Using Taglib in JSP. A brief introduction to taglibs and taglibs programing.
- JSP date example
- Reading Request Information
- Using Beans in JSP. A brief introduction to JSP and Java Beans.
- JSP 2.0 — New Features
- JSP Excel Tutorial
- Create Excel Sheet Using JSP
- Inserting Image In Excel Sheet Using JSP
- How to Create Excel Page Using JSP
- How to Create New Excel Sheet Using JSP
- Create Excel Sheet Using JSP
- Passing Various Data Types into Cells
- Assing Date into Cells
- Reading And Writing Excel File
- Selecting Excel Sheet File
- Zoom in Excel
- Zoom Out Excel
- Merging Two Cells
- Working With Fonts
- Working With Alignment Using JSP for Excel
- Shifting Row Using JSP
- Freeze And Split Pane
- Repeating Rows And Column
- different borders
- Create Shape in Excel Using JSP
- Creating Oval in Excel Using JSP
- Create Box in Excel Sheet Using JSP
- Filling Color in Excel Using JSP
- Set Font into TextBox String Using JSP
- Fit Hight Ad Wdth of Ecel Seet Uing JSP
- Setting Line Style
- Finding a Factorial using while loop
- JSP Simple Examples
- Calculate factorial Using Recursion
- Multiple Methods in Jsp
how to use jsp variables in javascript
posted 18 years ago
i have a doubt about accessing a jsp variable in java script, the problem is as follows
In my html document, i have jsp code to fetch the data from the database(mysql) and load the values of each record in an object and add the object to the arraylist, now after this is done i have to put the values inthe form of a tree structure(jtree) in my html page for this i am using java script, noe i have to use the arraylist values in the java script, how can i do that can any one help me out in this and send any sample code if possible.
Bartender
posted 18 years ago
All variables created in serverside code can only be accessed by clientside code if you render them as part of the HTML page your clientside code runs on. So if you want you «pass» an array of values, you really need to create a JavaScript array clientside and populate it with the values of your array. Then access that array from your JavaScript code.
Sheriff
posted 18 years ago
JPS/Servlets write a stream of text to the browser.
If you want to use the values of your java variables on the client as Javascript, you have to print their values into the the javascript declaration.
If you look at the HTML source from your browser (as you should always do) you will see:
The Java code is gone. All that remains is the text that your JSP code wrote. The browser will take this and interpret it as a Javascript function. It will create the ImAJavascriptVariable variable an initialize it with value of the string literal (‘Bob’) that it found in the text. The Javascript interpreter has no idea that that the string it’s using was the results of some JSP code that printed the value of a Java variable. It just knows that the literal ‘Bob’ was printed there.
Java and Javascript are two different languages that get executed on two different machines. They do not interact in real time.