Rendering a new .jsp page using JAX-RS UriBuilder

僤鯓⒐⒋嵵緔 提交于 2019-12-24 11:28:03

问题


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:


回答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 :-)



来源:https://stackoverflow.com/questions/32151507/rendering-a-new-jsp-page-using-jax-rs-uribuilder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!