Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

handle doGet exception  RSS feed

 
Timber Lee
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following code of doGet() method of a servlet:
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
String userId = loginUser(req); //this method takes the credential from the request and logs in the user.
if(userId == null)
{
// 1 Should send SC_FORBIDDEN
}
else
{
PrintWriter out = response.getWriter();
generateAndPublishData(out); //this method writes appropriate date to out.
}
}
The data should be sent to the client only if loginUser() returns a non null userid. Otherwise a status of SC_FORBIDDEN should be sent.

setStatus and sendErroe can do that.
can ServletException(res.SC_FORBIDDEN) be thrown to do that?
how to use
 
Andre Cesta
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can answer that question yourself.
Just look into the constructors for
ServletException, and see if your result
code can be passed to these constructures.
Originally posted by Timber Lee:
Consider the following code of doGet() method of a servlet:
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
String userId = loginUser(req); //this method takes the credential from the request and logs in the user.
if(userId == null)
{
// 1 Should send SC_FORBIDDEN
}
else
{
PrintWriter out = response.getWriter();
generateAndPublishData(out); //this method writes appropriate date to out.
}
}
The data should be sent to the client only if loginUser() returns a non null userid. Otherwise a status of SC_FORBIDDEN should be sent.

setStatus and sendErroe can do that.
can ServletException(res.SC_FORBIDDEN) be thrown to do that?
how to use
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!