2

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.

Here is my method

@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

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
theGuy05
  • 417
  • 1
  • 7
  • 22

1 Answers1

6

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();
}

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>

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.


UPDATE

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.

For example

@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
    }   
}
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Actually could you explain your update a little further? So I want to send a GET to the server that will return the Viewable? Then I send a POST when I "LogIn" that checks if the username and password exists that will redirect to the Viewable? – theGuy05 Aug 13 '15 at 03:16
  • Holy moly dude you give good answers! I am very new to this (just started learning last week) so this is very helpful – theGuy05 Aug 13 '15 at 03:25
  • When there is return new Viewable("/feed", model); what is the model? – theGuy05 Aug 13 '15 at 03:31
  • Look in the first link ("demonstrated here") I provided. It's just a map for any model properties you want to add to the jsp page. Also note, for the redirects, if you want to send any state over to the jsp page, it should be sent in query parameters as shown in the first example. Then retrieve the parameter with `@QueryParam` in the method signature of the redirected endpoint – Paul Samsotha Aug 13 '15 at 03:33
  • Also how does it know to render feed.jsp in the feed controller class? Wher e would I give the filepath to feed.jsp? – theGuy05 Aug 14 '15 at 03:00
  • Did you read the [complete link](http://stackoverflow.com/a/31900846/2587435) I provided? It gives an example of where to place things and configuration – Paul Samsotha Aug 14 '15 at 03:09
  • Also one thing to keep in mind that I didn't mention, is that when using the jsp-mvc, you need to configure your Jersey app as a filter rather than a servlet. I guess I should have mentioned that. You can see also how that is done in the same link – Paul Samsotha Aug 14 '15 at 03:12
  • In the first example, what is the ServletContext? It is not recognizing it. – theGuy05 Aug 15 '15 at 16:21
  • It's part of the servlet APIs. You need [this jar](http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api/3.1.0) – Paul Samsotha Aug 15 '15 at 16:48
  • It can't seem to find feed.jsp.. I included my problem in the edits – theGuy05 Aug 15 '15 at 17:41