This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes JSP and the fly likes Dynamic XML and JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Dynamic XML and JSP" Watch "Dynamic XML and JSP" New topic

Dynamic XML and JSP

Tania Smuts

Joined: Jul 21, 2004
Posts: 7
I am trying to create an application that will allow the users to change text and labels in an XML file, which in turn I will pull into the JSP. The idea is that I will code the static portion of the JSP with the include, but there after if they want to change a text paragraph or field label, it can be done in the XML file and will be available to the JSP next time the JSP is run.

In my JSP I have the following code:

String newState = null;

if (userProfile.getPreviousState().equals("createstudentresponse")){
newState = student.getState();
} else {
newState = request.getParameter(oldState);


<table border="0" width="650">
<form method="post" name="dealerp1">
<tr><td >

Paragraph[] paragraphs = webPage.getParagraphs();

for (int i = 0; i < paragraphs.length; i++) {

Column[] columns = paragraphs[i].getColumns();

out.println("<table border=\"0\" width=\"100%\"><tr>");

for (int j = 0; j < columns.length; j++) {

out.println("<td width=\"100%\">");


</td><td width="1%"></td></tr>

The XML which is being parsed is as follows:
<FixedData><input type="text" size="2" name="oldState" value="<%= newState != null ? newState : "" %>">
<Description>Previously titled in </Description>

The problem is that at run time the piece of Java code in the JSP is run and it outputs the description, but the variable newState which is determined by the Java code that is being pulled in from the XML file is not resolved. Any ideas on how I can make this work?
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
Exactly why do you think it should be pulling in the external XML for every request?
This is a prime example of code that should be in a servlet or a bean class. Writing extensive Java logic in JSP is just hard to read and hard to debug.
Tania Smuts

Joined: Jul 21, 2004
Posts: 7
I will move it to a bean. This is just a prototype. Even if this is in a bean, I will still have the same problem. At runtime, the code to access the bean will be resolved, but the expression coded in the xml will not have been resolved.

I want to put the text and labels in XML so that the users can change any field descriptions or text at any time and we don't have to make code changes. Next time the JSP is run, the new labels as defined in the XML will be pulled in and displayed. At the same time, through the use of html in the XML, they can change the format/font/color of the labels etc. A really scaled down versions of a dynamic content management system. It works really well in all instances where there is straight HTML in the XML attribute. The problem comes in when we have forms with fields and the value of the field needs to be resolved at runtime. The field (and expression to resolve it) is defined in the XML, but as I said before, the bean\java code gets resolved but it doesn't resolve the expression coded in the XML.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
You could read the XML file into a String and put it onto the request. Then, use the JSTL XML taglibs to access the data in the XML String and print it out in your response.

James Carman, President<br />Carman Consulting, Inc.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

This thread just screams ResourceBundle at me. I'm not even sure it would be appropriate in this situation, but XML seems like an overkill for the needed feature described.

If you use JSP 2.0's message Resource Bundle features, you can just define all these replacable texts in a .properties file. Then you can even internationalize it if need be.

GenRocket - Experts at Building Test Data
Tania Smuts

Joined: Jul 21, 2004
Posts: 7
The XML string will contain '<input type="text" size="2" name="oldState" value="<%= newState != null ? newState : "" %>">' If I write that out in the response, will value="<%= newState != null ? newState : "" %>"> get resolved?
Dan Novik
Ranch Hand

Joined: Jan 26, 2005
Posts: 39
HTML output (e.g. through out.println() ) will be resolved
If you need to output JSP code you have to evaluate that output again
See for example Eval taglib here:
Tania Smuts

Joined: Jul 21, 2004
Posts: 7
Thanks Dan. The evalbody tag is exactly what I need.
I agree. Here's the link:
subject: Dynamic XML and JSP
jQuery in Action, 3rd edition