0

I'm using a restful method in which I want to pass a list to a jsp file.
Here is the restful method:

@Path("myRest")
public void handleRequestInternal() throws Exception {
    try {
        Request req = new Request();
        List<String> roles = manager2.getAllRoles();
        req.setAttribute("roles", roles);
        java.net.URI location = new java.net.URI("../index.jsp");
        throw new WebApplicationException(Response.temporaryRedirect(location).build());
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}


I use webApplicationException in order to go to my desired page. (In fact I found no other way to forward or redirect, when using restful methods)
And here is my jsp file:

<% if(request.getAttribute("roles") != null){%>
<%      Object obj = (Object)request.getAttribute("roles");
    List<String> roles = (List<String>) obj;%>
        <select name="role">
    <%int size =  roles.size();%>
    <%for(int i = 0; i < size; i++){ %>
        <option value = "<%= roles.get(i)%>"><%= roles.get(i)%>
    <%} %>
    </select>
<%}%>


But I get nothing from the request.getAttribute("roles")
What is the problem?

Matin Kh
  • 5,192
  • 6
  • 53
  • 77

1 Answers1

1

I think you better do the following:
Write your restful method as follows:

@Path("myRest")
public void handleRequestInternal() throws Exception {
    try {
        List<String> roles = manager2.getAllRoles();
        String role = new String();
        for(int i = 0; i < roles.size(); i++){
            if(i != roles.size() - 1)
                role += roles.get(i) + '-';
            else
                role += roles.get(i);
        }
        java.net.URI location = new java.net.URI("../index.jsp?roles=" + role);
        throw new WebApplicationException(Response.temporaryRedirect(location).build());
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}


And in your jsp file:

<% if(request.getParameter("roles") != null){%>
<!-- process request.getParameter("roles") into an arraylist and you are good to go -->
<%}%>
  • Ah, you mean to put my variables into the url. Well as it's not a security thing, that works fine with me. Thank you. – Matin Kh Jul 07 '12 at 07:15