你可以...
重定向,(使用Response.seeOther(URI) http://docs.oracle.com/javaee/7/api/javax/ws/rs/core/Response.html#seeOther-java.net.URI-,将任何需要的值作为查询参数传递。例如
@POST
@Path("logIn")
public Response login(@Context ServletContext context) {
UriBuilder uriBuilder = UriBuilder.fromUri(URI.create(context.getContextPath()));
uriBuilder.path(.. <path-to-your-jsp> ..);
uriBuilder.queryParam("key1", "value1");
uriBuilder.queryParam("key1", "value2");
URI uri = uriBuilder.build();
return Response.seeOther(uri).build();
}
注意:请参阅对此选项的更正这个答案 https://stackoverflow.com/a/32152473/2587435。上面的方法不会起作用。
或者你可以..
使用泽西岛的JSP MVC 功能 https://jersey.java.net/documentation/latest/mvc.html#d0e15323,并且还清楚地证明了here https://stackoverflow.com/a/31900846/2587435。例如
@POST
@Path("logIn")
public Viewable login() {
Map<String, String> model = new HashMap<>();
model.put("key1", "value1");
model.put("key2", "value2");
return new Viewable("/feed", model);
}
feed.jsp
<html>
<body>
<p>${it.key1}</p>
<p>${it.key2}</p>
</body>
</html>
Aside: Do you really想要在 URI 路径中传递密码吗?这是一个巨大的安全风险。最好将其实际传递到请求正文中。
UPDATE
现在我想了一下,您应该始终从登录 POST 重定向,按照发布/重定向/获取 https://en.wikipedia.org/wiki/Post/Redirect/Get图案。如果您想在整个方法中使用 JSP MVC,您可以让控制器返回一个Viewable
对于登录页面(在 GET 上),成功后(POST),重定向到 feed 控制器,否则重定向回相同的登录页面(GET)。有几种不同的解决方案。
例如
@Path("/login")
public class LoginController {
@GET
public Viewable loginPage() {
...
return new Viewable("/login", model); // to login.jsp
}
@POST
public Response loginPost(Form form, @Context UriInfo uriInfo) {
...
UriBuilder builder = uriInfo.getBaseUriBuilder();
if (success) {
builder.path("/feed"); // to FeedController GET
} else {
builder.path("/login"); // to LoginController GET
}
return Response.seeOther(builder.build()).build();
}
}
@Path("/feed")
public class FeedController {
@GET
public Viewable getFeed() {
...
return new Viewable("/feed", model); // to feed.jsp
}
}