-1

Can anyone please help me with this: is there any other way that we can encode the special character and other characters in UTF-8 in java jsp page, without changing tomcat configuration.

I have used URLEncoder.encode() to encode the url in my jsp file and also in my application, but i don't want to make changes in tomcat configuration as my server is continuously running and i can't stop it, so is there any other way we can encode the url without changing the tomcat configuration...

SkyWalker
  • 28,384
  • 14
  • 74
  • 132
Akshay
  • 91
  • 1
  • 9

2 Answers2

0

use java.net.URI instead HTTP URL Address Encoding in Java OR use URIBuilder How do I encode URI parameter values? hope this helps you.

Community
  • 1
  • 1
Mohammadreza Khatami
  • 1,444
  • 2
  • 13
  • 27
0

You have missed to add UTF-8 in encoding.

Use it

 URLEncoder.encode(stringToBeEncoded, "UTF-8"); 

instead of

URLEncoder.encode();

In your jsp, use page charset to UTF-8.

<%@page contentType="text/html;charset=UTF-8"%>

In your servlet, you can use it

request.setCharacterEncoding("UTF8");
response.setCharacterEncoding("UTF8");

Resource Link:

How to get UTF-8 working in Java webapps?


UPDATE:

Use filter from this link:

http://tomcat.apache.org/tomcat-8.0-doc/config/filter.html

How to use UTF-8 everywhere?

Using UTF-8 as your character encoding for everything is a safe bet. This should work for pretty much every situation.

In order to completely switch to using UTF-8, you need to make the following changes:

  1. Set URIEncoding="UTF-8" on your <Connector> in server.xml. References: HTTP Connector, AJP Connector.

  2. Use a character encoding filter with the default encoding set to UTF-8

  3. Change all your JSPs to include charset name in their contentType. For example, use <%@page contentType="text/html; charset=UTF-8" %> for the usual JSP pages and <jsp:directive.page contentType="text/html; charset=UTF-8" /> for the pages in XML syntax (aka JSP Documents).

  4. Change all your servlets to set the content type for responses and to include charset name in the content type to be UTF-8. Use response.setContentType("text/html; charset=UTF-8") or response.setCharacterEncoding("UTF-8").

  5. Change any content-generation libraries you use (Velocity, Freemarker, etc.) to use UTF-8 and to specify UTF-8 in the content type of the responses that they generate.

  6. Disable any valves or filters that may read request parameters before your character encoding filter or jsp page has a chance to set the encoding to UTF-8. For more information see http://www.mail-archive.com/users@tomcat.apache.org/msg21117.html.

Community
  • 1
  • 1
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
  • Thanks for reply, I have already made changes as URLEncoder.encode(StringToBeEncoded,"UTF-8"); but i don't want to make change in tomcat server.xml Connector as URIEncoding="UTF-8",please suggest me other solution... – Akshay Apr 23 '16 at 11:47
  • @Akshay Would you please explain why you not want to change tomcat config? – SkyWalker Apr 23 '16 at 11:55
  • @Akshay in your jsp, use <%@page contentType="text/html;charset=UTF-8"%> – SkyWalker Apr 23 '16 at 11:57
  • used it already, and i don't want to use it in tomcat because the server is continuously running and , i can't stop it ,to make the changes... – Akshay Apr 23 '16 at 12:09
  • is there any problem to stop the tomcat server? is it in production? @Akshay – SkyWalker Apr 23 '16 at 12:19
  • on that case, you can use a filter in your project web.xml – SkyWalker Apr 23 '16 at 12:29
  • @Akshay I have updated the answer given filter link and best use of getting UTF-8 procedure. Hope it will help you. – SkyWalker Apr 23 '16 at 12:35
  • i have updated this also in my project web.xml , however it is encoding with special characters like "+ ,space, * " etc., but it is not encoding suppose the german character like "Ö,ß,ä" etc...any solution for this... – Akshay Apr 23 '16 at 12:38
  • Would you please post your web.xml? @Akshay – SkyWalker Apr 23 '16 at 12:49
  • setCharacterEncodingFilter org.apache.catalina.filters.SetCharacterEncodingFilter encoding UTF-8 true setCharacterEncodingFilter /* – Akshay Apr 23 '16 at 12:51
  • if you use specific German, then i got a link which states to use `<%@page contentType="text/html; charset=ISO-8859-1" %>` in jsp. Please have a try http://allinoneissues.blogspot.jp/2012/05/german-characters-encoding-issue.html – SkyWalker Apr 23 '16 at 12:57
  • not specific with german characters, want to encode all the character including the german and the rest... – Akshay Apr 23 '16 at 13:02
  • You just change in your specific jsp page `<%@page contentType="text/html; charset=ISO-8859-1" %>` – SkyWalker Apr 23 '16 at 13:03
  • @Akshay Have you changed your servlet for request and response as `request.setCharacterEncoding("UTF8"); response.setCharacterEncoding("UTF8");` – SkyWalker Apr 23 '16 at 13:28