Multipart form in JSP, encoding problems in Glassfish

怎甘沉沦 提交于 2019-12-10 20:03:56

问题


I'm getting invalid character from my jsp/servlet using Eclipse and Glassfish.

If I enter "Pêche" I get "Pêches". So, this is encoding problem. I tried several thinks and nothing works.

  • I tried to add a filter (Encoding problems in JSP)
  • I tried to add jsp properties in web.xml (Unable to change charset from ISO-8859-1 to UTF-8 in glassfish 3.1)
  • I tried to change the character encoding my self in java code by request.setCharacterEncoding("UTF-8");
  • I tried to add VM arguments (Unable to change charset from ISO-8859-1 to UTF-8 in glassfish 3.1) but I cannot because of this (Eclipse - No server found in Run Configurations)
  • I added this " accept="UTF-8" accept-charset="UTF-8" " to my
  • I added <parameter-encoding default-charset="UTF-8"/> in sun-web.xml and glassfish-web.xml

I still get Mojibake.

Here is my servlet code:

String name = (String) request.getParameter("templateName");

Here is my jsp content:

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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>....</title>
</head>
<body>
     <form action="<c:url value="/form/edit" />" method="post" enctype="multipart/form-data">

                <input type="text" id="templateName" name="templateName"  />
                <br />

                <input type="submit" value="Valider" class="button button_blue margin_button_form"/>
        </form>
</body>
</html>

Any other suggestion?


回答1:


At the end, it seems to be a Glassfish bug: https://java.net/jira/browse/GLASSFISH-18516

Solved awfully with this: new String (s.getBytes ("iso-8859-1"), "UTF-8"); (https://stackoverflow.com/a/549634/1458542)




回答2:


I just faced the same issue. I guess you can temporarily set this in glassfish-web.xml:

<parameter-encoding default-charset="UTF-8"></parameter-encoding>

This works for me.




回答3:


Try to add this:

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

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>

And in your Servlet add this:

request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");



回答4:


http://www.devsniper.com/glassfish-tips-default-encoding/

Furthermore test POST and GET forms as they are handled differently. The accept attribute is informative to the browser too. (Not getting HTML entities for instance.)



来源:https://stackoverflow.com/questions/16540470/multipart-form-in-jsp-encoding-problems-in-glassfish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!