Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Set HTTP status from JSP  RSS feed

 
Don Blodgett
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Struts 2 and I need to be able to set the HTTP status to 401 (Unauthorized) or 200 (Success) based on the result of an action. I am trying to find a way to do this from a JSP, as I also need to have a body along with both responses. I have looked at HTTPHeaderResult, but I don't think that will work as I would be unable to use a JSP to create the body of the response. Obviously this is only for the 401 status as the 200 status can just use the dispatcher result type.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using struts then you're already working in an MVC environment so I'm not sure why you want to do this from within your JSP.

That being said, take a look at the HttpServletResponse object.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletResponse.html

It has a sendError method along with constants for all the commonly used HTTP response codes.

JSP provides an implicit variable named response that points to your Response object.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From a JSP?
 
Don Blodgett
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben, thanks... I feel very stupid at the moment! This is exactly what I need to do.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's still a pretty poor way to do it though--please consider doing it from the Java code instead; there are a variety of reasons to do so.

Since you're using Struts 2, why not just make a result type subclass that also allows setting the response code via configuration? (I keep meaning to check this capability into the core since I've had it locally for ever :(
 
Don Blodgett
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should have been more clear on that, I am doing it from the Java, I am using the ServletResponseAware to get the response object and set the status from there. But creating a result type that allows me to do the same would be a great idea also.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, okay :)

Yeah, if someone else hasn't already added it I'll try to throw that in soon :/
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!