You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Kevin Jenkins <ra...@jenkinssoftware.com> on 2013/03/28 05:33:44 UTC

HttpServletResponse return plain text

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?

Re: HttpServletResponse return plain text

Posted by Konstantin Kolinko <kn...@gmail.com>.
2013/3/28 Kevin Jenkins <ra...@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