• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Execute javascript in servlet

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I currently have some code in a jsp that I'd like to move into a servlet. The only problem is that the jsp contains some javascript that I'd like to execute. The body tag has an onload call to this function:

Is it possible execute this from inside a servlet, or should I just move the code into a regular class and keep using the jsp to call it?
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Javascript doesn't get executed on the server regardless of whether you have it in a JSP or a servlet.

Before we go any further, why on earth would you want to move client-side markup from a JSP into a servlet? It's just template text to be sent to the client, and that's best handled by a JSP rather than a servlet.
[ October 03, 2006: Message edited by: Bear Bibeault ]
 
Jacob Fenwick
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason I'd want to use a servlet is because the process I want to use requires a parameter from the web.xml file, and I'd rather store it as an init-param than a context param.
Clarification: This is some fileupload code. I want the code to be reusable. The way I want it to be reusable is that I want to create numerous fileuploads, each by instantiating the same code, but by changing the data in the web.xml file so that it decides where the file is saved based on an init-param in the servlet. That's why I'd want to use an init-param instead of a global context-param.
However, I also need to execute that javascript markup. I don't know of a way to embed a servlet inside a jsp, so I was trying to do it the other way, storing the markup inside the servlet.
[ October 03, 2006: Message edited by: Jacob Fenwick ]
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jacob Fenwick:
so I was trying to do it the other way, storing the markup inside the servlet.



Bad idea, and runs contrary to all modern accepted best practices.

Rather, once your servlet is done with its processing, it should forward to a JSP page to render the HTML markup (along with any Javascript or other client-side stuff). You can "pass" data from the servlet to the JSP using scoped vairables on the request (via setAttribute()) including any init params that the servlet has read.
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This article might be helpful in understanding modern web app structure (and how we got there).
 
Jacob Fenwick
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bear.
I have read it. It's a good article.
I've managed to convert a lot of code from "model-less" architecture to model 1. I'm at the point now where I'm grappling with converting to model 2.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic