Zohaib Khan
Zohaib Khan

Reputation: 21

how to get utf-8 data from query string in JSP

I want to get utf-8 data from query string in jsp page therefore I have written following simple JSP page

But I am getting garbaage instead of utf-8 data.

I want to know how to get utf-8 data from query string in JSP

<%@ 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">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2" align="center" class="accountInfo">&nbsp;</td>
  </tr>

  <tr>
    <td colspan="2" align="center" class="accountInfo">&nbsp;</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>&nbsp;</td>
    <td align="left"><label>
      <input type="submit" name="Submit" value="Submit" />
    </label></td>
  </tr>
</table>

</form>


</body>
</html>

Upvotes: 0

Views: 998

Answers (1)

user684934
user684934

Reputation:

You're probably getting ISO-8859-1 encoded data, because your form's method is get, and your WAS (Tomcat probably?) is decoding URI as such (it is the default).

If you are using Tomcat, change the server.xml's Connector element for your HTTP port to include the attribute URIEncoding="UTF-8".

If your form is supposed to change the state of the server (such as registering a contact in the DB), you could just change the method to post, which defaults to the encoding of the requesting page (which is utf-8).

Upvotes: 1

Related Questions