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.

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?

share|improve this question
1  
recommended reading: Why is asking a question on “best practice” a bad thing? –  gnat Dec 31 '13 at 9:23
    
I highly recommend you research MVC theory - the wikipedia article has some excellent resources on it. If you want to experiment with MVC in a Java webapp, Stripes is an excellent lightweight framework that can give you an overview of the architecture. –  Michael K Jan 2 at 19:34
1  
Here's a more specific treatment of how MVC works in webapps. –  Michael K Jan 2 at 19:36

5 Answers 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.

share|improve this answer

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.

share|improve this answer

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

share|improve this answer
    
src/main/webapp contains standard WAR layout file and is read by the maven-war-plugin when compiling a Java webapp. –  Michael K Jan 2 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 Jan 2 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. –  Brice Ruppen Jan 3 at 9:43

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.

share|improve this answer

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)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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