GeeCON Prague 2014*
The moose likes Servlets and the fly likes Generating XML feed for users. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Generating XML feed for users." Watch "Generating XML feed for users." New topic
Author

Generating XML feed for users.

Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
I'm trying to revise some old code that is used by some of our clients to get data provided by us.
I'm pretty sure this is not the standard way to do it. If some of you have experience on this, please don't hesitate to give me inputs.

Currently there are two parts:
1. Backend
- There are JAVA programs generating ZIP files containing XML files. Let say these files are kept in a folder called "/original_data/"

2. The front-end
- For each client there's a special folder, let's call it "/clientA/", "/clientB/", "/clientC/"
- There's a JSP that manage client access (check client's username and password, client A can only access X times/ day, what kind of data the client will get, which folder client can get data from, etc)

How a client get data:
Let say "clientA" tries to login and it provides username and password passed to the JSP, then the JSP will check whether it's a valid login or not.
- If it is NOT a valid login, then it will output error message to user.
- If it IS a valid login, then it will
1. Copy data from "/original_data/" location to "/clientA/" in the JSP
2. Then from JSP it will redirect to new file just copied to "/clientA/" folder.
I think the reason behind this is that if the client tries to access "/clientA/" folder directly, it will get old data (Force clients to access the JSP to get fresh data).

But I'm pretty sure there's a better way to do this with more robust structure (security, standard, etc).

Thanks in advance for the help.
Rodrigo Tomita
Ranch Hand

Joined: Apr 28, 2008
Posts: 70
Hi, Susan,

I think that a more "classic" design would be to have all your logic (copying files, etc) in a Servlet and keeping the JSP only for presentation purposes. That usually increases code organization and maintainability.

Another think you may want to consider, if your XML generation is not a long running process and/or data changes frequently, you may have the data generation being triggered by the user.

Just a question, could your users read the data directly from the /original_data/ folder? (instead of copying it from original_data to clientA and then having them reading clientA).

Regards.
[ July 16, 2008: Message edited by: Rodrigo Tomita ]
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
Thanks for the pointers, Rodrigo.

Actually, I'm not too familiar with how to build Servlet itself. I'm a beginner at JSP and I know it got translated into Servlet.

>>>>>>>>>>>>>>
Another think you may want to consider, if your XML generation is not a long running process and/or data changes frequently, you may have the data generation being triggered by the user.
>>>>>>>>>>>>>>
I will do some testing for this.

>>>>>>>>>>>>>>
Just a question, could your users read the data directly from the /original_data/ folder? (instead of copying it from original_data to clientA and then having them reading clientA).
>>>>>>>>>>>>>>
If they got redirected directly to /original_data/ folder, then some users might bypass the JSP and access the /original_data/ folder to get new fresh data.
Currently if they got redirected to /clientA/ folder and then if they try to bypass the JSP and go to /clientA/ folder directly, they will get old data.
I know this old codes is not the best approach at all. =) .. that's why I really want to change this.
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
I'm not sure whether this follow-up question deserves its own post or not.

------------------------------------------------------
>>>>>>>>>>>>>>
Another think you may want to consider, if your XML generation is not a long running process and/or data changes frequently, you may have the data generation being triggered by the user.
>>>>>>>>>>>>>>
I will do some testing for this.
------------------------------------------------------

I want to create the ZIP file on the fly. But the ZIP file will consists one or more XML files.
Is it possible to create both the XML files and the ZIP file on the fly?
Or I should create the XML files to hard disk and return the response to user in the form of ZIP file (of those XML files) on the fly?
Rodrigo Tomita
Ranch Hand

Joined: Apr 28, 2008
Posts: 70
Susan,

Originally posted by Susan Smith:

Actually, I'm not too familiar with how to build Servlet itself. I'm a beginner at JSP and I know it got translated into Servlet.


Yes, the JSPs are translated to a Servlet by the container. The design of having your logic in your own servlet is only for code organization - it has no (or little) impact in runtime. If you application is simple, only a few JSPs, I think you won't have problems.


Originally posted by Susan Smith:

I want to create the ZIP file on the fly. But the ZIP file will consists one or more XML files.
Is it possible to create both the XML files and the ZIP file on the fly?
Or I should create the XML files to hard disk and return the response to user in the form of ZIP file (of those XML files) on the fly?


Well, for writing a ZIP file you will need a ZipOutputStream. Usually you would have to read the bytes from the file you want to compress and write them to the ZipOutputStream.

If you are generating the XML content on the fly, I think you can have whatever is creating the XML writing directly to the ZipOutputStream... but I've never done that (so post here if it works )
[ July 17, 2008: Message edited by: Rodrigo Tomita ]
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
This is what I have so far. "test.zip" is empty when I open using web browser.

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12791
    
    5
You will never accomplish this by mixing the HTML headers and tags that JSP automatically generates with a new zip stream. Instead, use a Filter to zip the entire response -OR- use a servlet to generate the zipped output and the correct headers. The servlet approach is recommended.

Bill
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
I'm sure this is a newbie question:

What's the advantage of using SERVLET compare with other scripting language?
Some of my coworkers recommend PHP or PERL or Python. The argument is that they are more lightweight. What's the benefit of using SERVLET?
Rodrigo Tomita
Ranch Hand

Joined: Apr 28, 2008
Posts: 70
Hi, Susan,

Well, code organization (and hence maintainability) is the first thing that I can think of (assuming you are not using any service provided by the Java EE container).

If your system is going to be small and simple and you are the only (or one of the few) developer, then probably you will have your system (or portions of it) done faster in one of those scripting languages (if you are already comfortable with one of them).

However, if your system will get bigger and more complex, or you will have more developers to work with, then Servlets will probably be a better choice.

Since you already have a Java EE container running (for your JSPs), using scripting languages will not be more lightweight in runtime (unless you re-code your system).

Just another thing, maybe you get more / better answers in the Servlets forum .
[ July 23, 2008: Message edited by: Rodrigo Tomita ]
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
Thanks Rodrigo. I follow-up my questions on the servlets forum:
http://www.coderanch.com/t/366023/Servlets/java/Benefit-SERVLETS-compared-scripting-languages
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Since you feel that the Servlets forum is a good place to continue, I'll move this thread there and ask for the new thread to be closed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

There is almost always no single answer to this type of question.

First thing to consider is what languages do people who will work on the site know? Do they know Java or not? If they do, that is a big plus for Servlets, but if not then will any benefits Servlets provide be worth the time and cost of either hiring a new programmer, training the current programmer(s), or maintaining poorly designed and implemented servlet applications (a bad Servlet application will be much worse in all respects than a good PHP or Pearl application)?

Assume people do know Java, then you have the full leverage of the Java Programming Language over the scripting languages. This includes not just the entire API, but also all the open source projects out there you can use in your application pretty much as is without modification for your application.

You can also more easily design and implement a good MVC style application using existing frameworks generating more robust and maintainable applications. This is especially important for larger applications, or ones you consider important (because the important ones end up large in the long run).

The key concept is maintainability. For large and important applications it is imperative that the application stays up, is easily modified, added to, adapted. When you clearly separate content display from functionality from data storage then you can manage the three different layers independently and efficiently. This is much harder to achieve in a scripting based language. Not to say it is automatic with Servlets, but the JavaBean/Servlets/JSP tandem make it easier to achieve and there are several frameworks built around this (Model View Control) concept to make application design and implementation quicker.


Steve
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
I know one of the argument against Java (in this case: Servlets) in my discussion with my colleagues would be memory issues.

We have some console based Java programs that are taking huge amount of memory and until now we're not entirely sure why these programs are using these memories. I blame it on the way data structure are being used in the programs. But some of my colleagues blame it on the way Java handles memory.

What do you think about Servlets in memory handling?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

There's a difference between a servlet and a console-based program. Namely, a servlet is supposed to respond to a single request and then terminate, whereas a console-based program is likely to be doing something more complex and long-lived.

Not that it isn't possible to use a large amount of memory responding to an HTTP request, but it's less likely.

And frankly, if somebody blamed memory problems on "the way Java handles memory" I would believe (1) they didn't know what they were talking about, and (2) they had a bias they were trying to hide from you. You are closer to being right when you blame it on the way that data structures are being used in those programs, I suspect. But speculating about the reason is pointless. There are products called "profilers" which enable you to find out just how the memory is being used.
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
Thanks Paul. I'll take a look at some profilers available out there. If you have any recommendation, that would be great too.

Not that it isn't possible to use a large amount of memory responding to an HTTP request, but it's less likely.

This makes me thing about one thing. Sometime there are some data saved in database that I really want to put in some cache/ memory because every HTTP request uses this data and I think it will save some memory if I can get the data from database once and save it somewhere so each HTTP request doesn't need to query again and again from database. Any recommendation for a good practice of how to do this?
 
GeeCON Prague 2014
 
subject: Generating XML feed for users.