• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP scriptlet passing a file name to clientside javascript

 
r merritt
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok this is probably a silly issue on my end but I am trying to use a scriptlet of JSP to pass the file name to my javascript client code like this



but I get an error
SyntaxError: missing ; before statement[Learn More]

I know the scriptlet works cecause when i click on the error i see




which is the correct name of the file
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65342
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you look at the generated HTML page? Is the JavaScript syntax correct? (Hint: no)

P.S. Why on earth are you using scriptlets 15 years after they were obsoleted?
 
r merritt
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the HTML is where I see

var String dataFile = ".\\temp\\" + 35E2FF995DC8809DA6124A692DF8FF76.3dcb0dc7e1f939e96f2fa8569aba741d_SASServer1_1.csv;

I am trying to pass a file named with a session id ...so how would I get it from server to client without using scriptlets?
 
Stefan Evans
Bartender
Posts: 1807
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thing I see missing from your generated code is quote marks around the second operand of the string addition.
i.e. to be valid javascript, it should be generating:
var String dataFile = ".\\temp\\" + "35E2FF995DC8809DA6124A692DF8FF76.3dcb0dc7e1f939e96f2fa8569aba741d_SASServer1_1.csv";

Having said that, programmatically sending a file from the client to the server is not something that can generally done via javascript (major security issues)

Also make sure you remember the distinction between scriptlets (java code between <% and %>) and javascript which is code between <script> //code </script> tags
Java runs on the server.
Javascript runs in your browser on the client.

In general this will not be the same machine.  A file that exists on the server will not be on the client and vice versa.
The standard way to get a file from client to server is to use the HTML file input component on the page, which requires user interaction to populate.
 
r merritt
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stefan that was the problem along with


correct

what is the best way to pass server to client side variables i did quite understand what BB was alluding too.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65342
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
r merritt wrote:what is the best way to pass server to client side variables i did quite understand what BB was alluding too.


Java scriptlets in JSP have been obsolete since 2001 when JSP 2 was introduced. You should be using the JSTL and EL in your JSP pages rather than obsolete scriptlets.

With regards to "passing" data from server to client -- you don't really do any kind of "passing". Rather, as you have seen, you write the values into the generated HTML via the JSP template (that is, after all, what a JSP page is).

And, the generated text must be valid JavaScript (or HTML, or CSS, or whatever you are generating). The problem was that you were generated invalid JavaScript syntax.

If the concept of JSP as a template is unclear, please read this article to understand how JSP operates.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic