如何将列表转发到jsp文件?

如何将列表转发到jsp文件?,jsp,redirect,request,forward,restful-url,Jsp,Redirect,Request,Forward,Restful Url,我使用的是restful方法,其中我希望将列表传递给jsp文件。 以下是restful方法: @Path("myRest") public void handleRequestInternal() throws Exception { try { Request req = new Request(); List<String> roles = manager2.getAllRoles(); req.setAttribute("

我使用的是restful方法,其中我希望将列表传递给jsp文件。
以下是restful方法:

@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();
    }
}
@Path(“myRest”)
public void handleRequestInternal()引发异常{
试一试{
请求req=新请求();
List roles=manager2.getAllRoles();
请求setAttribute(“角色”,角色);
java.net.URI location=新的java.net.URI(“../index.jsp”);
抛出新的WebApplicationException(Response.temporaryRedirect(location.build());
}捕获(URISyntaxException e){
e、 printStackTrace();
}
}

我使用webApplicationException进入所需页面。(事实上,在使用restful方法时,我没有找到其他转发或重定向的方法)
这是我的jsp文件:

<% 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>
<%}%>
<% if(request.getParameter("roles") != null){%>
<!-- process request.getParameter("roles") into an arraylist and you are good to go -->
<%}%>


但是我从请求中什么也没有得到。getAttribute(“角色”)

有什么问题吗?

我想你最好做以下几点:
编写restful方法如下:

@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();
    }
}
@Path(“myRest”)
public void handleRequestInternal()引发异常{
试一试{
List roles=manager2.getAllRoles();
字符串角色=新字符串();
对于(int i=0;i

在jsp文件中:

<% 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>
<%}%>
<% if(request.getParameter("roles") != null){%>
<!-- process request.getParameter("roles") into an arraylist and you are good to go -->
<%}%>

Ah,您的意思是将我的变量放入url。好吧,这不是一个安全问题,这对我来说很好。非常感谢。