java - Is setCharacterEncoding() needed if charset is declared in content type? -
i confusing how work encoding. actually, read books , says should setcharacterencoding
before firstly getparameter()
. while according https://docs.oracle.com/javaee/6/api/javax/servlet/servletrequest.html#setcharacterencoding%28java.lang.string%29
overrides name of character encoding used in body of request. method must called prior reading request parameters or reading input using getreader(). otherwise, has no effect.
so, think since container must know charset in http body, seems not need setcharacterencoding()
if charset declared in http body.
i tried existing project, submit request xml outputstream, use
conn.setrequestproperty("content-type", "text/xml;charset=utf-8"); printwriter pw = new printwriter(new outputstreamwriter(conn.getoutputstream(), "utf-8"));
to encode stream , , when submit request servlet, even though not call
request.setcharacterencoding("utf-8");` can see chinese text decoded well.
so seems works , when remove charset=utf-8
content-type , works, more confusing .
here how read xml stream map.
public static map<string, string> parsexml(httpservletrequest request) throws ioexception, documentexception { map<string,string> map=new hashmap<string,string>(); inputstream =request.getinputstream(); saxreader reader=new saxreader(); document document=reader.read(is); element root =document.getrootelement(); list<element> elementlist=root.elements(); for(element e: elementlist){ map.put(e.getname(), e.gettext()); } return map; }
can please explain little bit how encoding works when servlet handles post request.and time need explicitly setcharacterencoding()?
ps: run client project , server project in eclipse.
Comments
Post a Comment