Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between servlet and struts

 
Maheswaran Devaraj
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

What is the difference between servlet and struts ? Why we need struts?

regards,
Maheshwaran Devaraj
 
Alaa Nassef
Ranch Hand
Posts: 471
Hibernate Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Maheshwaran ,

Well, they are two really very different things. Programming web applications using servlets only is a very hard thing, since you will have to write your HTML code inside the servlet, which will produce complex, unreadable and unmaintainable code.

The same thing is with JSP. If you program a web application using JSP alone, you'll have to put your java code in JSP pages, which will also be very hard to read and maintain.

This was the reason of the rise of the model 2 architecture, where the servlet controls the flow of the request, calls a model class to do the business logic, decides which view (jsp or other) to send to the user, and then sends the response to the user. This is known as the MVC (Model-View-Controller) design pattern.

Another pattern was also created and widely used, and that is the front controller pattern. This pattern simply says "Why do we have a separate controller for each request? Why don't we have a SINGLE controller to decide what should be done with the request?". So here we have a single servlet that takes ALL requests, and decides which class/method (model) should handle it, and which jsp/page/template (view) should be sent to the user.

Of course the classes that the servlet calls to handle the request should not always be the MODEL. A typical use of struts actions is just to validate the data, and configure it, then pass the data to a service layer class to do the business logic, and this makes those action classes be considered as controllers/controller extensions, rather than the model.

From the previous paragraph, I'm sure that you have already guessed that struts is a web framework based on the front controller design pattern, and this is correct. Using struts will simplify development a lot, since it separates the business logic from the flow control and the view.

Another thing is that struts provides a set of useful tag libraries, a validation framework and the tiles framework (which allows you to create complex layouts by gluing simple JSPs together).

I hope that this would help you.
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why we need struts?

We don't
We use web frameworks to make our life easier and to devote more time to our life as web frameworks are supposed to handle the common task we to do in each web application we work on (data validation, type conversation ...).
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maheshwaran Devaraj:

What is the difference between servlet and struts ? Why we need struts?


As such they both are different. A Servlet is a technology and Struts is a framework which relies on or makes use of Servlets.

I believe Alaa Nassef's detailed reply will help you further!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's like asking what the difference is between a brick and a house. The house is built with bricks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic