Java servlet sendRequest - getParameter encoding Problem

I am building a web application for my lesson using Java servlets. At some point, I want to redirect to a jsp page, sending also some information that I 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 jsp, I get these options using

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

Everything works fine, except when the parameters do not contain only Latin characters (in my case, they may contain Greek characters). For example, if name = ΕΡΕΥΝΑΣ I get name = ¡¥. How can I fix this encoding problem (install it in UTF-8)? Doesn't this work encodeRedirectURL ()? Should I also use encodeURL () at some point? I tried the latter, but the problem still existed.

Thanks in advance:)

+3
4

HttpServletResponse#encodeRedirectURL() URL- URL-. jsessionid URL- , cookie. , .

URLEncoder#encode() 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));

filter, /* doFilter():

request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);

JSP:

<%@ page pageEncoding="UTF-8" %>

, :

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

. :

+7

java.net.URLEncoder URL-. : "&", , ?

+1

URLEncoder.encode() , .

encodeRedirectURL URL-, ( URL-, cookie)

0

?

  • ;
  • JSP ;
  • Use the query manager to forward the request to the JSP;
  • Access to these attributes in a request from the JSP.

This allows you to redirect the browser from the first servlet to the processed JSP servlet and completely eliminate the whole problem of parameter encoding.

Also make sure the JSP page directive sets the encoding of the content to UTF-8.

0
source

Source: https://habr.com/ru/post/1794247/


All Articles