为什么在网址中忽略数字符号?

时间:2015-11-03 05:17:11

标签: web-services rest url filter jersey

我应该开发一个网站服务,并通过泽西休息解析下面的网址: http://localhost:8080/userApp/processMsg/request?Username= $用户名和安培;名字= $ lastName的&安培;消息= $消息和; SessionID的= $的sessionId

我使用下面的代码并正常工作:

@POST
@Path("/request")
@Produces(MediaType.APPLICATION_XML + ";charset=utf-8")
public UserResponse getUsers(@QueryParam("Username") String Username,
                               @QueryParam("LastName") String LastName,
                               @QueryParam("Message") String Message,
                               @QueryParam("SessionId") String SessionId
) {

    // my code
}

我的问题是当我有#'#' url中的字符,在这种情况下,从此字符到url的结尾,将被忽略。我无法对网址格式进行任何更改,并且#符号始终存在于消息字段的末尾。 为了避免这种情况,我在我的web.xml中添加了一个过滤器,使用以下代码:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain       next)
        throws IOException, ServletException
{
    // Respect the client-specified character encoding
    // (see HTTP specification section 3.4.1)
    if(null == request.getCharacterEncoding())
        request.setCharacterEncoding(encoding);


    /**
     * Set the default response content type and encoding
     */
    response.setContentType("text/html; charset=UTF-8");
    response.setCharacterEncoding("UTF-8");


    next.doFilter(request, response);
}

但是request.getParameterMap()的值与之前的相同,并且没有url的结尾。例如,当我在url中时: http://localhost:8080/userApp/processMsg/request?Username= $ test& LastName = $ test2& Message = $ HelloWorld#& SessionId = $ 123456,我在网址收到的申请是: http://localhost:8080/userApp/processMsg/request?Username= $试验&安培;名字= $ TEST2&安培;消息= $的HelloWorld

我该如何处理这个问题?

1 个答案:

答案 0 :(得分:1)

pound(#)符号是URL中的保留字符。您需要对其进行URL编码。

http://someurl?Message= $的HelloWorld%23安培; otherParam

http://localhost:8080/userApp/processMsg/request?Username= $试验&安培;名字= $ TEST2&安培;消息= $的HelloWorld%23安培; SessionID的= $ 123456

英镑符号用于"锚定#34;。

空格和其他特殊字符都需要URL编码