I'm building a web app for my lesson using java servlets. At some point i want to redirect to a jsp page, sending also some info that want to use there (using the GET method). In my servlet i have the following code:
String link = new String("index.jsp?name="+metadata.getName()+"&title="+metadata.getTitle());
response.sendRedirect(response.encodeRedirectURL(link));
In the jsp, I get these parameters using
<%
request.getParameter("name");
request.getParameter("title");
%>
Everything works fine, except when the parameters do not contain only latin characters (in my case they can contain greek characters). For example if name=ΕΡΕΥΝΑΣ i get name=¡¥. How can i fix this encoding problem (setting it to UTF-8)? Isn't encodeRedirectURL() doing this job? Should I also use encodeURL() at some point? I tried the last one but problem still existed.
Thanks in advance :)
The
HttpServletResponse#encodeRedirectURL()
does not URL-encode the URL. It only appends thejsessionid
attribute to the URL whenever there's a session and the client has cookies disabled. Admittedly, it's a confusing method name.You need to encode the request parameters with help of
URLEncoder#encode()
yourself during composing the URL.And create a filter which is mapped on
/*
and does basically the following indoFilter()
method:And add the following to top of your JSP:
Finally you'll be able to display them as follows:
See also: