[prev in list] [next in list] [prev in thread] [next in thread] 

List:       tomcat-user
Subject:    Re: HttpServletResponse return plain text
From:       Konstantin Kolinko <knst.kolinko () gmail ! com>
Date:       2013-03-28 5:38:40
Message-ID: CABzHfVnQRNUcGaBbjGEvdcj6MCQCaVL5L5to+Pyvem9fwWx3gw () mail ! gmail ! com
[Download RAW message or body]

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

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic