在Response中阻止URL编码

时间:2017-03-16 13:33:53

标签: java encoding character-encoding url-encoding

在我的一个资源中,我正在通过会话将响应中的JSON对象传递给JSP页面。

@POST
@Path("/updateUser")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response userProfile(
        @Context UriInfo uriInfo, String updateProfileData,
        @Context HttpServletRequest req
) throws JSONException, UnsupportedEncodingException {

    String session = req.getSession(true).getId();

    JSONObject jsOb = new JSONObject(updateProfileData);
    JSONArray jaArr = jsOb.getJSONArray("profileData");
    JSONObject a;

    StringBuilder sbv = new StringBuilder();
    StringBuilder sbk = new StringBuilder();
    String prefix = "";

    try {
        conn = DaoMessenger.PostGresCon().getConnection(); 
        for (int i = 0; i < jaArr.length();) {
            JSONObject jsonObject = jaArr.getJSONObject(i);
            Iterator<String> keys = jsonObject.keys();

            while (keys.hasNext()) { 
                String key = keys.next();
                Object value = jsonObject.get(key);
                sbk.append(prefix);
                sbv.append(prefix);
                prefix = ",";
                sbv.append("'").append(value).append("'");
                sbk.append(key);
            }
            query = conn.prepareStatement("INSERT INTO addresses (" + sbk.toString() + ") values (" + sbv.toString() + ")" );
            System.out.println("QUERY: " + query);

            rs = query.executeUpdate();
            System.out.println("RS: " + rs);
            i++;
            System.out.println("rowsUpdated: " + rs);
        }

    } catch (Exception e){
        e.printStackTrace();
    }


    if (rs > 0) {
        System.out.println("SUCCESS");

        URI uri;
        uri = uriInfo.getBaseUriBuilder()
            .path("../home/index.jsp?page/#/home" )
            .queryParam("obj", URLEncoder.encode(updateProfileData, "UTF-8") )
            .build();
            return Response.seeOther(uri).build();

    } else {
        System.out.println("STUCK SOMEWHERE");
        URI uri = uriInfo.getBaseUriBuilder()
            .path("/messenger/home/index.jsp?page/#/error;" + req.getSession(true).getId()).build();
        return Response.seeOther(uri).build();
    }

}

但我一直面对

  

404错误

正在编码URL和路径。 因此:?变为:%3F#变为:%23

而不是:http://localhost:8080/messenger/home/index.jsp?page/#/home

我得到:http://localhost:8080/messenger/home/index.jsp%3Fpage/%23/home

第二个不适合我,导致404错误。我通过谷歌搜索尝试了几个解决方案,但没有一个是有用的。

如何在将响应发送到JSP页面后强制它保留路径的原始字符?

1 个答案:

答案 0 :(得分:0)

尝试更改您的响应创建。

FROM:

return Response.seeOther(uri).build();

TO:

return Response.seeOther(URI.create(uri)).build();