gpt4 book ai didi

jsp - 在JSP中无法使用UTF-8进行编码

转载 作者:行者123 更新时间:2023-12-02 06:58:17 24 4
gpt4 key购买 nike

我使用以下jsp搜索一些文本:

<%@page session="false" contentType="text/html; charset=utf-8" %>
..
...
<form method="post" action="searchPageAction.html">
<input type="text" name="search_value" value="ö-Test"/>
</form>

...

Servlet看起来像这样:
public class SearchServlet {
private static final String SEARCH_VALUE = "search_value";

@Override
protected final void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws ServletException {
try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String searchValue = request.getParameter(SEARCH_VALUE);
String redirectPath = "/content/vrbp/de/tools/suchergebnis.html?q="+searchValue+"&_charset_=UTF-8";
try {
response.setCharacterEncoding("UTF-8");
response.sendRedirect(redirectPath);
} catch (IOException e) {

} catch (IllegalStateException e) {
}
}
}

现在,当我搜索“ö-Test”示例时,在浏览器中会写以下链接:

http://www.testproject:4502/content/myProject/searchResult.html?q=%F6-Test&charset=UTF-8



我期望:

http://www.testproject:4502/content/myProject/searchResult.html?q=ö-Test&charset=UTF-8



并在searchResult.html中将文本 ö-Test ist printet作为 &#65533;-Test。如何在 UTF-8中正确编码?我做错什么了?

最佳答案

您需要设置默认参数编码,可以通过 Sling Main Servlet 设置进行配置,即-

http://localhost:4502/system/console/configMgr/org.apache.sling.engine.impl.SlingMainServlet

最后一个选项 sling.default.parameter.encoding应该设置为UTF-8,否则它将退回到默认值,如帮助文本中所指定:

The default request parameter encoding used to decode request parameters into strings. If this property is not set the default encoding is "ISO-8859-1" as mandated by the Servlet API spec. This default encoding is used if the "charset" request parameter is not set to another (supported) character encoding. Applications being sure to always use the same encoding (e.g. UTF-8) can set this default here and may omit the "charset" request parameter.



为了避免在每个环境的Felix控制台中手动执行此操作, you can add a sling:OsgiConfig node with these settings in your codebase,然后在部署代码时配置此设置。

关于jsp - 在JSP中无法使用UTF-8进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26865281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com