0

I have a textbox input field on my index.jsp inside form tag page. When I insert the character in my textbox, then after hitting submit button, it takes me to its corresponding servlet. From servlet I get the value of text box using request.getParameter(). And then show that value as response from servlet on page. But on servlet response it is showing me a junk value : †whereas I wanted the same character () as output.

files:

index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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"/>
        </head>    
<html>            
<body>
<form action="hello.do" method="post">
<input type="text" name="t1"/><br>
<input type="submit"/>
</form>
</body>
</html>

servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String s=request.getParameter("t1");
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        out.println(s);
        System.out.println("inside servlet: "+s);
    }
JPG
  • 1,247
  • 5
  • 31
  • 64

1 Answers1

-1

It looks like you are not getting properly encoded character in HTTPServletResponse.

So you can use method setCharacterEncoding("UTF-16LE") with servlet response which sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8. If the character encoding has already been set by setContentType(java.lang.String) or setLocale(java.util.Locale), this method overrides it.

Ex. response.setHeader("Content-Type", "text/xml; charset=UTF-16LE");

Sai prateek
  • 11,842
  • 9
  • 51
  • 66