I am trying to create a website, kind of a search engine. In the back-end I have Java servlets which process requests and return responses.
I am not sure what I should use for front-end, so that I can keep the back-end an开发者_运维技巧d the front-end completely (or as much as possible) de-coupled. Also, which one of these (or some other) has the most capabilities. Few options are:
- Freemarker templates
- JSP
- PHP
Please help me; how can I proceed with this idea?
The servlet API does not out the box offer seamless integration with PHP as view technology. You would in PHP need to invoke HTTP requests to the servlet using curl
and consorts or to provide the servlet as a webservice and use JS/Ajax to get results from it in the generated PHP output. That's may unnecessarily end up to be too clumsy and expensive.
Use a Java based view or template technology. JSP is a perfect suit since it goes hand in hand with Servlets. You just store the search results as a request attribute, forward the request to the JSP and then use taglibs/EL in the JSP to present it.
If you want more templating capabilities, e.g. reusing the same template for both HTML output or in some email message or for whatever purpose you'd like to generate the output programmatically without the need for a HTTP request, then Freemarker is a better choice.
See also:
- JSP info page
- Servlets info page
JSP's are servlets so development is can be easily coupled if not done properly and ensuring the JSP's only stay as the views.
PHP would have to be entirly de-coupled as it is a seperate language.
Freemarker templates - I have never used this product so I can't recommend anything.
Another option would be using JavaScript/HTML for your view. Servlets could serve up the data and JavaScript could run on the client side and create the view.
Of course any of your options will function it comes down to what technology you are experienced with and what is your timeframe. If you are on a short timeframe and are already familar with Java and servlets then you are not far off from just using JSP to render your front-end view. Although my preference is for servlet back-end using Jax-RS to serve the data as JSON to a JavaScript front-end view.
The usual way to create dynamic web pages in Java is to have servlets on the server that creates html that is sent to the browser. Technologies like JSP and Freemarker templates is designed as enhancements to servlets that makes it easier to create the html. JSP pages will i.e. be compiled to pure servlets when you deploy them.
If you want to decouple the technology that creates the html totally from the servlets you have created, then I would go for static html and ajax. You would create your system this way:
Create static html pages with javascript. Use a javascript library like jquery.
Create your servlets so that they accept parameters and respond using json-formatted data.
Write Javascript in your html-pages that send queries to the servlets and modifies the html-code based on the response.
精彩评论