Results 1 to 2 of 2
  1. #1
    zohaib82 is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default how to get utf-8 data from query string in JSP and add in database.

    Hi,

    I want to get utf-8 data from query string in jsp page therefore I have written following 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 and add in databse.

    Any solution?
    Java Code:
    <%@ 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>

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: how to get utf-8 data from query string in JSP and add in database.

    What do you see on the page when you run this?
    What should you see?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Formatting String data from database
    By computerbum in forum JDBC
    Replies: 1
    Last Post: 04-28-2010, 01:27 PM
  2. Replies: 3
    Last Post: 02-28-2009, 09:17 AM
  3. Query in database
    By anki1234 in forum JDBC
    Replies: 3
    Last Post: 08-20-2008, 05:45 PM
  4. [SOLVED] General string problem in my database query.....?
    By prabhurangan in forum New To Java
    Replies: 7
    Last Post: 06-25-2008, 08:55 AM
  5. How to query data from database using SSL
    By mano in forum New To Java
    Replies: 0
    Last Post: 08-02-2007, 05:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •