JSP页面中的UTF-8编码

我有一个JSP页面,其页面编码是ISO-8859-1 。 这个JSP页面有一个问题答案博客。 我希望在Q / A发布期间包含特殊字符。

问题是JSP不支持UTF-8编码,即使我已经将它从ISO-8859-1更改为UTF-8 。 这些字符( ~%&+ )正在产生问题。 当我单独或使用任何字符的组合发布这些字符时,它在数据库中是storinh null ,当我在发布应用程序时删除这些字符时它工作正常。

任何人都能提出一些解决方案吗

您应该在应用程序的所有层上使用相同的编码以避免此问题。 添加filter来设置编码很有用:

 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException { request.setCharacterEncoding("UTF-8"); chain.doFilter(request, response); } 

要仅在JSP页面上设置编码,请将以下行添加到它们:

 <%@ page contentType="text/html; charset=UTF-8" %> 

将数据库配置为使用相同的char编码。

如果需要转换字符串的编码,请参阅:

  • 在java中编码转换

我不建议在您的数据库中存储HTML编码的文本。 例如,如果您需要生成PDF(或HTML以外的任何内容),则需要先转换HTML编码。

完整的JSP标记应该是这样的,请注意pageEncoding:

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 

一些旧的浏览器也搞乱了编码。 您可以使用HTML标记

   

此外,文件应以UTF-8格式记录,如果您使用Eclipse左键单击文件 – >属性 – >签出 – >文本文件编码。

我也有一个问题,显示像“ṀŮ”这样的charectors。我在web.xml中添加了以下内容。

   *.jsp UTF-8   

这解决了除标题之外的页面中的问题。 试过很多方法来解决这个问题,在我的案例中没有任何效果。 标题的问题是标题jsp页面包含在另一个jsp中。 所以给编码导入,这解决了我的问题。

  

谢谢

您必须确保使用UTF-8编码保存文件。 您可以使用多个纯文本编辑器来完成此操作。 使用Notepad ++,即可以在菜单中选择Encoding – > Encode in UTF-8 。 即使使用Windows的记事本( Save As – >编码UTF-8),您也可以执行此操作。 如果您使用的是Eclipse,则可以在文件的Properties中进行设置。

另外,检查问题是否必须转义这些字符。 作为你的问题并不奇怪,因为其中一个角色是&

该线程可以帮助您:将请求参数作为UTF-8编码的字符串传递

基本上:

 request.setCharacterEncoding("UTF-8"); String login = request.getParameter("login"); String password = request.getParameter("password"); 

或者你在jsp文件上使用javascript:

 var userInput = $("#myInput").val(); var encodedUserInput = encodeURIComponent(userInput); $("#hiddenImput").val(encodedUserInput); 

上课后恢复:

 String parameter = URLDecoder.decode(request.getParameter("hiddenImput"), "UTF-8"); 

我使用编码filter解决了我的所有编码问题……

  package com.dina.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; /** * * @author DINANATH */ public class EncodingFilter implements Filter { private String encoding = "utf-8"; public void doFilter(ServletRequest request,ServletResponse response, FilterChain filterChain) throws IOException, ServletException { request.setCharacterEncoding(encoding); // response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding(encoding); filterChain.doFilter(request, response); } public void init(FilterConfig filterConfig) throws ServletException { String encodingParam = filterConfig.getInitParameter("encoding"); if (encodingParam != null) { encoding = encodingParam; } } public void destroy() { // nothing todo } } 

在web.xml中

   EncodingFilter  com.dina.filter.EncodingFilter   encoding UTF-8   forceEncoding true    EncodingFilter /*  

这是一个常见问题。

最简单的解决方法之一是检查特殊字符是否到达操作层内部,然后修改java代码中的特殊字符。

如果您能够在Action或您选择的任何其他Java层(如业务层)中查看此字符,只需使用StringEscapeUtils.html#escapeHtml将字符替换为相应的HTML字符

做完逃跑之后。 使用新字符串保存到DB。

这是html中的特殊字符。 为什么不编码呢? 请查看: http : //www.degraeve.com/reference/specialcharacters.php

这对你有所帮助。

 <%@page contentType="text/html" pageEncoding="UTF-8"%>    

我在JSP上使用特殊字符作为分隔符时遇到了同样的问题。 当特殊字符被发布到servlet时,它们都搞砸了。 我通过使用以下转换解决了这个问题:

 String str = new String (request.getParameter("string").getBytes ("iso-8859-1"), "UTF-8"); 

我添加这个shell脚本来转换IS的jsp文件

 #!/bin/sh ############################################### ## this script file must be placed in the parent ## folder of the to folders "in" and "out" ## in contain the input jsp files ## out will containt the generated jsp files ## ############################################### find in/ -name *.jsp | while read line; do outpath=`echo $line | sed -e 's/in/out/'` ; parentdir=`echo $outpath | sed -e 's/[^\/]*\.jsp$//'` ; mkdir -p $parentdir echo $outpath ; iconv -t UTF-8 -f ISO-8859-1 -o $outpath $line ; done 

感谢所有的提示。 使用Tomcat8我还添加了像@Jasper de Vries这样的filter。 但是在现在较新的Tomcats中,已经实现了一个filter,可以在Tomcat web.xml中使用resp只是取消注释:

  setCharacterEncodingFilter org.apache.catalina.filters.SetCharacterEncodingFilter  encoding UTF-8  true  ...  setCharacterEncodingFilter /*  

和所有其他人一样; 我将URIEncoding="UTF-8"到Apache中的Tomcat连接器。 这也有帮助。

重要的是,Eclipse(如果你使用它)有一个web.xml的副本并覆盖Tomcat-Settings,如下所述: 在JSP中破坏的UTF-8 URI编码