tags:

views:

140

answers:

2

I'm working on a website running in Tomcat that stores a lot of member data, and I'm not sure how to best package it.

I want to be able to deploy a war easily without needing to remove anything from it first.

My first thought was to break out the member data into its own directory outside of the war, but my problem is that some of that content needs to be linked in the website (images, etc).

There has to be a better solution than deploying a war, then copying back the data. Does anyone have any suggestions?

+4  A: 

Your first thought was correct. Storing data in outside direcotry is best solution. You can easily fix problem with images by creating servlet that would access that outside directory on image request.

I can't post my code, but here is similar snippet that creates response. You just have to create class extending FileServlet and write code to load file from your directory.

http://snippets.dzone.com/posts/show/4629

Then, add this to your web.xml:

<servlet>
 <servlet-name>My File Servlet</servlet-name>
 <servlet-class>my.package.FileServlet</servlet-class>
 <load-on-startup>0</load-on-startup>
</servlet>

<servlet-mapping>
 <servlet-name>My File Servlet</servlet-name>
 <url-pattern>/upload/*</url-pattern>
</servlet-mapping>

and every link like http://myapp.com/upload/userimage.jpg will be created by servlet

GvS
This is what I was thinking, but I couldn't think of how to do the servlet.Say, I break out the member dir to /usr/members. If I try and return a link there, the container won't be able to find it. So what do I need to do in the servlet?
Jesse
I've added some more explanation and code.
GvS
Many thanks. I just did a small test, and it worked great!
Jesse
+1  A: 

You should definitely separate your data from your application. As I am unsure on the reasons you originally had for putting member data (apparently images and something else) I can offer suggestions based on a more ideal environment.

If you are storing information on a lot of different people and need access to it in multiple places, why wouldn't you choose to use a database? Perhaps even a free one like PostgreSQL or MySQL?

If you store your data in a database it is easy to retrieve through java, either use a ORM like Hibernate or just some basic JDBC libraries. This assumes you understand SQL and the database layer in Java.

Then if you need access to certain files you can either create a Service Orientated Architecture (SOA) and use a restful web service to pull the images based off a url. This would allow storage to be offloaded to another machine in your trust zone.

It is hard to offer a good solution without more details on the problem and your server setup.

Martin Dale Lyness
We're already using a db to store the member data, but needed file storage for things like images.As to why it is like that, well, it wasn't my choice. I would assume it was done to provide easy access to the images for display on the site.
Jesse