Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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 need a opensource project that implements 3 layer programmign feature. for study conde and learn three layer in action. can you bring forward me a opensource project that implements it. thanks

update - 2013/7/11:
i create a project that called Ramznegar. it is web project. in this user can register and get a account. then login and can create category. like gmail, yahoo and etc. in each category user can add any item and its password. this programm actually is password keeper.
for develop this program i create 4 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 true or have a problem with 3 layer concept. and what is its problem.
thanks.

share|improve this question

closed as too broad by user61852, Kilian Foth, BЈовић, Bart van Ingen Schenau, Dynamic Jul 2 '13 at 19:26

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

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. –  MichaelT Jul 8 '13 at 20:39
add comment

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.

<html><body>
<h1 align=”center”>Beer Selection Page</h1>
<form method=”POST”
  action=”SelectBeer.do”>
  Select beer characteristics<p>
  Color:
  <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>
  </select>
  <br><br>
  <center>
    <input type=”SUBMIT”>
  </center>
</form></body></html>

The JSP code (used to display results)

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

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”);
    }
    return(brands);
  }
}

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.).

share|improve this answer
    
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. –  Rasoul Taheri Jul 1 '13 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 Jul 1 '13 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. –  Rasoul Taheri Jul 2 '13 at 9:01
add comment

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