我想从jsp页面的查询字符串中获取utf-8数据,因此我编写了以下简单的JSP页面
但我得到的是garbaage而不是utf-8数据。
我想知道如何从JSP
中的查询字符串中获取utf-8数据<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.util.Random.*"%>
<%
request.setCharacterEncoding("UTF-8");
String strFirstName=request.getParameter("firstname");
String strLastName=request.getParameter("lastname");
String strEmail=request.getParameter("email");
String strPassword=request.getParameter("password1");
out.println("<br/>First Name => "+strFirstName);
out.println("<br/>Last Name => "+strLastName);
out.println("<br/>Email => "+strEmail);
out.println("<br/>Password => "+strPassword);
Connection conn;
conn=null;
int rs;
int Flag=0;
String Query=null;
PreparedStatement stat = null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:xe","system","mypassword");
Random randomGenerator = new Random();
int ID = randomGenerator.nextInt();
String sql =" INSERT INTO CONTACTS (id,firstname,lastname,email,password1) VALUES (?,?,?,?,?) ";
stat = conn.prepareStatement(sql);
stat.setInt(1,ID);
stat.setString(2,strFirstName);
stat.setString(3,strLastName);
stat.setString(4,strEmail);
stat.setString(5,strPassword);
rs=stat.executeUpdate();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>New Contact</title>
<META http-equiv="Content-Type" content="text/html;charset=UTF-8">
<link href="Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="frmForm" name="frmForm" method="get" action="form.jsp">
<table width="500" align="left" border="0">
<tr>
<td colspan="2" align="center" class="accountInfo"><div align="left"><h2>New Contact Information</h2> </div></td>
</tr>
<tr>
<td colspan="2" align="center" class="accountInfo"> </td>
</tr>
<tr>
<td colspan="2" align="center" class="accountInfo"> </td>
</tr>
<tr>
<td colspan="2" align="center" class="accountInfo"> </td>
</tr>
<tr>
<td width="200" align="center"><div align="left"><strong>First Name </strong><span class="style3">* </span></div></td>
<td width="300" align="left"><label>
<input type="text" width="300px" name="firstname" id="firstname" />
</label></td>
</tr>
<tr>
<td width="200" align="center"><div align="left"><strong>Last Name</strong><span class="style3"> *</span> </div></td>
<td width="300" align="left"><input width="300px" type="text" name="lastname" id="lastname"/></td>
</tr>
<tr>
<td width="200" align="center"><div align="left"><strong>Email</strong></div></td>
<td width="300" align="left"><input width="300px" type="text" name="email" id="email"/></td>
</tr>
<tr>
<td width="200" align="center"><div align="left"><strong>Password</strong></div></td>
<td width="300" align="left"><input width="300px" type="password" name="password1" id="password1"/></td>
</tr>
<tr>
<td> </td>
<td align="left"><label>
<input type="submit" name="Submit" value="Submit" />
</label></td>
</tr>
</table>
</form>
</body>
</html>
答案 0 :(得分:1)
您可能正在获取ISO-8859-1编码数据,因为您的表单method
是get
,而您的WAS(可能是Tomcat?)正在解码URI(这是默认值) 。
如果您使用的是Tomcat,请更改HTTP端口的server.xml的Connector
元素以包含属性URIEncoding="UTF-8"
。
如果您的表单应该更改服务器的状态(例如在数据库中注册联系人),您可以将方法更改为post
,默认为请求页面的编码(是utf-8)。