I have been doing web development with PHP for the last few years, and like a lot of people, I have a strong dislike for PHP. I also have done lots of Java development, but never web development. Having a good knowledge of Java, I decided I would try web development using JSP. I have read a few articles but I am not fully "understanding" it. My first question is, what is the difference between a JSP (Java Servlet Page) and a servlet? Is a JSP not simply a file that contains a servlet and HTML? My second question is can Servlets interact with HTML elements as easily as PHP? Can I check for HTML form input using POST/GET etc.
I was also wondering if Servlets could use "native" java code. By this, I mean can I use the same code that I use in desktop applications/use the same methods and classes that I have already made. What I am trying to get at is, can servlets do (almost) anything that a desktop application can do. Can I access the servers file system to delete/ modify files? Can I use third part java libraries in my JSP? If someone could clear this up for me that would be great! Thanks in advance!
JSPs are a templating language for generating servlets. Every JSP is compiled into a servlet. Everything you can do with a JSP can be written as a servlet.
No, a JSP does not contain a servlet. It's compiled into servlet Java code and then that's compiled into Java byte code.
Servlets can interact with all HTML elements. They're HTTP listeners.
I would strongly advise you not to use native code in a servlet.
You should not be putting scriptlet code into JSPs. That's a 90s style of writing JSPs that's been discredited. Use JSTL. JSPs are for display only.
At a basic conceptual level:
JSPs are similar to PHP in that they provide server-side mark-up scripting to standard HTML pages, allowing for dynamic content within a page. The language a JSP uses is similar to HTML, but it is not valid HTML without being pre-processed by a web container like Apache Tomcat. A JSP can contain Java code, although due to separation of concerns this is often discouraged in large systems.
Servlets are separate files written entirely in Java and are used to process actions requested by the client. The requests sent to a servlet can take the form of any HTTP action type, i.e. GET/POST/PUT/etc. A servlet can technically do everything (and more) that a JSP page can do, but the mark-up language that JSP provides is more suitable for working on the presentation layer.
As an example: you may have an HTML form in your JSP that you POST to a servlet, which might then use the posted form data to send a secure email. Once the email has been sent, the servlet may redirect you to a success/failure page, which in turn could be another JSP page. More recently Javascript has become a more common way to interact with servlets, using the XMLHttpRequest API.
Take a look at this guide, particularly the diagram provided.
Related
I'm learning JavaEE now and I kind of feel that JSP can be messy... I hate to mix my Java code with HTML... I want to separate the front end view and Java code as much as possible.
I've ever tried Twitter Bootstrap and I find it amazing. Is there any way that I can use HTML and JS(maybe some framework like Bootstrap) to substitute for JSP view technique? How should I accomplish the data interaction?
Yes, that is what all the kids are doing. You use REST.
You can see an example of just that in this Quickstart.
Strictly speaking, no. JSP is a server-side technology. That means that the code you write in the page is executing on your server in the environment you have set up there. HTML and JavaScript are client-side technologies; the code you write in these languages executes in the web browser of the people viewing your website. Neither technology is a complete replacement for the other; both are needed.
Even if you move more of functionality out of JSP into JavaScript (a "heavy client" implementation), you still need some server technology for your JavaScript to call to. You could get rid of JSP by moving some of the server functionality into other technologies: webservices (JAX-WS, Axis, etc.), RESTful services (JAX-RS, ApacheCXF, etc.), or straight up synchronous web-pages. But the point is regardless of what you use or whether you get rid of JSP entirely, you still need a server-side technology.
I´ve programmed some basic web applications using GWT. So all I´ve coded is just pure java (although the client part is transformed to javascript).
What I wanna know is how can I access and manipulate html elements (fields of a form, some text, etc) from server side using java (no GWT).
I mean, a web application where I have some .html files and then I use some kind of framework from server side. Is that way of accesing html element framework dependent? How could I manipulate those elements from a servlet, for example?
I know it´s a very general question, but I´d appreciate any help or tips, thanks.
I think, at first you might want to look at a basic servlet tutorial and at a JSP tutorial. These are the first steps, no frameworks are involved here, but I think for the basic understanding of how it works you really should look through this information, and then only read about Spring or any other framework with a great MVC structure.
What I wanna know is how can I access and manipulate html elements (fields of a form, some text, etc) from server side using java (no GWT).
The server side creates the HTML (possibly using data submitted through a form). At the lowest level, it's simply concatenating text. The various web application frameworks offer different ways to work at a more abstract level, i.e. via a template mechanism (JSP) and/or through component libraries that replace elements in a basic HTML layout with the actual data (JSF, Wicket).
However, once the HTML has been generated and sent to the client, it cannot be manipulated by the server, except through Javascript sent along with the HTML and executed by the browser. AJAX then allows it to connect back to the server.
Take a look at Spring MVC Framework.
My website is built on JSP and Java Servlets. Html, Javascript and CSS (limited) is contained within my JSP pages as well.
I recently started looking into Joomla. I have quite a bit to learn yet...
I understand that Joomla is based on PHP.
[ I don't know anything about PHP, but I've read that knowing PHP is not really necessary in order to use Joomla. Do you agree? ]
I have some questions about Joomla -
1) The code that Joomla generates, is that in PHP? or Html? Perhaps a combination of both?
2) How, if possible, can I integrate my JSP code into what Joomla generates?
-- or do I need to integrate the Joomla code into my JSP code?
I have a fully functional website designed in JSP and Java - but after seeing the layout/design that can be accomplished with a CMS, such as Joomla, I want to know how I can 'convert' my site, or 'integrate' it, and still keep it running under JSP and Java.
Joomla is written in PHP. It generates the HTML files which are output to the browser. As far as I know, you'll need PHP to use Joomla unless there's been some sort of JSP port for it (which is highly unlikely).
Simply, Joomla IS PHP, and it generates HTML code which the browser reads.
Also, a CMS has nothing to do with layout/design, a CMS is simply that, a Content Management System. They are usually designed to be used as the actual website itself, the user just picks a theme and inserts content.
If you are looking to keep the server-side business based on JSP, JAVA and upgrade the layout using Joomla you are facing a lot of PHP programming and the result would not be great in terms of performance. It is easier to migrate to a solution based on JSF and Facelets.
Joomla is written in PHP ans as with every other server side webbased view/template technology (including JSP), it just generates HTML/CSS/JS (a webbrowser doesn't understand anything else anyway).
If the sole requirement is to have a webbased blog engine with a CMS in Java/Servlet, then checkout Hippo. It's similar to Wordpress/Joomla (although less eye-candy imo).
You could check out Quercus from Caucho which allows you to run PHP applications in a servlet container. I believe they support Joomla though I'm not sure which versions.
Possible solution or answer is that you will need complete rewriting.
You can use Joomla for the web content and JSP for the front end of an enterprise application. Integrating external content in Joomla can be done in many ways, for example, you can embed pages in Menu items, AJAX calls to retrieve content from your JSP pages -either in html or in another format (JSON or XML).
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 and 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.
I'm a .NET web developer who has just been asked to produce a small demo website using NetBeans IDE 5.5. I have no experience with Java up to this point.
I've followed a couple of quick tutorials, one which just uses a JSP file and another which uses a servlet. My concern at this early stage is that it looks difficult to keep my application code away from my markup.
Using JSP files looks quite similar to the old days of classic ASP. On the other hand, servlets look useful but seem to involve a lot of writing out markup to the output stream, which I'm not happy with either.
Is there an equivalent to the ASP .NET code-behind model, or any other strategies for separating out markup and code? Is it possible to keep markup in the JSP and then use the servlet from the JSP?
You can't do something similar with ASP.NET code behind using pure Java EE technologies. You need to use an MVC framework like Spring MVC or Struts. The idea is that you create your controller (Java Class) and a JSP page and configure an action to tie the JSP page with the controller.
It isn't a simple as the ASP.NET as it requires configuration and an external framework.
Netbeans has JSF framework embedded. Have a look at this tutorial.
The thing about Java is that it doesn't really come 'bundled' with stuff in the same way .NET does... you generally go looking for the goods.
This makes starting with web apps in Java daunting because there are so many options. Many of them are built off JSPs, which is a plus.
If you are set on using vanilla JSPs you are not going to have a good time. Otherwise, I would suggest Wicket, Stripes or Spring MVC.
Stripes is probably the simplest of the three (Spring is a little complicated and Wicket is conceptually different from the other two). Having said that Spring MVC is probably the most used of the three..
All Stripes really is is JSPs and ActionBeans. Action beans are Java classes that contain actions, which are methods that perform your actions. Theses ActionBean classes also contain the data for the current page.
This page has more information about Stripes.
Wicket has "proper mark-up/logic separation" (as they put it).
You might want to take a look at Custom Tag Libraries in JSPs
For a simple website I would use Struts and Tiles, and make use of the Tag Libs that they provide to make the JSPs look neat and marked up, and separate the code into Actions in the Controller - an Action is just a block of code the Struts servlet calls after doing its work.
It doesn't take long to get the basics of Struts 2 and Tiles, you can pick it up in a day easily.
And yes, Servlets are quite a simple level to write such systems, and it is easy to end up with HTML generators there, which as you've said, is clearly not right. I think you need to be putting more values on the request and session objects, and then formatting them within the JSP.
The strength here is that you can pick your MVC framework to match the scale of your application, yet still have access to raw servlets should you require them for specific actions (e.g., one thing I did in the past was generating and serving an Excel file from values in the database).
If you want to get something out quickly (I note you're putting together a demo), I would perhaps forgo the Java web frameworks, and simply use the servlet solution, combined with a simple templating solution.
Either Velocity or Freemarker will provide the facility of separating your markup from your code. Velocity is simpler than Freemarker. Freemarker is more powerful than Velocity.
I'm not down on the various frameworks mentioned. It's just that if your timescales are short and you're happy to program to the servlet interface for this requirement, then the above may provide the quickest/dirtiest solution :-)
You can use this and do the same as what you did in .NET! It is a very good MVC framework.