Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlets - JSPs - EJB  RSS feed

 
Chris Weathers
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between a servlet and a JSP exactly? And how do EJBs fit into this equation?
 
Bajji Pat
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,

We have a design pattern or rather an architecture called the MVC(Model View Controller) according to which responsibilities are split between the layers to have a loose coupling between the various components that make an application.

So JSP belong to the View tier(where the data is rendered) and Servlets belong to the Controller tier(which routes/controls the flow of request/response) and EJB belong to the Model tier(where the actual business logic resides)

Balaji
 
Chris Weathers
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks...so can it be viewed as the servlets sit in between the JSPs and the EJBs?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are plenty of ways to glue the bits together. Here's a common one that I used at work:

The browser submits a GET or POST to a servlet. The servlet forwards the request into the model, maybe EJBs, maybe plain Java. The model returns some data to the servlet. The servlet temporarily puts the data on the session or the request object, and transfers to a JSP. The JSP is all HTML and tags, as little Java as possible. The JSP gets the data, merges it into the HTML and writes the HTML back to the browser.

This gives you nice separation of concerns. The servlet fits the controller definition well. It interprets the user action - which button or link was hit - into a business action - which model function to call - and a next view. It's possible to write one servlet that runs an entire application based on configuration - for incoming request code X call model function Y and forward to JSP Z.

Does all that sound like fun or what!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, all that missed the original question - what is the difference between JSP and servlet?

Servlet came first. It's a Java class that knows about the HTTP request and response. They can generate HTML by writing strings to the output stream.

out.write("<html><head> etc");

This is a really ugly mix of Java and HTML, hard to read and maintain and all that. So they invented JSPs which look like HTML with little bits of Java sprinkled in. The first time you use a JSP the container generates Java code for a servlet and compiles it. Now you never see the ugly mix of Java and HTML that winds up in the servlet. Unless of course you make your JSP an ugly mix of Java and HTML, which you can guess is A Bad Thing.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In short, JSPs are merely a short-hand for writing servlets. That is somewhat of an oversimplification, because the JSP spec also introduces tag libraries, but in the grand scheme of things that's really what they are. As Stan said, you write HTML with some Java sprinkled in. This gets translated into a servlet by the container. But, your JSP code is MUCH shorter than the corresponding servlet ...

index.jsp


Generated index_jsp.java


Of course, there's some stuff in the generated source that you wouldn't normally do, but you get the point. Note: That source was generated by Tomcat.
 
Chris Weathers
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all, that makes perfect sense!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!