开发者

Reading a XML File from a servlet class and displaying it on JSP page

First of all i would like to know whether my approach is correct or not. I have products that are stored in a xml file. I will be re开发者_高级运维ading this file, and storing Products, so that i can pass it to the JSP page. Here is my Product.xml file

I would also like to know, what sort of data structure I should use. Since on the products page, I will have an ADD TO CART button, for every product.

<inventory>
<product>
    <name>Dictionairy</name>
    <description>Words from the English language</description>
    <price>400</price>

</product>
<product>
    <name>Calculator</name>
    <description>solving numbers</description>
    <price>1000</price>

</product>
<product>
    <name>LCD</name>
    <description>displaying output from computer</description>
    <price>8000</price>
</product>
</inventory>


Parsing the XML in the servlet and turning it into an in-memory data structure that the JSP can use sounds rather heavy-weight.

Passing XML to a JSP which then parses and traverses it sounds even worse.

If the servlet is extracting kosher XML, and the task of the rendering code is simply to filter and render the content as HTML, then maybe you should be looking at an XSLT processor rather than a JSP to do the rendering.

Indeed, you could even send the XML in the HTML response with an embedded processing directive to tell the client-side what XSL to use ... if it wants to.


Well, as a quick shot I'd go for a class Inventory containing a collection of Product classes which represent all possible products. Then have a class Cart which can also contain products (references to the products) as well as a quantity (this might actually be a CartPosition and Cart holds positions).

Note that Inventory should be stored in application scope, since it seems to be the same for each customer. Keep in mind that access to the inventory might need to be synchronized or it should at least be locked/hidden until fully read. Cart on the other hand might be stored in the session.

Also note that this is a very simple approach. Doing a real shop involves a lot more.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜