File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes outputing javascript from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "outputing javascript from a servlet" Watch "outputing javascript from a servlet" New topic
Author

outputing javascript from a servlet

Ricky Jay
Ranch Hand

Joined: Feb 12, 2010
Posts: 62
Is there a certain way you have to code your javascript in your servelt out.println(); ?

I have html/javascript that works fine on the jsp page. I'm wanting to move it to a servlet that will generate the code off an ajax call. It's outputing all the html code correctly everything looks correct, but my "onclick" java functions aren't working. I've just copied and pasted the code in.

Is there any tricks to getting this working?
Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
can you show us your code? without any code is hard to diagnose.


Do you know why this cup is useful? Because it is empty.
Ricky Jay
Ranch Hand

Joined: Feb 12, 2010
Posts: 62
This is the line of code i think that is messing up:

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18140
    
    8

Ricky Jay wrote:Is there a certain way you have to code your javascript in your servelt out.println(); ?

Yes. You have to code it in such a way that the output is the Javascript you want.

You're approaching this from the wrong end. You say you have Javascript in which the onclick attributes aren't doing what you want? Okay. Then fix that Javascript so that it does what you want. Then fix your servlet to output that Javascript.
Ricky Jay
Ranch Hand

Joined: Feb 12, 2010
Posts: 62
I'm sorry, you may have miscommunicated this to you.

It is working on the jsp side right now. Right now I'm using ajax to call the jsp page that his javascript is sitting on. It works fine. I would like to convert this to the Ajax calling hte servlet and it outputing the html/javascript that is on that jsp page. I'm just copying the code from the jsp page. so I don't know what is wrong. I've added the "\" before the " on the lines of code.

Does this make more sense?
Ricky Jay
Ranch Hand

Joined: Feb 12, 2010
Posts: 62
hang on, I might be on to something.
I'll try to put the ' ' around the javascript and inside use \"
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18140
    
    8

Ricky Jay wrote:Does this make more sense?

If you're still looking at the Java code, then no.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Why are you trying to output Javascript code from a servlet?

There are much easier ways to do this.
If the Javascript code is dynamic, use JSP.
If not then just keep it as a static *.js file and have your ajax calls request it directly.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Ricky Jay wrote: I'm wanting to move it to a servlet that will generate the code off an ajax call.

Why would you do that?

If it's working in the JSP -- the appropriate place for it in any case -- why move it?

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ricky Jay
Ranch Hand

Joined: Feb 12, 2010
Posts: 62
Bear Bibeault wrote:
Ricky Jay wrote: I'm wanting to move it to a servlet that will generate the code off an ajax call.

Why would you do that?

If it's working in the JSP -- the appropriate place for it in any case -- why move it?

cause on this particular jsp page that it is woring on has some business logic/ database calls and I was wanting to just move it to the servlet.


I'm still a "greenhorn" so my approach to somethings probably isn't the right way. I'm learning by trial and error.

So is having a database call from a jsp page, a bad thing? That's what I was trying to avoid.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Yes, any Java code in a JSP is a poor choice. But so is markup, like JavaScript, in a servlet. The best approach is to use both.

The request is made to the servlet that performs the business logic and deals with the DB (preferably delegated to a model layer). Then the servlet forward the request to a JSP where the output markup is generated. Communication between the servlet and the JSP happens with scoped variables.

Please see this article.

Also, please edit your code block with proper wrapping so that this topic isn't 5 miles wide. Horizontal scrolling is a pain.
Ricky Jay
Ranch Hand

Joined: Feb 12, 2010
Posts: 62
So I should move it like this:
Ajax calls Servlet.
Servlet gets data from DB and then sends that to the jsp?

I didn't know that this could be down through ajax. I'm trying to load a div box with data based off a call.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Ricky Jay wrote:I didn't know that this could be down through ajax

Why not? From the point of view of the server, an Ajax request is just like any other request.

2nd request: please edit your code to wrap and keep this topic from being so wide.
Ricky Jay
Ranch Hand

Joined: Feb 12, 2010
Posts: 62
Bear Bibeault wrote:
Ricky Jay wrote:I didn't know that this could be down through ajax

Why not? From the point of view of the server, an Ajax request is just like any other request.

2nd request: please edit your code to wrap and keep this topic from being so wide.

Thanks Bear.

This topic is complete. Do not use a servlet to output javascript.

Use this method:
AJAX --->SERVLET--->JSP(forward to jsp)

Thanks again. I've tested this and it works.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: outputing javascript from a servlet
 
Similar Threads
Date comparision
OutputStream instead of out
How to generate PDF report using servlets
google map using jsp/mysql
embedding javascript into servlet