vaibhavaj2
vaibhavaj2

Reputation: 105

Trying to send back error json from Servlet filter instead of allowing the request to go through

I have implemented a Filter for a servlet(java). But there is a condition that in some cases i should not be allowing the request to go through( chain.doFilter() ) and allow access to Servlet.

Instead i need to send Error json object back to browser form filter itself. Please let me know how this can be implemented.

Upvotes: 0

Views: 695

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280112

Each Filter has access to the ServletResponse. Simply get its OutputStream and use it to write the JSON content. (And don't follow the chain.)

Upvotes: 2

Related Questions