Java servlet sendRequest - getParameter encoding Problem -


i'm building web app lesson using java servlets. @ point want redirect jsp page, sending info want use there (using method). in servlet have following code:

string link = new string("index.jsp?name="+metadata.getname()+"&title="+metadata.gettitle());  response.sendredirect(response.encoderedirecturl(link)); 

in jsp, these parameters using

<% request.getparameter("name"); request.getparameter("title"); %> 

everything works fine, except when parameters not contain latin characters (in case can contain greek characters). example if name=ΕΡΕΥΝΑΣ name=¡¥. how can fix encoding problem (setting utf-8)? isn't encoderedirecturl() doing job? should use encodeurl() @ point? tried last 1 problem still existed.

thanks in advance :)

the httpservletresponse#encoderedirecturl() not url-encode url. appends jsessionid attribute url whenever there's session , client has cookies disabled. admittedly, it's confusing method name.

you need encode request parameters of urlencoder#encode() during composing url.

string charset = "utf-8"; string link = string.format("index.jsp?name=%s&title=%s",      urlencoder.encode(metadata.getname(), charset),      urlencoder.encode(metadata.gettitle(), charset));  response.sendredirect(response.encoderedirecturl(link)); 

and create filter mapped on /* , following in dofilter() method:

request.setcharacterencoding("utf-8"); chain.dofilter(request, response); 

and add following top of jsp:

<%@ page pageencoding="utf-8" %> 

finally you'll able display them follows:

<p>name: ${param.name}</p> <p>title: ${param.title}</p> 

see also:


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -