Java Web Project Structure Best Practice

253

Solution 1

To continue my previous answer, I have many web projects. In all of them the structure under src is more or less the same. The packages are roughly separated to 3 logical layers.

First is the presentation layer, as you said, for servlets, app listeners, and helpers.

Second, there is a layer for the hibernate model/db access layer. The third layer for business logic. However, sometimes the boundary between these layers is not clear. If you are using hibernate for db access then the model is defined by hibernate classes so I put them in the same area as the dao objects. E.g. com.sample.model holds the hibernate data objects and com.sample.model.dao hold the dao objects.

If using straight jdbc (usually with Spring), then sometimes I find it more convenient to put the data objects closer to the business logic layer rather than with the db access layer.

(The rest of the stuff typically falls under the business layer).

Solution 2

It really depends on your web framework.

For example if you use Wicket, java files and webpages co-exist in the same directory while in most other frameworks, pages (.jsp files or whatever is your presentation engine) and code-behind stuff (java files ) are completely separate.

So read the documentation that comes with your framework (Spring MVC, Struts, JSF e.t.c).

Another good proposal is to use Maven Archetypes to generate a skeleton for your specific framework. Some web frameworks (such as seam) have even their own code generation tool that lays the foundations for your web project.

My only good suggestion (that is not mentioned by Yoni) for the src directory is to make packages according to business purpose and NOT according to type/layer

That means packages for

  • com.mycompany.myproject.customers
  • com.mycompany.myproject.departments
  • com.mycompany.myproject.billing
  • com.mycompany.myproject.reports
  • com.mycompany.myproject.admin

and NOT

  • com.mycompany.myproject.entities
  • com.mycompany.myproject.tables
  • com.mycompany.myproject.graphs
  • com.mycompany.myproject.dialogs
  • com.mycompany.myproject.servlets

The second structure is too generic, tends to resolve around huge packages with unrelated stuff and is hard to maintain.

Solution 3

First, the to follow the conventional structure of a popular ide, ala Eclipse, Netbeans, etc. In Eclipse, for example, everything is arranged already with a WEB-INF and META-INF folders, so packaging and deployment is easy. Classes source code (typically under src) is automatically copied to WEB-INF/classes. There are a few other considerations:

  1. If you are using MVC, then it is possible that you don't need to access your JSPs directly. If so, keep the JSP source code under WEB-INF/jsp, for security reasons.
  2. Similarly, keep custom tag files under WEB-INF/tags.
  3. Keep javascript files in js folder, css files in style folder, etc. All folders should be at the same level as WEB-INF to mimic a real deployment.
  4. It is good to separate your code into packages according to layers. Obviously your Hibernate daos don't need to be at the same package as your servlets.
  5. If you end up with too many servlets in the same package, consider sub-packaging them accord to functionality, but it is nice that they have a common ancestor package - it helps with readability.

Solution 4

Java Web Structure

Share:
253
ruchika doifode
Author by

ruchika doifode

Updated on June 04, 2022

Comments

  • ruchika doifode
    ruchika doifode almost 2 years

    Current setup

    1. mysql connector version-mysql-connector-java-5.1.46
    2. sqoop version-sqoop-1.4.7.bin__hadoop-2.6.0
    3. hadoop version-hadoop-2.7.5
    4. java version- Jdk-8u171-linux-x64/jdk1.8.0_171(oracle JDK)
    5. OS-Ubundu

    hadoop running in local mode single node

    Note: Also tried with openjdk , same issue exist with this version also

    Sqoop Command : bin/sqoop import -connect jdbc:mysql://localhost:3306/testDb -username root -password root --table student --target-dir /user/hadoop/student -m 1

    Error Screenshot

  • Guido
    Guido over 15 years
    I prefer WEB-INF/content or WEB-INF/view, who knows what is or could be the presentation technology...
  • Mark Davidson
    Mark Davidson over 15 years
    Some good points there Yoni. I am however still interested in peoples opinions on how in particular the src directory should be structured with packages, aka would servlets be put in a package called presentation since they are mainly call JSP content.
  • Mark Davidson
    Mark Davidson over 15 years
    @kazanaki Unfortunately in this project I am just using plain MVC, no framework. I do already have my src directory structured according to the maven rules. With your package layout do you sub package that at all into the different layers or just drill down in to sub purposes.
  • kazanaki
    kazanaki over 15 years
    @Mark subpackages are layers. For the example I give customers has customers.actions, customers.servlets, customers.entities e.t.c. But in a really big project subpackages can be sub purposes as you say. No correct answer there IMHO
  • Renato
    Renato over 4 years
    I upvoted this answer: I had big project and It's true, packages associated with layers are very difficult to maintain but some questions: where put the interfaces? And if there are common interfaces or classes (abstract for example)?