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.
Related
First of all i'm a newbie in java-ee. I'm working as a java developer and where i work, the company has a web application with java-ee on the frontside and cache(intersystem) at the backend.
Is it possible that the web application may not have any servlet class? I only can find httpservlet imports.
From my understanding, java-ee application always work with servlets with his get/post/init method's. Am i right?
Also,I really don't get the difference between servlets and jsp's.
For the moment, I know that the application is using maven,struts2,jsp,hibernate,taglib...
(Sorry for my english,I try my best)
You are not right, but not totally wrong either. Java-ee application mostly works with servlet but there is also others mechanisms involve like Filter and Listener.
Filters are used to manage request and response before and after servlets are called. For example you could use one to always redirect to a login page if there is no session.
Filter documentation
Listeners are used for listening to events in a web container, like Session creation.
I don't really know Struts2 but with a little of research i found out that it works with a front filter who is interpreting request and dispatch them to your Action class.
Some infos about Struts
A lot of Framework/Apis use a similar system it's actually the design pattern Front Controller, springMVC and Jersey for example both work with an unique front servlet
As for the difference between JSP and servlet, JSPs are just file that are compiled by the web server as servlet.
What is a "reverse controller" in Play! 2.0? I've read the documentation, but I'm still struggling to understand it.
I'm specifically trying to understand the difference between controllers.routes.MyController and controllers.routes.ref.MyController. When should I use each one? (I'm using Java.)
The normal controller handles a HTTP request as defined in the file routes. The reverse controller can prepare the URL that would trigger the request.
You should use the reverse controller computed URLs instead of hard coded urls in the href attributes of your links, in the src attributes of your java scripts and in your test cases. etc. Whenever you need a link from your application back to your application, consider using the reverse controller instead of hard coding the URL.
This is useful when you later change the route to your controller, you don't have to change all the URLs in the HTML files or in the tests.
At work I have to regularly work on a site that uses Tomcat, Hybris and (I think?) Spring. Although I'm slowly learning, I'm quite unfamiliar with all of these technologies.
Is there a simple way to handle 301 redirects through Hybris? Perhaps through the Hybris administration console or Hybris management console?
Currently, we are using http://www.tuckey.org/urlrewrite/ and functionally, it works great. But adding a vanity URL or a URL redirect to the website involves editing / testing on localhost, then pushing urlrewrite.xml to stage and testing, then finally pushing urlrewrite.xml to the production environment.
Is there a better way to handle 301's with the technology we're using?
Hybris is built almost entirely upon the Spring framework. I'm not sure if the site you are maintaining uses the Accelerator template for the storefront, but if it is, then you'll want to look into Spring MVC. Look for methods that are annotated with #Controller. You can do just about anything you want with Spring MVC including 301 redirects.
There is no simple way to do this immediately in a nice configurable way in hybris. And frankly you would not want to. You should handle this in your web server.
But if you really want to, you should add a filter to the Accelerator storefront to check incoming requests against a list of items (perhaps RedirectURL Items) and redirect as required.
Could you explain exactly what you are trying to achieve ? I think like it has been said, in most cases redirection at the web server level would be more appropriate.
I've written the bare bones of my application using the MVC pattern. I don't currently have any AJAX functionality in my application but I was looking for suggestions on how I would change the architecture of my application to achieve this, to that end I'll try my best to describe my current architecture:
I have a controller servlet "controller.java" which reads the servlet path i.e. request.getServletPath() to determine the action required
I have a number of different Enterprise Java Beans (EJB 3.1) which handle the business logic and which are called by my controller servlet depending on the action requested
I have a number of views which relate to different aspects of my application to which the request is forwarded (by the controller servlet) based on the action requested (i.e. request.getRequestDispatcher(url).forward(request, response);)
I understand that the current architecture could support AJAX functionality (by matching a pattern from my "controller.java" servlet) but I'm getting to the point where I have a huge number of actions supported by my controller and it's getting messy.
Does anybody have any suggestions?
Is there a standard pattern for doing this? I'm trying to stay free of any frameworks just now as I'm a relative beginner! :-)
Thanks
If your controller supports a huge number of actions - it's where you need refactoring. In general your architecture looks correct, if the number of actions is reasonable (up to 10, I would say) per each controller.
One possible way of refactoring is to group controllers into modules.
You can check for ajax requests as follows:
boolean ajax = "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));
and then handle the kind of response accordingly. I.e. returning the view ID which is to be used in a forward or redirect, or returning some JSON which is then to be written to response body, or returning a special View object which contains this kind of information. Given this basic MVC example, it should not be that hard to expand it with ajax support.
Same idea with BalusC.
We have an MVC app that runs by itself. Now to add AJAX functionality we added JQuery and used jqGrid in the presentation layer. It communicates with the backend via AJAX. If we remove the JQuery and jqGrid, we still have a fully running MVC app.
I've put a demo of that at http://krams915.blogspot.com/2010/12/jqgrid-and-spring-3-mvc-integration. Here we integrated Spring MVC 3 and jqGrid/JQuery
I've coded some algorithms in Java and I need to include those algorithms in my web application.
I'm using Apache Tomcat and what I need to do is that when, for example, I click on the start button on my web page a Java class should be executed. I think this done by using servlets, am I right? If so, do you know where I can find some literature about it? I've searching in internet but it's a little bit confusing for me.
Yes, you're right. You'd want to write a servlet that handles request to an URI.
Here's some introduction:
http://java.sun.com/developer/onlineTraining/Programming/BasicJava1/servlet.html
Tomcat comes with some samples, you might look at the source code as a start, they should be in the webapps/sample directory.
The Tomcat documentation is also a good start.
http://tomcat.apache.org/tomcat-6.0-doc
I would check out this introduction to servlets
A servlet receives an HTTP request (e.g. a request for a page etc.). It will process that request via the methods doGet() or doPost(), and return an HTTP response. That response will contain your results (most likely an HTML page, although that's not mandatory).
I would (in order)
get a static 'hello world' page going
get a static page returning some data from your libraries
get a dynamic page returning some data from your libraries using data from the HTTP request
Integrating your library will be trivial, since a servlet is simply a Java class that can instantiate/call on any other Java class.
You will need to learn how to use Java in a web server. I would recommend using JSP just to start with as it allows you to create web pages that call Java code easily.
There are many JSP tutorials on the net - I believe this one is suitable: http://www.jsptut.com/