0

I am creating an mvc application, in controller (servlet), I am populating a request variable and then trying to read and show it in view (jsp) via foreach jstl tags. The problem is that I don't know how to read xml from session and then parse it and show attributes and elements in jsp page. Below is the code that I have.

In servlet, set request attribute:

request.setAttribute("XMLDocument", xmlResultStr);

This is a sample xml that can be stored in xmlResultStr.

<?xml version="1.0" encoding="UTF-8"?>
<SearchResults>
    <result url="http://haacked.com/" totalcount="14">
        <keyword count="9">start</keyword>
        <keyword count="5">end</keyword>
    </result>
    <result url="http://feeds.haacked.com/haacked" totalcount="14">
        <keyword count="9">start</keyword>
        <keyword count="5">end</keyword>
    </result>
</SearchResults>

In jsp page, I am using this code but completely without luck, I am searching for many hours and fully stuck on this:

<x:parse xml="${XMLDocument}" var="xml"/>

                <c:out value="${xml}" />

                <x:forEach select="$xml/SearchResults/result" var="item">
                    <c:set var="elementName" value="url"/> 
<x:out select="$item/*[name()=$elementName]" />
                    <h3><a href="<x:out select="$item/*[name()=$elementName]" />" target="_blank"><x:out select="$item/*[name()=$elementName]" /></a></h3>
                    <p>
                        <%-- somehow display keywords list here, like shown below --%>
                    </p>
                    <hr />
                </x:forEach>

What I want is this:

  <h3><a href="http://haacked.com/" target="_blank">http://haacked.com/</a></h3>
            <p>
                (start - 9, end - 5)
            </p>
            <hr />

1 Answer 1

2

My suggestion: avoid storing an XML string in the session and trying to parse it in the JSP.

Adopt the MVC principles: parse the document using Java code, before storing it in the session. Transform the document into a Java bean and store this Java bean in the session instead of storing the XML string.

Then use the JSTL core taglib and the JSP EL in the JSP to display the information stored in this Java Bean.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.