direct call of java bean values using ajax - java

I want to call values from Java Managed beans using AJAX calls directly not using JSF capabilities. It means using simple AJAX or JQuery AJAX retrieve a value stored in a managed bean field, without using JSP write method; I have seen some samples doing this. and I think that's not a good solution. I should say as a test I want create a Web UI using plain HTML, CSS, JavaScript or AJAX and populate the UI with AJAX calls of any type without using any JSF UI components. Is it possible?
thanks,

Related

best way to import form data from HTML to java Spring controller as JSON request body

I am building my first spring boot project with java in intellij. When capturing form data from an HTML page, is there a 'best practice' way of transporting the data to the server controller as a JSON request body? I've seen solutions with javascript / ajax, but I prefer something more native to the spring library.
There are two ways.
Setting your Back-End as a REST API and completely separate your Front-End and communicating through REST calls. (as you said with JS/Ajax)
Setting your complete app under one roof. Spring and Thymeleaf are a good option. It works like this, every time you do a /GET request then spring will provide a template. Which is a Thymeleaf HTML document which has Thymeleaf specific syntax to make your life easier.
You can submit your data to your back-end through forms. When you submit the form then your app decides what happens next, will you be redirected somewhere else or will another template be opened...That's your call.
If you want a quick way to do it, go with the second way. Personally, I like the first one. It separates the concerns perfectly.

Call my GWT backend method from a JSP Page

I have a method residing at backend of GWT framework , I usually call this method via RPC.
I am in a situation where the application redirects to a separate JSP page , Now from this jsp page i like to call the same method which resides at the backend.
Is there a possibility i can call that method from my jsp page via RPC or some other means .
There are 2 approaches I'd suggest:
Load the app's JS file (in your JSP), and have a JS function "exported" from it, which knows how to make RPC calls. This function would thus work as a passthrough JSP->GWT->Backend
Expose your GWT backend through an easier protocol, one that can be used easily from JSP (such as a REST API, or even a simple ajax call), and avoid RPC alltogether.
Other than this, please be aware that you can't easily decode and/or compose a RPC message, so I don't think you can do it yourself.

Auto poll database repeatedly in jsp

I am relatively new to spring. I would like to know if there are any possible ways to automatically poll the database using Spring 4. The user would not need to refresh any pages for the code to fire.
I would need my jsp page to automatically poll the database every few seconds.
I think you are confused about how JSP works. JSP is a graphical facade on Java Servlets, which (normally) take in an http request, and returns some html or an error code. That's the servlet's entire life span... the servlet will re-run with every incoming request.
To do what it seems you want to do, you will need to find a solution in JavaScript / JQuery. A commenter has suggested you look up on AJAX programming - you should take his advice.

Is RESTful webservices with Ajax ,Json,javascript and jquery alternative of JSP?

I have perception that there is no or very less need of jsp if we use RESTful with ajax call and json.
We can update views using Javascript or jquery.
Do we need JSP in this case?
I am using jsp for session management only.
Do we need jsp only for session management in my case?
You don't need JSPs to have a HttpSession, if you manage your REST services with servlets or Spring MVC. So, the answer to your question, is no.
However, a template framework like JSP or FreeMarker is still useful in case you want to use tags that are resolved on the server.

Best way to implement Register in a Java Website

I have started working on a Web java learning project.
I am making a webapp and I want to register users to the website:
Whats the best way to implement registration functionality:
Have a register.html and in form call a servlet register which registers the user into database.
Or have a jsp page which does all this or similarly call the servlet .
Or any other..
Please explain the reasons too, that why one is better than other, or why some method should be used or preferred?
Thanks
All three that you cite are equivalent. JSPs are compiled into servlets that are HTML factories.
Yes, you need a web UI.
You'll need a database to persist the data, so you'll be using JDBC.
You'll need some object and relational models representing users and their credentials.
You'll want to read about Model-2 MVC for web apps. It describes an architecture where JSPs interact with a servlet, which delegates to other objects to do the work and redirects the response to the right JSP depending on what happens.
You'll want to read about the front controller servlet.
I will go for option 1 :
"Have a register.html and in form call a servlet register which
registers the user into database."
but with a sort of javascript/jquery validations.
Create a html page which contains form and js to validate form fields and a servlet to make
database entries for newly registered user. Options 2 is also similar with this.
Both the methods mentioned by you are same. Take html page and then call a servlet to persist in database or you take JSP page to do,both are same,as in both you are using servlets.. In both methods you will use server side validation and also handle for SQL injection.
If you are comfortable with any of the MVC framework then try to use it.

Categories