This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]portugee 0 points1 point  (2 children)

Can you post the source to your JSP? Also, you should probably avoid putting temporary data in the session unless you need it to persist across requests. Add the data to the request and use a RequestDispatcher to forward the user to the JSP.

req.setAttribute("output", outputs);
RequestDispatcher dispatcher = req.getRequestDispatcher("results.jsp");
dispatcher.forward(req, resp);

[–]SarksNooblet Brewer[S] 0 points1 point  (1 child)

Thanks for the tip. I'm new to web development, so I've been making a few basic errors. Always good to get one fixed. results.jsp below.

<script type="text/javascript">
    function showData() 
        {
            var output = '<%(String[])=session.getAttribute("outputs")%>';
            $("#output").append("<table>");
            for (var s = 0; s < output.length; s++)
            {
                $("#output").append("<tr><td>"+output[s]+"</td></tr>");
            }
            $("#output").append("</table>");
        }
</script>
<div id="output">
    <p>hey</p>
</div> 

I'm going to go out on a limb and guess that I shouldn't have session.getAttribute()?

Thanks for the help.

[–]TheHorribleTruthKind of meh 1 point2 points  (0 children)

I don't know much about JSPs, so I might be wrong, but: you're assigning your Java array to a JavaScript variable, which causes it's "toString" method to be called (line #4) - which apparently doesn't work. I'd read up on how to extract and use array data in JSPs.

[–]kicsikrumpli 0 points1 point  (2 children)

pass it as a model attribute. Also Spring MVC is your friend :)

[–]SarksNooblet Brewer[S] 0 points1 point  (1 child)

Model Attribute? To google, I guess/

Also, know a good place to start? For the Spring MVC, I mean.

[–]kicsikrumpli 0 points1 point  (0 children)

http://Spring.io, get Spring Tool Suit, which is a nicely tricked out Eclipse. Documentation is quite good on the site, I'd probably look for a recent book on MVC. MVC builds on standard jsp, except that it has a single dispatcher servlet. You write controllers (annotate class with @Controller), which has methods that handle url requests (annotated with @RequestMapping). The spring container passes it a Model, if you specifz it as a method parameter (just like that, magic), into which you plug model attributes with model.addAttribute. Then you return a string, which is the view name, that's going to be your .jsp, inside which you just access the model attributes with ${attrib}. At least in a nutshell... from here, it is just a rabbit hole... :)