简体   繁体   中英

Handling an exception in a servlet

I am working on an assignment that I have. It is fairly straight forward. An HTML form which contains a single input is submitted to a Servlet that grabs the parameter, creates a message based on the parameter, adds the message as an attribute to the request, and uses a requestdispatcher to forward to a jsp to display the message.

I have a requirement that if the parameter is missing, I need to display an error page. The catch is that I can't explicitly check for a null, or use a try/catch block. My guess is that the goal is to define an error page in the web.xml page to handle errors of a certain type, but the problem is, if I cant check to see if the request parameter is null, or use a try/catch, how do I know if I need to throw an exception? Any ideas?

In the web.xml, you can specify an error page, like this.
Let's assume you want to catch HTTP400, 500 and exceptions:

<error-page>
    <error-code>400</error-code>
    <location>/errorpage.html</location>
</error-page>
<error-page>
    <error-code>500</error-code>
    <location>/errorpage.html</location>
</error-page>

(as suggested by Arjit)

<error-page>  
   <exception-type>java.lang.Exception</exception-type>  
   <location>/errorpage.html</location>  
</error-page>

And then to put it all together, as suggested by DeveloperWJK, in the servlet:

public void doGet(HttpServletRequest request, HttpServletResponse  response) throws IOException, ServletException, NullPointerException
{
       String param = request.getParameter("param");
       if(param.equals("x"))
       {
          response.sendRedirect("x.jsp");
          return;
       }
}

If you intend to create a message based on the parameter then it is a bit difficult to see how you can achieve this if you cannot check the parameters value (for null for instance). Presumably you call...

HttpServletRequest.getParameter()

Which returns the parameter value or null if the parameter is missing.

In web.xml , you can mention exceptions also.

<error-page>  
  <exception-type>java.lang.Exception</exception-type>  
  <location>/error.jsp</location>  
</error-page>  

Or you can take help from this link to create new servlets to handle errors. Servlet Exception Handling

Normally to check for null you'd do:

       String param = request.getParameter("param");
       if(param!=null)

If they don't want you doing that, probably they want you to use the DOT operator to cause the NullPointerExpection

public void doGet(HttpServletRequest request, HttpServletResponse  response) throws IOException, ServletException, NullPointerException
{
       String param = request.getParameter("param");
       if(param.equals("x"))
       {
          //if param was null, simply using 
          //the DOT operator on param would throw
          // the NullPointerExpection
          response.sendRedirect("x.jsp");
          return;
       }
    }

To avoid having to explicitly check for null and avoid the NullPointerExpection you could do:

 if("x".equals(param))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM