1

I am trying to get some user input value from a text box with special characters. But when I get it string is getting discarded with special character.

Like If i m passing input: poly&&(mer@) Below code is giving this string as: poly

HttpServletRequest request = ServletActionContext.getRequest();
        request.setCharacterEncoding("UTF-8");

        String text = (new String(request.getParameter("searchBarField")
                .getBytes("UTF-8")));

In my jsp character encoding tag is there. Please suggest.

Tanu Garg
  • 3,007
  • 4
  • 21
  • 29
  • 1
    Hope this link answer helps you !.. http://stackoverflow.com/questions/138948/how-to-get-utf-8-working-in-java-webapps – Velu Jul 16 '13 at 09:14

3 Answers3

3

I have worked your problem as follows and it works perfectly..

my JSP is index.jsp

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
        pageEncoding="ISO-8859-1" %>
    <!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=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <form action="GetParameter">
    Name:<input type="text" name="name">
    <br>
    <input type="submit" value="submit" />
    </form>
    </body>
    </html>

And following my servlet to handle the request.

public class GetParameter extends HttpServlet {
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        String name = request.getParameter("name");
        PrintWriter out = response.getWriter();
        out.write("Name is " + name);

    }

Hope it works with your code....

emphywork
  • 448
  • 1
  • 4
  • 15
  • I got the answer. actually problem was with my jsp. I was calling action by location.href. When i used post it is working fine. thanks guys for your suggestions. – Tanu Garg Jul 16 '13 at 12:12
  • Thanks @krupa. my problem is solved but not with the solution given by you. – Tanu Garg Jul 17 '13 at 06:21
  • I had the same problem and for me worked this solution: i replaced in my jsp pages "ISO-8859-1" with "UTF-8". My jsp page declaration looks like this: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> – Muno May 09 '17 at 18:31
2

It seems your application server is not configured to support Unicode characters. If you are using Tomcat Container . Set the URIEncoding to UTF-8 in your connectors.

<Server port="8105" shutdown="SHUTDOWN">

 <Service name="Catalina">
    <Connector port="8180" URIEncoding="UTF-8" />
    <Engine name="Catalina" defaultHost="localhost">
        <Host name="localhost" appBase="webapps" />
    </Engine>
 </Service>
</Server>

Set request.setCharacterEncoding("UTF-8") and get the parameter as :

String text=request.getParameter("searchBarField");

Another way will be to write a Filter which handles the character encoding for your web app. Also look into this SO Q&A .

Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
0

When you send an HTTP request to your server, make sure your URL is encoded first (e.g.: encodeURIComponent() in Javascript):

    $.ajax({
    type: "GET",
    url: "/Server1/endpoint?serialnumber=" + encodeURIComponent(x),
    success: function(data, status){

    },
    error: function (jqXHR, exception) {

    }
});

}

And on the server side, it should already be decoded:

String serialnumber = request.getParameter("serialnumber");
sklimkovitch
  • 251
  • 4
  • 8