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.
Related
I've build a RESTful backend in Spring + Hibernate framework. Now, i'm starting with frontend in Spring MVC. So, my jsp pages make an AJAX call to the frontend URL which inturn calls the backend URL to get the data.
Is this a proper way of implementation? Or should i re-think on the design? How about replacing frontend Spring MVC with Angular.js or any other framework?
So, my jsp pages make an AJAX call to the frontend URL which inturn calls the backend URL to get the data.
No. The JSPs are your frontend. JSPs are one way to generate and deliver HTML to the client. When it is successfully delivered to the client, the HTML is rendered and the JS is executed.
Within the JS-code you are able to make calls to the backend to get your data.
Is this a proper way of implementation?
Yes.
How about replacing frontend Spring MVC with Angular.js or any other framework?
You are free to do that as you like.
Since recommending one or the other framework would be based on my subjective opinion, I won't say anything but: Angular is en vogue. So: yes, it is one possible solution.
And since Angular itself is only a JS-framework you could combine it without further ado with JSPs.
I've a java based web application running on Tomcat and it uses spring framework. I need to expose a ping URL to check whether the application is up and running. I've considered the following implementation approaches and all of them seems to work well when I tried them. However, I could not make up my mind whether one approach is better than another. Does it matter which path I take? Could someone advise which approach is better and why?
Create a web page and modify web.xml to redirect the url to the jsp page.
Create a REST service using Spring-WS
Create a servlet and return response
Use anything you want :-) But notice, that both Spring-WS and JSP are a little heavier (really not a meaningful reason here) than servlets. If you already have REST API to your aplication, use Spring-WS, if you render pages through JSP, use JSP. Or if you use none of these, write a plain servlet.
Since you are using Spring, assuming you use Spring MVC, you can just add another controller mapped to a certain URL which would be responsible for returning a status.
Solution 1 might not work depending on your requirement since a jsp page might work even if the rest of the app does not since it is not part of spring config.
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.
I want to realize the following scenario:
The frontend is based on HTML5 / CSS3 / JavaScript (with a JavaScript framework like ExtJS or jQuery). I have full HTML pages with forms, buttons, text fields, which have submit buttons which should send the data to the webserver. On the other hand I also have a website in this web application which get/send the data via a JSON webservice.
The backend should be realized with Java EE (MySQL database, Hibernate as ORM, ...). But which basic technology should I use? JavaServer Faces? Or JavaServer Pages?
What is the best method to handle JSON and HTTP GET/POST requests?
Thank you in advance & Best Regards.
Take a look at Play Framework, which has excellent JSON support, built-in JPA support and jQuery included.
Spring MVC has a very good support for rendering JSON responses and REST-like URLs. So you can use it for both your standard views and your JSON responses.
RESTEasy is a REST framework by JBoss. With it you can use anything - JSF, Wicket, etc.
Be careful not to duplicate code. Whatever solution you choose, move the code into services, and consume them from the json and regular response renderers.
I want to write ajax code in java i.e. i want use the functionality of ajax without using ajax. I'm looking for some API of JAVA which can do so.
Like we post data of a web page through JAVA program similarly I want to perform operation of ajax through JAVA program.
Please suggest.
Google Web Toolkit is Java-only framework for writing AJAX aplications.
Echo is an alternative to gwt
You can use jQuery for this. In jQuery you have the great form plugin which unobtrusively changes an existing form into an ajaxform.
HTML (in JSP):
<form id="myform" action="myservlet" method="post">
<input type="text" name="foo">
<input type="submit">
</form>
<div id="message">${message}</div>
JS ((in)directly in JSP):
$('#myform').ajaxForm({
success: function(message) { $('#message').text(message); }
});
Java ((in)directly in doPost() method of the Servlet behind myservlet):
String foo = request.getParameter("foo");
String message = "You entered 'bar': " + ("bar".equals(foo) ? "yes" : "no");
if ("XMLHttpRequest".equals(request.getHeader("x-requested-with"))) {
// Ajax request.
response.getWriter().write(message);
} else {
// Normal request.
request.setAttribute("message", message);
request.getRequestDispatcher("page.jsp").forward(request, response);
}
If you want to get some steps further, you can use Gson in Servlet to convert complete Java objects to Javascript object notation (JSON). This way you can access the data the javabean-like way in Javascript.
If your application is running on browser and its a web application, you can go with GWT. If your application is a core java application.. you can simply create a HttpURLConnection and use it.
JavaServer Faces (JSF) 2.0 should be able to do partial updates of a page in place, using AJAX under the covers. It sounds to me as if that is what you need.
I think the easiest way to get a JSF 2.0 capable server running right now, is using the latest version of Glassfish.
There are plenty of Java based AJAX frameworks out there.
Apache Struts [complex AJAX-tags (by integrating DOJO-toolkit)]
Direct Web Remoting is a framework for calling Java methods directly from Javascript code.
Guiseā¢ Framework - elegant server-side component architecture that doesn't require developers to write HTML or JavaScript
GWT - Java software development framework that makes writing AJAX applications
JAXCENT - Just-Java Framework and API for AJAX
IT Mill - Ajax framework for developing web applications for with Java language at server-side
and even more JAVA AJAX Frameworks
I suggest you take a lok at DWR - Easy Ajax for Java:
DWR is a Java library that enables
Java on the server and JavaScript in a
browser to interact and call each
other as simply as possible.
DWR is Easy Ajax for Java
It enables you to ajax-ify your web application with minimal effort.
It is not a complete new web-framework; it focuses solely on ajaxification, allowing you to keep and use your existing web framework.
If you want to use a "heavier" web framework like JSF, there exist ajax-ready JSF frameworks like IceFaces and RichFaces with provide ajax out-of-the-box.
you can submit form to servlet, when servlet response, you using jquery or prototype javascript framwork to get data from server by using Ajax call function.
I tried it and it run smoothly!
Good luck!
The quick answer is GWT. However, why would you want to do this? JavaScript is similar to Java. If you know Java, then you can easily write JavaScript code. The advantage of using JavaScript would be that you would be more versatile and would not be locked into a single tool. Using GWT to generate JavaScript (AJAX) code is not natural.