Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

outputing javascript from a servlet

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you show us your code? without any code is hard to diagnose.
 
Ricky Jay
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the line of code i think that is messing up:

 
Sheriff
Posts: 26951
83
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hang on, I might be on to something.
I'll try to put the ' ' around the javascript and inside use \"
 
Paul Clapham
Sheriff
Posts: 26951
83
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ricky Jay wrote:Does this make more sense?


If you're still looking at the Java code, then no.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67618
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

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?
 
Ricky Jay
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67618
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
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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67618
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

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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic