I am confused to understand the structure of a 3 layer programm. Where is good place for exceptions, entity and etc.? If I use MVC, in my UI model, how to transfer entity class to model class and etc.?

I have created a project that called Ramznegar. It is web project. In this a user can register and get a account. Then login and can create category, like gmail, yahoo, etc... In each category user can add any item and its password. This programm actually is password keeper.

For developing this program I created four project with following structure:

  1. RamznegarDAL: contains all DAO file
  2. RamznegarBLL: contains logic of the program. like password decrypt and encrypt class
  3. RamznegarUI : contains all JSP and Servlet class.
  4. entity : contains 2 type of classes. entity (like Category, Item and User class) and exceptions (include all exception class that use in program like DataSourceAccessException)
    RamznegarDAL use entity as library.
    RamznegarBLL use RamznegarDAL and entity as library RamznegarUI use RamznegarBLL and entity as library.

Now, please say me this structure is correct or if I have a problem with 3 layer concept.

  • 1
    Resource requests ("I am looking for books, projects, examples, etc...") are a poor fit for the Stack Exchange Q&A format. Adding this to the question doesn't help make it a better question. The format is designed for where you have an identified problem that can be answered here.
    – user40980
    Commented Jul 8, 2013 at 20:39

1 Answer 1


There is a good example about the MVC pattern applied to JSP and Servlet world in the HeadFirst Servlets and JSP book.

Basically, the Controller part is represented by a Servlet. The JSP is the view and the Model functions are performed by some Plain Old Java Object, i.e. a regular Java Class. In the book they show how you can write a simple BeerExpert app as an illustration of the MVC pattern naturally supported by the Web tier of the J2EE.

The HTML code of the starting page.

<h1 align=”center”>Beer Selection Page</h1>
<form method=”POST”
  Select beer characteristics<p>
  <select name=”color” size=”1”>
    <option value=”light”> light </option>
    <option value=”amber”> amber </option>
    <option value=”brown”> brown </option>
    <option value=”dark”> dark </option>
    <input type=”SUBMIT”>

The JSP code (used to display results)

<%@ page import=”java.util.*” %>
<h1 align=”center”>Beer Recommendations JSP</h1>
  List styles = (List)request.getAttribute(“styles”);
  Iterator it = styles.iterator();
  while(it.hasNext()) {
    out.print(“<br>try: “ + it.next());

The Servlet code (used to initialize the Model, get the result of its calculations and forward them to the View component)

package com.example.web; 

import com.example.model.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class BeerSelect extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)
           throws IOException, ServletException {

String c = request.getParameter(“color”);
       BeerExpert be = new BeerExpert();
       List result = be.getBrands(c);

request.setAttribute(“styles”, result);
       RequestDispatcher view = request.getRequestDispatcher(“result.jsp”);
       view.forward(request, response);

The Model (used to perform the actual calculations and contain the business logic; can also contain interaction with the database)

package com.example.model;
import java.util.*;

public class BeerExpert {
  public List getBrands(String color) {
    List brands = new ArrayList();
    if (color.equals(“amber”)) {
       brands.add(“Jack Amber”);
       brands.add(“Red Moose”); 
    else {
       brands.add(“Jail Pale Ale”);
       brands.add(“Gout Stout”);

Please refer to the cited book for more detailed description.

UPDATE - July, 01, 2013

Regarding th three-tier architecture. There are a lot of possible ways to approach this question. The 3-tier design pattern means that your software is architectured in three parts: Presentation layer (the user interface part of the app, might be a browser, SWING app, Android / iOS app., etc), Business Logic Tier (your server-side app consisting of servlets, POJOs, EJBs, etc.) and the Data store tier (could be a relational or object database, NoSQL database, etc.). So, the three-tier architecture describes how your whole software system / application is organized. There are also so called design patterns that are used to organize the particular tiers / layers of your application / software system. For example, the Business Logic and Presentation tier of your app can be organized in an MVC pattern (like in the previous example, where JSPs and HTML were the UI / View part constituting the Presentation layer of your architecture, while Servlets (the Controller part) and POJOs (the Model part) were collectively your Business Logic Layer. The Data Storage Layer could be covered by the Oracle or MySQL or PostgreSQL database servers and while they become part of your thee-tier architecture, they have nothing to do with the MVC pattern you used to organize your business logic. If you used a SWING app as the Presentation Tier, you could also write that app alone in the MVC style.

You know that there are a lot other options besides MVC. For example, you could build your Business Logic + Presentation Tiers in Model-View-Presenter, Model-View-ViewModel and lots of other design patterns.

Lastly, your could also write a one-tier application (like a MS Access) in an MVC way. The way you architecture your whole system (which tiers you have, etc) does nothing to do with how each of the tiers are designed (MVC, MVP, etc.).

  • thanks for your response. but im not problem with mvc. my problem is in three layer structure and his combination with mvc. do you known any sample project source code that construct over three layer architecture. i think see over a sample project can help me to understand 3 layer architecture. Commented Jul 1, 2013 at 9:25
  • @RasoulTaheri You are welcome. I have updated my answer to provide information on the relationship between the three-tier architecture and a design pattern like MVC.
    – akhilless
    Commented Jul 1, 2013 at 11:46
  • Thanks you. but i have a question. are you sure Servlet is in buesiness layer? if it's true then jsp is in business layer beacuse web server create Servlet from JSP file. Commented Jul 2, 2013 at 9:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.