Jump to content

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


zohab

Recommended Posts

Hi,

 

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 garbage 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"> </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>

 

 

I have updated my server.xml but still I am getting garbage values from get method also i have updated web.xml file

 

<Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" /> 
<!-- Define an AJP 1.3 Connector on port 8009 --> 

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8" /> –

 

- Thanks

Zohaib.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.