1

I am simply trying to send a new .jsp page to be rendered by the client using the URIBuilder. So I have a main.js send a POST to the server which calls logIn(). For now I just want this to send a new .jsp file to the client to be rendered. Nothing happens thus far, I have tried using different file paths - before I just used "Feed.jsp" as the file path.

I feel like there is more to this that I am not picking up on.

Here is my main.js file. It sends a POST to server successfully via the logIn() method. This main.js is used successfully by my index.jsp file.

var rootURL = "http://localhost:8080/messenger/webapi";

$(function() {

$('#btnRegister').click(function() {
    var username = $('#username').val();
    registerProfile();
});

$('#btnSignIn').click(function() {
    logIn();
});

function logIn() {
    var profileJSON = formToJSON();
    $.ajax({
        type: 'POST',
        contentType: 'application/json',
        url: rootURL + "/profiles/logIn",
        dataType: "json",
        data: profileJSON,
        success: (function(data){
            alert("Success!");
        })
    });
}
function registerProfile() {
    var profileJSON = formToJSON();
    $.ajax({
        type : 'POST',
        contentType: 'application/json',
        url: rootURL + "/profiles",
        dataType: "json",
        data: profileJSON,
        success: (function() {
            alert("Resgistered");
        })
    }); 
}   
function formToJSON() {
    return JSON.stringify({
        "profileName": $('#username').val(), 
        "password" : $('#password').val(),
        });
}
});

Here is my is the method LogIn() called in my ProfileResource.java. It successfully can call the post but for some reason when I include the UriBuilder it does nothing.

@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@POST
@Path("/logIn")
public Response logIn( @Context ServletContext context) {
        UriBuilder uriBuilder = UriBuilder.fromUri(URI.create(context.getContextPath())); 
        uriBuilder.path("Deployed Resources/webapp/Feed.jsp");
        URI uri = uriBuilder.build();
        return Response.seeOther(uri).build();
}
}

So basically my index.jsp gets rendered to the client. My "btnResgister" button does what it needs, my "btnSignIn" just doesn't do anything although I know it accesses the "profiles/login" resource fine.

Update I have implemented my login method like this using user peeskillets UriUtils class:

@POST
@Path("/logIn")
public Response logIn( @Context ServletContext context, @Context UriInfo uriInfo) {
    URI contextPath = UriUtils.getFullServletContextPath(context, uriInfo);
    UriBuilder uriBuilder = UriBuilder.fromUri(contextPath);
    uriBuilder.path("Feed.jsp");
    return Response.seeOther(uriBuilder.build()).build();
}

But the POST is still not being completed. I am wondering if this has to do with the ServletContext or UriInfo parameters... Are these parameters automatically sent when I POST or do I have to send more info from the client using .js?

Also here is my file structure:

enter image description here

theGuy05
  • 417
  • 1
  • 7
  • 22

1 Answers1

1

Apologies for this answer. It seems the ServletContext.getContextPath() only returns the relative path and not the full uri. In which case, when Jersey finds a relative URI, it will construct a full URI from the base URI of the application. So you will always get a path containing the Jersey root path (which will never lead to the jsp page).

I don't know how else to get the the complete context path (full URI), from Jersey except to do some string manipulation. You can use

public class UriUtils {

    public static URI getFullServletContextPath(ServletContext context, 
                                                UriInfo uriInfo) {

        URI requestUri = uriInfo.getRequestUri();

        String contextPath = context.getContextPath();

        if (contextPath.trim().length() == 0) {
            String fullContextPath = requestUri.getScheme() 
                    + "://" + requestUri.getRawAuthority();
            return URI.create(fullContextPath);
        } else {
            int contextPathSize = contextPath.length();
            String requestString = requestUri.toASCIIString();
            String fullContextPath = requestString.substring(
                    0, requestString.indexOf(contextPath) + contextPathSize);
            return URI.create(fullContextPath);
        }
    }
}

Then use path with a UriBuilder the path of the jsp page, relative to the servlet context path. For example, if your jsp page is at the root of the context path (i.e at the root of the webapp folder, or in most cases same location as index.jsp), you can do

@POST
public Response post(@Context ServletContext servletContext, 
                     @Context UriInfo uriInfo) {

    URI contextPath = UriUtils.getFullServletContextPath(servletContext, uriInfo);

    UriBuilder uriBuilder = UriBuilder.fromUri(contextPath);
    uriBuilder.path("feed.jsp");
    return Response.seeOther(uriBuilder.build()).build();
}

I will make a correction to the first linked post :-)

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Hey once again thankyou for the through answer. I have attempted to use your class and method in the UPDATE, if you could have a look. – theGuy05 Aug 23 '15 at 20:20
  • Print out the uri (after you append Feed.jsp). If it is the same as the url that leads to the index page, then there is no reason for it not to work. Let me know – Paul Samsotha Aug 23 '15 at 21:24
  • ok so it outputs this: http://localhost:8080/messenger/Feed.jsp. This is what I want isn't it? I am still not getting a message client side that says the POST was successful – theGuy05 Aug 23 '15 at 21:48
  • You're not supposed to get a message. It supposed to redirect to the Feed page. – Paul Samsotha Aug 23 '15 at 22:12
  • ok in the main.js I made it so it is meant to produce an alert when the POST is successful. It does not produce this alert and it does not redirect also – theGuy05 Aug 23 '15 at 22:20
  • Do [some debugging](http://stackoverflow.com/a/27351400/2587435) to get the status code and message. – Paul Samsotha Aug 23 '15 at 22:28
  • So this is how a redirect works. The server sends a status code of 30X, with a `Location` headers whose value is the url to redirect to. This is what `Response.seeOther(url)` does. It sets the status to 302 and `Location` header to the `url`. When the browsers sees this it automatically sends a GET request for the url in the `Location header. Your javascript is not notified of this, so your alert should not happen. So do some debugging with the tools I showed you. Look for the status and any error messages in the response. – Paul Samsotha Aug 24 '15 at 00:46