+5  A: 

JSPs should be used in the presentation layer, servlets for business logic and back-end (usually database layer) code.

I don't know any reason why you can't use a JSP as you describe (it gets compiled to a servlet by the containter anyway), but you're right, the preferred method is to make it a servlet in the first place.

Bill the Lizard
A: 

Yeah, this should be a servlet. A JSP may be easier to develop, but a servlet will be easier to maintain. Just imagine having to fix some random bug in 6 months and trying to remember how it worked.

sblundy
Out of personal experience, no matter where you put the code, trying to remember 6 months down the line how something worked is a hassle.
Antony
Hmm, why is it easier to find the relevant section of code in a servlet than in a JSP? Please clarify. I'd think it would be easier to find in a JSP, because you can look at the URL and immediately know what program is executed.
Jay
+1  A: 

JSP's are essentially markup that automatically gets compiled to a servlet by the servlet container, so the compile step will happen in both instances. This is why a servlet container that supports JSP must have the full JDK available as opposed to only needing the JRE.

So the primary reason for JSP is to reduce the amount of code required to render a page. If you don't have to render a page, a servlet is better.

Steve Moyer
Needing a JDK vs only a JRE is a good consideration. Amount of code required to render the page would be approximately the same, you do take a hit the first time the JSP is started though after you (re)start the server.
Antony
Yes, it's true that you need a JDK on your app server to use JSPs, but, (a) Any app server that supports JSPs is going to expect the JDK to be there if it's not included as part of the app server itself. I've never tried to install an app server with only a JRE. Would it even work? And even if it did, who cares? Is your server so constrained that you can't afford the, what, 10 MB or whatever of disk space for a JDK? (b) The performance hit is once per deployment update. It's no different than the cost of compiling a servlet before you deploy.
Jay
+1  A: 

Most java applications nowadays are build on the MVC pattern... In the controller side (servlet) you implement business logic. The servlet controller usually forward the request to a jsp that will generate the actual html response (the View in MVC). The goal is to separate concerns... Thousands of books have been written on that subject.

Alexandre Victoor
In my case the view is (as is most of the functionality) actually handled by another application that is written in Java by another company. The use of the JSP / Servlet is only necessary since the processing that the other application does causes problems at the payment application side of things.
Antony
+15  A: 

JSPs: To present data to the user. No business logic should be here, and certainly no database access.

Servlets: To handle input from a form or specific URL. Usually people will use a library like Struts/Spring on top of Servlets to clear up the programming. Regardless the servlet should just validate the data that has come in, and then pass it onto a backend business layer implementation (which you can code test cases against). It should then put the resulting values on the request or session, and call a JSP to display them.

Model: A data model that holds your structured data that the website handles. The servlet may take the arguments, put them into the model and then call the business layer. The model can then interface with back-end DAOs (or Hibernate) to access the database.

Any non-trivial project should implement a MVC structure. It is, of course, overkill for trivial functionality. In your case I would implement a servlet that called a DAO to update the status, etc, or whatever is required.

JeeBee
+2  A: 

JSPs are a shortcut to write a servlet. In fact they are translated to servlet java code before compilation. (You can check it under some tomcat subdir wich I don't remember the name).

To choose between servlet an JSP I use a simple rule: if the page contains more html code than java code, go for JSP, otherwise just write a servlet. In general that translates roughly to: use JSPs for content presentation and servlets for control, validation, etc.

Also, Its easier to organize and structure your code inside a servlet, since it uses the plain java class syntax. JSPs tend to be more monolithic, although its possible to create methods inside then.

+13  A: 

A JSP is compiled to a servlet the first time it is run. That means that there's no real runtime difference between them.

However, most have a tradition to use servlets for controllers and JSPs for views. Since controllers are just java classes you can get full tool support (code completion etc.) from all IDEs. That gives better quality and faster development times compared to JSPs. Some more advanced IDE's (IntelliJ IDEA springs to mind) have great JSP support, rendering that argument obsolete.

If you're making your own framework or just making it with simple JSPs, then you should feel free to continue to use JSPs. There's no performance difference and if you feel JSPs are easier to write, then by all means continue.

Sebastian
+1  A: 

In an MVC architecture, servlets are used as controller and JSPs as view. But both are technically the same. JSP will be translated into servlet, either in compile time (like in JDeveloper) or when accessed for the first time (like in Tomcat). So the real difference is in the ease of use. I'm pretty sure that you'll have a hard time rendering HTML page using servlet; but opposite to common sense, you'll actually find it pretty easy to code even a fairly complex logic all inside JSP (with the help of some prepared helper class maybe). PHP guys do this all the time. And so they fall into the pitfall of creating spaghetti codes. So my solution to your problem: if you found it easier to code in JSP and it wouldn't involve too many code, feel free to code in JSP. Otherwise, use servlet.

Rudi Adianto
I agree that you shouldn't put complex logic into a JSP, but it's easy enough to push it into helper classes. Just use the JSP for output formatting and -- while I'm sure the purists will jump on me for this -- for simple logic where it's more trouble to break it up than to just do it.
Jay
+1  A: 

Agreed with all the points above about the differences between JSPs and Servlets, but here are a couple additional considerations. You write:

I have an application that sends the customer to another site to handle the payments. The other site, outside of the customer, calls a page on our server to let us know what the status is of the payment. The called page checks the parameters that are given by the payment application and checks to see whether the transaction is known to us. It then updates the database to reflect the status. This is all done without any interaction with the customer.

Your application is consuming the payment service of another application. Your solution is fragile because if the payment service in the other application changes, that breaks your JSP page. Or if you want to change your application's payment policies, then your page will have to change. The short answer is that your application should be consuming the application's payment service via a web service. Neither a servlet nor a JSP page is appropriate place to put your consumption logic.

Second, along those lines, most usages of servlets/JSP pages in the last few years have been put inside the context of a framework like Spring or Struts. I would recommend Spring, as it offers you the full stack of what you need from the server pages to web service gateway logic to DAOs. If you want to understand the nuts and bolts of Spring, I would recommend Spring in Action. If you need to understand better how to tier an enterprise architecture written in a language like Java (or C#), I would recommend Fowler's Patterns of Enterprise Application Architecture.

Alan
The payment service is offered by a bank to its clients. The client enters information in the web pages on our servers and when it comes to payment is sent to the bank's web-site. At the end of the process on the side of the bank, the bank calls a page (JSP) on our side to let us know the status.
Antony
+1  A: 

I know this isn't the popular answer today, but: When I'm designing an app from scratch, I always use JSPs. When the logic is non-trivial, I create ordinary Java classes to do the grunt work that I call from the JSP. I've never understood the argument that you should use servlets because, as pure Java classes, they are more maintainable. A JSP can easily call a pure Java class, and of course an ordinary Java class is just as maintainable as any servlet. It's easier to format a page in a JSP because you can put all the markup in-line instead of having to write a bunch of println's. But the biggest advantage of JSPs is that you can just drop them in a directory and they are directly accessible: you don't need to mess with setting up relationships between the URL and the class file. Security is easily handled by having every JSP begin with a security check, which can be a single call statement, so there's no need to put security into a dispatch layer.

The only reason I can see to use a servlet is if you need a complex mapping between URLs and the resulting execution class. Like, if you want to examine the URL and then call one of many classes depending on session state or some such. Personally I've never wanted to do this, and apps I've seen that do do it tend to be hard to maintain because before you can even begin to make a change you have to figure out what code is really being executed.

Jay
+1  A: 

There are 2 pretty simple rules:

  1. Whenever you want to write Java code (business logic), do it in a Java class (so, Servlet).
  2. Whenever you want to write HTML/CSS/JS code (view/template logic), do it in a JSP.

Related question:

BalusC
Okay, so you answered the 1st part of the question. How about answering the other 2?
Antony
It's answered in the link. It makes little sense to copy'n'paste it here. It at least boils all down to those two simple rules.
BalusC
I agree that it boils down to those rules. I also agree that you have given good reasons for the rules in the other thread. I personally think it would be worthwhile to copy it here since not all people will click through to other answers. Then again, this discussion might just be enough to get them to the other page.
Antony