我没有关于Java的丰富经验,并且面临以下(小)问题:

如何在执行SQL语句后在Java类中执行转发到页面?
使用Jersey 1.19

资源中的代码:

@POST
@Path("/remove")
public String removeUserId(@FormParam("uid") int uid) throws Exception {

    FormData test = new FormData();
    return test.removeUser(uid);

}


子资源中的代码:

public String removeUser(@FormParam("uid") int uid) throws Exception {

    con = DbConn.apiUserProfileConn().getConnection();
    Statement stRemoveUser = con.createStatement();
    String queryRemoveUser = "DELETE FROM users " + "WHERE id = " + uid;

    int rsRemoveUser = stRemoveUser.executeUpdate(queryRemoveUser);

    return "user: " + uid + " has been removed";

}


当我尝试:

@POST
@Path("/remove")
public Response removeUserId(@Context UriInfo uriInfo, @FormParam("uid") int uid) throws Exception {

    URI uri =    uriInfo.getBaseUriBuilder().path("removeUser/index.jsp").build();
    return Response.seeOther(uri).build();

}


然后我进入removeUser/index.jsp但该用户尚未从数据库表中删除。这是因为未将uid传递给子资源中的removeUser。因此,我该如何传递uid来删除并同时执行一种调用(转发和/或重定向)至:removeUser/index.jsp

提前致谢!

最佳答案

似乎在更改removeUserId()方法时,已删除了对子资源的调用。如果删除和重定向都独立工作,请在尝试进行重定向之前先呼叫removeUser()

@POST
@Path("/remove")
public Response removeUserId(@FormParam("uid") int uid) throws Exception {
    // remove the user
    FormData test = new FormData();
    test.removeUser(uid); // you may want to use the return to indicate a success/failure

    // redirect
    URI uri = uriInfo.getBaseUriBuilder().path("removeUser/index.jsp").build();
    return Response.seeOther(uri).build();
}


稍后在index.jsp中,您可以使用request.getParameter("uid")来获取用户的ID。

10-07 13:28