我正在尝试使用JSP(JSTL)将俄语字符写入MySQL 5.5。 虽然它可以在我自己的localhost上运行,但我无法让它在我租用的服务器上运行。
它正在从表单向另一页发送正确的参数,然后正确显示输出。这也适用于服务器。 在DB上写入/读取只能在我自己的本地主机上工作。
如果我尝试在服务器上写入DB,俄语字符会变成问号。但是,我可以使用phpMyAdmin存储俄语字符,然后在我的网页上正确显示。
这是我的查询以及到目前为止我尝试过的内容: 查询:
<sql:update dataSource="${ds}" var="updatedTable">
INSERT INTO Question (question) VALUES (?)
<sql:param value="${param.questionAsked}" />
</sql:update>
DBconn:
<sql:setDataSource var="ds" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/myDBname?useUnicode=true&characterEncoding=utf-8" user="secret" password="secret" />
每个JSP文件中的页面编码:
<%@page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
将其添加到tomcat server.xml:
<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true"
URIEncoding="UTF-8"
/>
取消注释web.xml中的CharacterEncodingFilter:
<filter>
<filter-name>setCharacterEncodingFilter</filter-name>
<filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>setCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
使用utf8更改/创建新的DB和DB表以进行测试:
CREATE TABLE `Question` (
`questionID` int(6) unsigned NOT NULL AUTO_INCREMENT,
`question` varchar(255) DEFAULT NULL,
`categoryID` int(6) unsigned DEFAULT NULL,
PRIMARY KEY (`questionID`),
KEY `categoryID` (`categoryID`),
CONSTRAINT `question_ibfk_1` FOREIGN KEY (`categoryID`) REFERENCES `Category` (`categoryID`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8;
我想我的问题出在MySQL连接器的某个地方。我无法弄清楚如何更改它,因为我无法访问mysql的配置文件。我请求SSH访问,如果这有助于我。我对这些问题相当新,因为我没有长时间处理数据库,特别是服务器。
phpMyAdmin显示:
character set client - latin1, (session value - utf8)
character set connection - latin1, (session value - utf8)
character set database - latin1
character set results - latin1, (session value - utf8)
character set server - latin1
character set system - utf8
collation connection - latin1_swedish_ci (session value - utf8_unicode_ci)
collation database - latin1_swedish_ci
collation server - latin1_swedish_ci
我希望你能建议做什么......
答案 0 :(得分:0)
我自己想通了。 只是取消注释CharacterEncoding过滤器是不够的。 我也必须在我的webapp中实现一个过滤器。 下面是代码:
package filters;
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;
public class CharsetFilter implements Filter {
private String encoding;
public void init(FilterConfig config) throws ServletException {
encoding = config.getInitParameter("requestEncoding");
if (encoding == null)
encoding = "UTF-8";
}
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);
}
public void destroy() {
}
}
不要忘记它在web.xml中:
<filter>
<filter-name>CharsetFilter</filter-name>
<filter-class>filters.CharsetFilter</filter-class>
<init-param>
<param-name>requestEncoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>