18

As a beginner to J2EE, I have recently started developing my own project from scratch using the Core of J2EE : Servlets & Jsps.

I could not evaluate whether my project folder structure is right or not. Here is my project folder structure. enter image description here

Before asking question, I admit that I could not answer or neither justify if somebody asks me, why this type of folder structure. The question: Is it a good sign to put my jsps outside of web-inf. If not, why is it so? If yes why?

Is there any standard folder structure convention for a J2EE web application, I know maven has brought up some standards but still, we can customize as per the requirement I believe.

I have done a bit of googling and found the two references 1 2

where in the answers are not on the same page, from which I couldn't draw any conclusion.

What are the points to be considered while laying out the folder structure for a J2EE web application, importantly where should the Jsps, static content should go into & why?

4

5 Answers 5

8

The standard structure for a WAR file is:

/META-INF
   Standard jar stuff like manifest.xml
/WEB-INF
  web.xml
  /classes
    /com...etc.
  /lib

Maven generates this for you using your src/main/java, resources, webapp and your dependencies (placing them in /lib) in the maven-webapp-plugin, but that's implementation. The important thing to realize is that anything you put in WEB-INF is not externally accessible, whereas everything in the root directory of the WAR is public.

Generally you don't want to put much in the root, since you want your application to handle all access using the servlets and filters you define in web.xml. It's common to see an index.html (or .jsp) in the root that redirects to a servlet, for instance a Struts action.

Typical MVC implementations such as Stripes or Struts recommend against user's accessing JSPs directly, preferring that JSPs be view-only. They recommend creating controllers that forward to JSPs after processing the request, and the JSPs merely render the result. For instance, submitting a form to /login will run an action that processes the login request, creates the user's session, and forwards the user to the logged-in view of the home page JSP.

5

The usual answer to "what is the right way?" or "is this the right way?" is..... it depends.

All I can do is tell you the pros and cons to specific ideas. What follows is 100% my opinion. I don't know of any specific requirements or rules. I'm sure somebody will disagree with me.

JSP's

Let's work on whether to put JSP's in WEB-INF or not.

Pros of putting JSP's in WEB-INF:

  • You control how the JSP's are executed. If you want a JSP to be parameterized and re-usable (which is really hard with a JSP anyway), you can put them into WEB-INF and use a servlet or a Struts action controller or some other front controller to do pre-processing and then pass control to the JSP, passing in the right environment context (like request attributes, any security checks, parameter sanitation, etc.)
  • You can programmatically or even at a firewall or IDS level block HTTP requests to *.jsp to reduce the likelihood of somebody uploading a JSP to the web root and then being able to execute code as the web server. They'd have to over-write an existing JSP. Not a huge security gain, but it does make compromise slightly harder.
  • Enforces good habits, like MVC, front controller, servlet filters, dependency injection, etc. as opposed to a big monstrous JSP that does all the work itself and is difficult to read/maintain.

Cons of putting JSP's in WEB-INF:

  • You cannot access the page directly, even if it is a simple standalone page which needs no upfront processing. This is because files under /WEB-INF are not servable by a servlet container.

Static files

In terms of purely static files like HTML, image, stylesheet, javascript, etc. put those under the web root (my_app in your case), but NOT /WEB-INF (because it is not accessible).

Overall layout

As for the overall directory layout, it depends somewhat on your build process. I like storing everything under "src" or "source" because it makes it clear what files are generated by building and which are pure source files. main lets you separate test code like junit classes from your main source code, which is good too. But if you don't have any unit tests (oh no!), then it's a meaningless distinction.

On the other hand, if you don't manipulate the web root at all during build (like if it's all JSP and static files), then perhaps you keep it at the top level, like /webroot or /deploy and copy files in as needed, such as .class or .jar files. It is a habit of human beings (especially developers) to over-organize. A good sign of over-organizing is having lots of folders with only a single sub-folder.

What You've Shown

You've indicated that you are following a convention set by maven, so if you are already using maven, just stick with that layout. There is absolutely nothing wrong with the layout you described.

1

Well, your src/main/webapp surely reminds me of a maven project. Which is good.

For the my_app/jsps I'm not sure though. Developers usually leave their jsp in the webapp folder, or if you want to do a little bit of url-mapping, in a webapp/jsp directory.

!Warning! : You should never put a jsp file in web-inf. Your WEB-INF should only contain xml files to configure your website. Remember, your jsp is a webpage or part of a webpage.

You can use folders name like template, partial... whatever feels fine for you. It should be easy to find for a stranger. Just separate different types of contents like fullpages, templates, partial views...

3
  • src/main/webapp contains standard WAR layout file and is read by the maven-war-plugin when compiling a Java webapp.
    – Michael K
    Commented Jan 2, 2014 at 19:40
  • 1
    There is no reason why you shouldn't put JSPs in WEB-INF, as long as you have configured servlets in your web.xml that will eventually forward to them. This is the standard way of implementing a Struts app - all requests go through the Struts servlet, which maps them to an action and then to a JSP.
    – Michael K
    Commented Jan 2, 2014 at 19:46
  • I agree with the fact that a jsp should not be accessed directly and preventing that should be considered good practice. But there are other means to do that. Commented Jan 3, 2014 at 9:43
1

I agree with Brice. I'm beginner too in J2EE, but I think that it´s better working easy and clearly in the beginning.

The root folder is WEBAPP, and you should make your web structure thinking that the most of the pages will locate there. If not, when the pages communicate between them probably you can't manage file relationships without errors.

1

Actually WAR application can be constructed without WEB-INF/web.xml. It is possible to make WAR application with only Java classes inside.

Source: A web.xml Deployment Descriptor Elements

With Java EE annotations, the standard web.xml deployment descriptor is optional. According to the servlet 3.0 specification at http://jcp.org/en/jsr/detail?id=315, annotations can be defined on certain Web components, such as servlets, filters, listeners, and tag handlers.

So nowadays is possible to build WAR than looks like JAR with .war extensions :)

Answering your question, WAR structure depends on your requirements.

http://en.wikipedia.org/wiki/WAR_(Sun_file_format)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.