aspose file tools*
The moose likes Servlets and the fly likes Execute javascript in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Execute javascript in servlet" Watch "Execute javascript in servlet" New topic
Author

Execute javascript in servlet

Jacob Fenwick
Ranch Hand

Joined: Apr 28, 2006
Posts: 55
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?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jacob Fenwick
Ranch Hand

Joined: Apr 28, 2006
Posts: 55
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

This article might be helpful in understanding modern web app structure (and how we got there).
Jacob Fenwick
Ranch Hand

Joined: Apr 28, 2006
Posts: 55
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Execute javascript in servlet