tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Konstantin Kolinko <knst.koli...@gmail.com>
Subject Re: HttpServletResponse return plain text
Date Thu, 28 Mar 2013 05:38:40 GMT
2013/3/28 Kevin Jenkins <rakkar@jenkinssoftware.com>:
> I know this isn't the right mailing list but there's a lot
> of knowledgeable people here so I'd give it a shot.
>
> When I was hosting on Google App Engine the java servlet
> HttpServletResponse.sendError would return the 2nd parameter text in plain
> text.
>
> Now that I'm hosting on Tomcat it's putting the text inside an <HTML> body.
> I'd like to to return the text in plain text instead.
>
> This is my code:
> String resultStr = "Request " +  req.getRequestURI() + ": __developerId " +
> __developerId + " in use.";
> resp.getWriter().println(resultStr);
> resp.sendError(HttpServletResponse.SC_CONFLICT, resultStr);
>
> I tried this, but it still returned the text inside an HTML body.
>
> resp.setContentType("text/plain");
> String resultStr = "Request " +  req.getRequestURI() + ": __developerId " +
> __developerId + " in use.";
> resp.getWriter().println(resultStr);
> resp.sendError(HttpServletResponse.SC_CONFLICT);
>
> Anyone know how to do this?

sendError() clears response and triggers error page processing
You can specify your own page/servlet for this specific error code
with error-page element in your web.xml.

If you want to keep your message, just use "setStatus()" and return.
If you want to clear response before printing your message, call
ServletResponse.resetBuffer().

Beware that if you have user-provided data in your "text/plain"
response, I would recommend you to specify an explicit charset (e.g.
ISO-8859-1 in your case). If charset is not mentioned in Content-Type
header of an HTTP response, some browsers may start guessing and it
may lead to security issues.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message