File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Difference between servlet and struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Difference between servlet and struts" Watch "Difference between servlet and struts" New topic

Difference between servlet and struts

Maheswaran Devaraj
Ranch Hand

Joined: Sep 14, 2007
Posts: 47
Hi All,

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

Maheshwaran Devaraj
Alaa Nassef
Ranch Hand

Joined: Jan 28, 2008
Posts: 471

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.

Visit my blog:
Hussein Baghdadi
clojure forum advocate

Joined: Nov 08, 2003
Posts: 3479

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

Joined: Apr 20, 2006
Posts: 3381

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!

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

It's like asking what the difference is between a brick and a house. The house is built with bricks.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Difference between servlet and struts
It's not a secret anymore!