• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

How to call a java class in JSP / javascript

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All:

I'm sure this question has been asked many times in this forum but my searching has not revealed a concrete answer for me.

I have a JSP that has several enterable fields, all of which are to be used to build an XML document to be written on a directory on a server. This same JSP page has a HTML "Submit" button tag that has an "on-click" event handler code. When the user enters data into all of those fields and clicks the submit button, I would like to call a POJO, sending to it all of the data elements that were entered in those enterable fields and have that POJO create and write out the XML file.

Can I do this this way? Can a POJO be called via javascript? If yes, can someone direct me to where I can find a sample working solution? If a POJO canoot be called in javascript then what are my alternatives? Do I somehow save the data in a DOM object and have the JSP invoke the POJO sending in the DOM object? If so, how do I do that? Do I use AJAX?

I'm sure there must be a way to complete such a fundamental process, but I cannot find a solution.

BTW: So far I have been able to create the XML document in javascript but then I am reading that javascript cannot write files on a directory on a server. Can someone confirm this?

Thank you for your time.
Gary
 
Sheriff
Posts: 67750
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
No, you cannot call a POJO in JavaScript. You have a fundamental misunderstanding of how JSP works. Please read this article to understand JSP fundamentals and why that is impossible.

And yes, Ajax is the answer.
 
Greenhorn
Posts: 12
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, i think you should be able to accomplish that using AJAX.

When you click the submit button, it will call a javascript function. This function will be responsible for sending your form-data to a JSP and getting the response.

In the JSP, you should be able to call a POJO and make it create an XML with the data received, since it's on the server side.

Do a little search on Google about AJAX and you will soon find how to do it. I recommend W3Schools.
 
Bear Bibeault
Sheriff
Posts: 67750
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

Emmanuel F. Borges wrote:function will be responsible for sending your form-data to a JSP ...


Sending data to a JSP for processing is considered a poor practice. A servlet is more appropriate.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, within a JSP you can call a POJO type method.



In the above code, you have a java method called printStatusInfo that takes a request and can return a String. It can return an XML string that you can alert to the screen to validate you are working properly.
 
Bear Bibeault
Sheriff
Posts: 67750
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
Welcome to the Ranch,

justin chin wrote:Yes, within a JSP you can call a POJO type method.


But that is not what was asked. The question regardied calling a method from JavaScript, which cannot happen as already pointed out,

And, scriptlets have been discredited for almost 10 years now, please do not encourage their use,.
 
justin chin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I understood the original question. The user wants to call a java method from within a javascript snippet that lives with a JSP.

1. Onclick calls your javascript snippet.
2. Your javascript snippet that lives in the jsp (which lives on a Web App Server) has access to java code via the <%= methodName(parms) %> functionality. You can past that method your request which has all of your form parameters in it.
3. You can use a JSPF to contain the java code "methodName" that can access the Web App Server Java layer to keep things a little cleaner. This of course then can access anything you can imagine including POJO's.

This probably isn't the recommended way to solve this solution, but this answers you question directly.
 
Bear Bibeault
Sheriff
Posts: 67750
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

justin chin wrote:
2. Your javascript snippet that lives in the jsp (which lives on a Web App Server) has access to java code via the <%= methodName(parms) %> functionality. You can past that method your request which has all of your form parameters in it.


Completely incorrect.

The JSP snippet executes on the server long before the page is even sent to the client where the JavaScipt can be interpreted, and where form parameters can be entered. The JavaScript in no way has access to the Java method. None at all.

Your misunderstanding of how JSP works can be addressed by reading this article.
 
Those are the largest trousers in the world! Especially when next to this ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic