Object <-> XML <-> web-form in servlet..

A

Andrew Thompson

I need to store a custom ImageProperties class
for photos (location, date, F-stop etc..).

I would like to save it as XML (I may
consider a DB eventually, but want to
learn more about XML, this is my way
of doing it) and use a web-based form
to display and edit the data.

It seems that every time I read a thread
mentioning forms, Struts or similar tag
libraries are suggested, but when I looked
further into the tag libraries they all
seemed to stress how they made .jsp's
simpler, there was never mention of
servlets.

Can the tag-libraries be called/used
from servlets? (Or do I misunderstand
the strengths of tag-libraries)

What is the best way to approach this
Object <-> XML <-> web-form transformation
using servlets?
 
R

Ryan Stewart

Andrew Thompson said:
I need to store a custom ImageProperties class
for photos (location, date, F-stop etc..).

I would like to save it as XML (I may
consider a DB eventually, but want to
learn more about XML, this is my way
of doing it) and use a web-based form
to display and edit the data.

It seems that every time I read a thread
mentioning forms, Struts or similar tag
libraries are suggested, but when I looked
further into the tag libraries they all
seemed to stress how they made .jsp's
simpler, there was never mention of
servlets.

Can the tag-libraries be called/used
from servlets? (Or do I misunderstand
the strengths of tag-libraries)

What is the best way to approach this
Object <-> XML <-> web-form transformation
using servlets?
Tag libraries are for use in JSPs. They have nothing to do with servlets
besides the fact that the JSP becomes one. And Struts is more than just tag
libraries. Why are you wanting to use servlets?
 
A

Andrew Thompson

"Andrew Thompson" <[email protected]> wrote in message
Tag libraries are for use in JSPs. They have nothing to do with servlets
besides the fact that the JSP becomes one. And Struts is more than just tag
libraries.

Yep, I appreciate there is a lot more
to them, it just seemed every thread
about web-forms mentioned tag-libraries..
..Why are you wanting to use servlets?

It seems to be the natural progression from
having my .jsp's littered with Java code.

I find .jsp's inconvenient in various ways.

They do not seem as neat or clean to write
or debug. My .jsp book is 245 pages whereas
the servlet book is 700 pages before it hits
the index [and is much better reading ;-) ].

...so, no really clearly definable reasons,
it is just I thought servlets were a logical
direction to move in, but am still having
trouble figuring the best way to separate
the model and view in my web-apps.
 
R

Ryan Stewart

Andrew Thompson said:
Yep, I appreciate there is a lot more
to them, it just seemed every thread
about web-forms mentioned tag-libraries..
By "them", do you mean Struts? Struts is an "it", not a "them".
It seems to be the natural progression from
having my .jsp's littered with Java code.
Yes and no.
I find .jsp's inconvenient in various ways.

They do not seem as neat or clean to write
or debug. My .jsp book is 245 pages whereas
the servlet book is 700 pages before it hits
the index [and is much better reading ;-) ].
This is *exactly* why the Model 2 architecture came about.
..so, no really clearly definable reasons,
it is just I thought servlets were a logical
direction to move in, but am still having
trouble figuring the best way to separate
the model and view in my web-apps.
JSPs are your view. Servlets can be your model and controller, but take it a
step further. Look into Struts. Struts has *a* controller servlet which you
configure via XML descriptors. You don't really mess with the servlet
itself. All of your code goes in Java classes of one kind or another. You
could use some servlets if you wanted to, but there's little reason to do
so. JSPs should ideally have no Java code in them at all.
http://jakarta.apache.org/struts/

You'll also want to get familiar with JSTL. This *is* just a bunch of tag
libraries, so it's simpler to grasp than Struts. You can just throw the
..tld's and .jar's in your app and start using them. There's nothing else to
set up.
http://java.sun.com/products/jsp/jstl/index.jsp
 
S

Sudsy

Andrew Thompson wrote:
It seems that every time I read a thread
mentioning forms, Struts or similar tag
libraries are suggested, but when I looked
further into the tag libraries they all
seemed to stress how they made .jsp's
simpler, there was never mention of
servlets.

Ah, but Struts isn't just a tag library: it's an implementation of an
MVC Model 2 architecture. The "controller" functionality is provided
by org.apache.struts.action.ActionServlet which extends HttpServlet. The
programmer writes classes implementing org.apache.struts.action.Action
and interaction with them is managed by the framework according to XML
configuration files.
You use tag libraries in JSP to keep them free of scriptlets. They also
typically use attributes or parameters stored in one of the available
scopes. Most often they use attributes which have been stored by the
Action, running under the auspices of the ActionServlet.
Read a primer on Struts for more foundation.
 
A

Andrew Thompson

On Tue, 01 Jun 2004 02:30:49 -0400, Sudsy wrote:

....
Read a primer on Struts for more foundation.

I shall. I have various Struts related
bookmarks grabbed from mentions of it,
including some of your pages..

I just want to check specifically.

From what both you and Ryan are saying,
it sounds Struts/JSP would be a good way
to approach the Bean <-> XML <-> form
transfer.

Do I understand that correctly?
 
I

iksrazal

Andrew Thompson said:
What is the best way to approach this
Object <-> XML <-> web-form transformation
using servlets?

There are two ways do this easily - once you understand the
technologies involved:

1) java.beans.XMLEncoder and java.beans.XMLDecoder . Since you seem to
be using beans for presentation, this may be a good fit.

2) JAXB - which marshals objects to XML and XML to Objects. Its part
of - but not dependant on - the sun webservices toolkit and explained
alot on onjava.com . The potential disadvantage is you need a XML
schema for the xml .

HTH

Outsource to an American programmer living in brazil!
http://www.braziloutsource.com/
iksrazal
 
R

Ryan Stewart

Andrew Thompson said:
From what both you and Ryan are saying,
it sounds Struts/JSP would be a good way
to approach the Bean <-> XML <-> form
transfer.

Do I understand that correctly?
Don't you mean form <-> Bean <-> XML? Struts will help you with the form <->
Bean part and leave you open to implement the <-> XML part in any way you
wish. That's your data persistence which is usually done via a database.
Struts doesn't tie you to one data access mechanism. What use would it be if
it did?
 
A

Andrew Thompson

...
Don't you mean form <-> Bean <-> XML?

....errrr. I think it means I cannot draw
triangles in ASCII ;-)
... Struts will help you with the form <->
Bean part

...but OK, yes, that is the crux of it..
..and leave you open to implement the <-> XML part in any way you
wish.

That is often dangerous, with me, anyway.. :)
..That's your data persistence which is usually done via a database.

ehh.. I think XML description stored in
the same directory as the image should
be OK for the moment.
Struts doesn't tie you to one data access mechanism.

Good.
 
A

Andrew Thompson

2) JAXB - which marshals objects to XML and XML to Objects. Its part
of - but not dependant on - the sun webservices toolkit and explained
alot on onjava.com . The potential disadvantage is you need a XML
schema for the xml .

Do you just mean the effort required
to define the schema? What disadvantages
are you thinking of?
 
I

iksrazal

Andrew Thompson said:
Do you just mean the effort required
to define the schema? What disadvantages
are you thinking of?

Just defining a schema in this context. I like XML Schema - although
it has its warts like not even supplying a boolean type yet being a
notoriously complex specification. An average java programmer won't
know xml schema, and it just raises the bar for JAXB. Still, I look at
how .net does xml binding and I think having a flexible and pluggable
architecture, based on xml schema, is the least worst option. Matter
of fact, the first JAXB iteration, based on dtd's, was scapped due to
the near impossibiltiy of adding schema support.

What makes XML schema even more difficult when used with JAXB is the
schemas annotations required to prevent flaky method naming and so on.
In fact, its recommended to do your schemas by hand. I'm a vi guy, but
not every java programmer is. Check out this article to see where I'm
comming from.

http://www.onjava.com/pub/a/onjava/2003/12/10/jaxb.html?page=1

Still, I really like JAXB after using it for the last couple of days.

iksrazal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top