如何使用 Jersey for java 在浏览器中呈现新的 .jsp 文件? [英] How do I render a new .jsp file in the browser using Jersey for java?
问题描述
我的网站转到一个登录页面,我想在用户登录时重定向到另一个页面.我有一个POST"方法将用户名"和密码"发送到服务器,服务器检查是否用户名和密码存在.
My site goes to a login page that I want to redirect to another page when the user logs in. I have a "POST" method that sends the "username" and "password" to the server and the server checks if the username and password exist.
这是我的方法
@POST
@Path("logIn")
public void signIn(@PathParam("profileName") String profileName, @PathParam("password") String password) {
if (profileService.getProfile(profileName) != null && (profileService.getPassword(profileName)).equals(password)){
//Render a new page ex "feed.jsp"
}
else {
//send unsucessful message back to client??
}
客户端能够正确发布用户名和密码并检查它是否存在......我只是不知道如何让它呈现(重定向到???)一个新页面
The Client is able to POST the username and password properly and check if it exists... I just have no idea how to make it render (redirect to???) a new page
推荐答案
你可以...
重定向,(使用 Response.seeOther(URI)
,传递任何需要的值作为查询参数.例如
You could...
Redirect, (using Response.seeOther(URI)
, passing any needed values as query parameters. For example
@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();
}
注意:请参阅此答案中对此选项的更正.以上将不起作用.
或者你可以..
使用 Jersey 的 JSP MVC 特性,并且也清楚地展示了此处.例如
Note: please see correction to this option in this answer. The above will not work.
Or you could..
Use Jersey's JSP MVC feature, and also clearly demonstrated here. For example
@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>
旁白:您真的想在 URI 路径中传递密码吗?这是一个巨大的安全风险.最好在请求正文中实际传递它.
Aside: Do you really want to pass the password in the URI path? hat's a huge security risk. Better actually pass it in the body of the request.
现在我考虑了一下,你应该总是从登录 POST 重定向,根据 POST/REDIRECT/GET 模式.如果您想在整个方法中使用 JSP MVC,您可以让控制器返回登录页面的 Viewable
(在 GET 上),并在成功 (POST) 时重定向到提要控制器,否则重定向回相同的登录页面(GET).有几种不同的解决方案.
Now that I think about it, you should always redirect from the login POST, per the POST/REDIRECT/GET pattern. If you want to use the JSP MVC for the entire approach, you can have a controller returning a Viewable
for the login page (on GET), and on success (POST), redirect to the feed controller, else redirect back the same login page (GET). There a are few different solutions.
例如
@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
}
}
这篇关于如何使用 Jersey for java 在浏览器中呈现新的 .jsp 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!