1 Reply Latest reply: Aug 2, 2012 4:57 AM by r035198x RSS

    UTF-8 data in JSP and Ajax.

    774597
      Hi,

      I am not able to process utf-8 data in JSP.

      I am getting garbage data and want to know how to solve this problem?


      -------------------------------------------------

      <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>Utf-8 JSP Example</title>
      <script language="javascript" type="text/javascript">

      var xmlhttp

      function loadContent(str)
      {
           xmlhttp=GetXmlHttpObject();

           if (xmlhttp==null)
           {
                alert ("Your browser does not support Ajax HTTP");
                return;
           }
           
           var firstname = document.getElementById("firstname").value;
           var lastname = document.getElementById("lastname").value;
           var email = document.getElementById("email").value;
           var password1 = document.getElementById("password1").value;
           
           var url="Server.jsp";
           url=url+"?firstname="+(firstname)+"&lastname="+(lastname)+"&email="+(email)+"&password1="+(password1);
           // url=url+"?firstname="+encodeURIComponent(firstname)+"&lastname="+encodeURIComponent(lastname)+"&email="+encodeURIComponent(email)+"&password1="+encodeURIComponent(password1);
           
           alert(url);     
           
           xmlhttp.onreadystatechange=getOutput;
           xmlhttp.open("POST",url,true);
           //xmlhttp.open("GET",url,true);
           xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
           xmlhttp.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
           xmlhttp.send(null);
      }

      function getOutput()
      {
           if (xmlhttp.readyState==4)
           {
                //alert(xmlhttp.responseText);
                
                document.getElementById("prtCnt").innerHTML=xmlhttp.responseText;
           }
      }

      function GetXmlHttpObject()
      {
           if (window.XMLHttpRequest)
           {
           return new XMLHttpRequest();
           }
           if (window.ActiveXObject)
           {
           return new ActiveXObject("Microsoft.XMLHTTP");
           }
           return null;
      }

      </script>
      </head>

      <body>
      <form id="frmForm" name="frmForm" method="post" action="">
      <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="button" name="Submit" value="Submit" onclick="loadContent()" />
      </label></td>
      </tr>
      </table>
      </form>


      <div id="prtCnt"></div>


      </body>
      </html>

      -----------------------------------------------

      <%@ page language="java" contentType="text/html; charset=UTF-8"
      pageEncoding="UTF-8"%>
      <%
      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);

      %>

      ----------------------------------------

      - Thanks
      Zohaib.

      Edited by: user12259914 on Aug 1, 2012 3:04 AM

      Edited by: user12259914 on Aug 1, 2012 3:06 AM