1

This problem is little bit odd. All UTF-8 requirement of MYSQL and JSP are fully justified in my code. I have two simple files input.jsp (for taking input) and NewFile.jsp (for retrieving input from database). The database QASKU.production is already created and loaded with UTF8 data and is working fine. The problem is with retrieved data through select statement but not always. When I use this statement

ResultSet rs = stmt.executeQuery("select * from QASKU.production");

All the data is retrieved and displayed perfectly.

but when I use these statements:

ResultSet rs = stmt.executeQuery("SELECT * FROM QASKU.production WHERE rhs LIKE '" + sent + "' ORDER BY prob DESC");

or

String query = "select * from QASKU.production WHERE rhs = ?";
PreparedStatement pstmt = con.prepareStatement( query );
pstmt.setString( 1, sent );
ResultSet rs = pstmt.executeQuery( );

The data is retrieved and displayed perfectly but it depends on the input which I gave to this file NewFile.jsp from file input.jsp.

The data in the database is looking like this:

ADJ|اسسٹنٹ|0.001222

ADJ|اسلامی|0.01956

ADJP|ADJ ADJ|0.098214

ADJP |ADJ ADJ.DEG|0.044643

So, when I gave ADJ as input value, the output displayed via NewFile.jsp is perfect.

Now, when I gave, for example, "اسسٹنٹ" as input value, the select statement did not fetch any result set from the database and it will remain empty which is a problem even the record for "اسسٹنٹ" exists in the database.

I don't think this is a problem with MySQL or JSP. I think the problem lies within the select statement, but I'm not sure.

My code file are here as below:

input.JSP

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
    <title>QASKU URDU PARSER</title>

    <script type="text/javascript" >
    var ids = [];
    var blurfocus = function(id){
          document.getElementById(id).onfocus = function(){
        if(!ids[id]){ ids[id] = { id : id, val : this.value, active : false }; }
        if(this.value == ids[id].val){
          this.value = "";
        }
          };
          document.getElementById(id).onblur = function(){
        if(this.value == ""){
          this.value = ids[id].val;
        }
      }
    }

    function checkSubmit(e)
    {
       if(e && e.keyCode == 13)
       {
          document.forms[0].submit();
       }
    }
    </script>

    </head>
    <body>
    <form name="myform" action="NewFile.jsp" method="post" enctype="application/x-www-form-    urlencoded" >

       <div align="center" onKeyPress="return checkSubmit(event)">

    <h4>QASKU URDU PARSER</h4><br>
    <h5>Type sentence using Urdu/Arabic script only and then press the 'Parse' button below</h5><br>

    <textarea cols="100" rows="5" style="text-align: right" name="mytextarea" id="message" >Type here</textarea>
    <script type="text/javascript" >
    blurfocus("message");
    </script>

    <br><br>
    <input type="submit" value="Parse" >

    </div>

    </form>
    </body>
    </html>

and then the second file NewFile.jsp as below:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
try
{
String sent=request.getParameter("mytextarea");
 out.println(sent);

Statement stmt;
Connection con;
String url = "jdbc:mysql://localhost:3306/";
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url, "root", ""); 
//stmt = con.createStatement();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
//out.println(con.getMetaData().getDatabaseProductVersion());
//stmt.executeUpdate("DROP DATABASE QASKU");
//out.println("Deleted");
//stmt.executeUpdate("CREATE DATABASE QASKU CHARACTER SET utf8 COLLATE utf8_general_ci");
//stmt.executeUpdate("CREATE TABLE QASKU.production(lhs varchar(50) NOT NULL, rhs varchar(200) NOT NULL, prob double NOT NULL) CHARACTER SET utf8 COLLATE utf8_general_ci");
//stmt.executeUpdate("LOAD DATA LOCAL INFILE '/QAS/JSP/myfirst/WebContent/PCFG.utf' INTO TABLE QASKU.production CHARACTER SET utf8  LINES TERMINATED BY '\r' ");

//ResultSet rs = stmt.executeQuery("SELECT USER(),CHARSET(USER()),COLLATION(USER())");
//ResultSet rs = stmt.executeQuery("select * from QASKU.production");
ResultSet rs = stmt.executeQuery("SELECT * FROM QASKU.production WHERE rhs LIKE '" + sent + "' ORDER BY prob DESC");

//String query = "select * from QASKU.production WHERE rhs = ?";
//PreparedStatement pstmt = con.prepareStatement( query );
//pstmt.setString( 1, sent );
//ResultSet rs = pstmt.executeQuery( );

if(rs != null)
{
%>  
    <table align=center border="1" bgcolor="green" width="75%">
    <col width="25">
    <col width="25">
    <col width="25">
    <tr>
        <th align=left>LHS</th>
        <th align=left>RHS</th>
        <th align=left>PROBABILITIES</th>
    </tr>
<%

    while(rs.next())
    {
        out.println("<tr><td align=left>"+rs.getString(1)+"</td>");
        out.println("<td align=left>"+rs.getString(2)+"</td>");
        out.println("<td align=left>"+rs.getDouble(3)+"</td></tr>");    
    }
}
else
{
    out.println("Result Set is Emptry");
}

%>
    </table>
<%

con.close();
}
catch(Exception e)
{
out.println(e);
}
/*
try
    {
        BufferedReader reader = new BufferedReader(new FileReader("/QAS/JSP/myfirst/WebContent/PCFG.utf"));
        String text = "";
        while ((text = reader.readLine()) != null) 
            {
                out.println(text);
            }
    }
    catch(Exception e)
    {}
 */
%>

</body>
</html>
4

3 回答 3

0

I don't know Urdu, but you probably should add %% in your LIKE.

Something like this:

ResultSet rs = stmt.executeQuery("SELECT * FROM QASKU.production WHERE rhs LIKE '%" + sent + "%' ORDER BY prob DESC");
于 2013-03-16T01:27:31.427 回答
0

Finally, I solved my problem after 24 hours. The problem is related to some other statement as follows:

String sent=request.getParameter("mytextarea");

This statement is retrieving values from input.jsp page via post method. This statement is no doubt available in jsp by default but its origin is Java Servlets. By default it takes values from the page in ASCII depends upon the two defined methods 'get' and 'post'. So, here 'post' method was used in input.jsp, due to which the values retrieved has different format in servlets. you can read in some jsp tutorial. I solved this problem by embedding the two files input.jsp and newfile.jsp into one file, and then remove some information from the following line:

<form name="myform" action="NewFile.jsp" method="post" enctype="application/x-www-form-urlencoded" >

and transformed into this simple form:

<form name="myform" method="get" >

now the following statement is taking values directly from the same page and not sending data to servlets:

String sent=request.getParameter("mytextarea");

This is not a big solution, but at least it is now working perfectly for Urdu language means utf8 character. So, the final conclusion is the bug of retrieving ASCII values from MySQL and not retrieving utf8 values from MySQL database has a problem with this statement and not with others.

于 2013-03-16T22:24:09.363 回答
0

The perfect solution is to add following string URIEncoding="UTF-8" in of /conf/server.xml file located in Servers directory of your project in eclipse for tomcat. Then all the encoding/decoding will be automatically. Short but perfect solution

<Connector URIEncoding="UTF-8" ...........>

This is the best solution ever have and now pray for me.

于 2013-03-21T14:02:25.310 回答