Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Error Handling Question

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm learning how the error is handled throught DD mapping.
I am running following code to see how the servlet get invoked and display somthing on browser.

http://localhost:8080/myWeb/AAA/error2

ErrorTest2 servlet throws new IOException("wrong stuff.."), based on the DD, MyErrorServlet2 servelt will be invoked, however I can only see the console output, but nothing is rendered in browser.
---- ---- ---- ---- ---- ---- ----
public class ErrorTest2 extends HttpServlet
{
public void doGet( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
System.out.println("In ErrorTest2 class, I am going throw IOException exception.");
throw new IOException("wrong stuff...");
}
}
--- ---- ---- ---- ---- ---- ---- ----
public class MyErrorServlet2 extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
Integer scode = (Integer) req.getAttribute("javax.servlet.error.status_code");
System.out.println("Ha, status code is: " + scode );

res.setContentType("text/html");
PrintWriter out = res.getWriter();

out.write("<html><body>");
out.write("<br>Ha, error message is: " + msg );
out.write("</body></html>");
}
}



------- DD ---------------------
<servlet>
<servlet-name>ET202</servlet-name>
<servlet-class>coreservlets.ErrorTest2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ET202</servlet-name>
<url-pattern>/AAA/error2</url-pattern>
</servlet-mapping>


<servlet>
<servlet-name>MyErrorServlet2</servlet-name>
<servlet-class>coreservlets.MyErrorServlet2</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>MyErrorServlet2</servlet-name>
<url-pattern>/aaa/*</url-pattern>
</servlet-mapping>

<error-page>
<exception-type>java.io.IOException</exception-type>
<location>/servlet/MyErrorServlet2</location>
</error-page>
--------------
What's wrong with code?

Thanks in advance.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

<location>/servlet/MyErrorServlet2</location>


MyErrorServlet2 is mapped to "/aaa/*", so you should write :


I'm not asking why there's an undeclared "msg" variable in MyErrorServlet2
 
Grace Yang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.


<error-page> <exception-type>java.io.IOException</exception-type> <location>/aaa/MyErrorServlet2</location></error-page>



I tried, it does not work. Should the location use "/servlet ..." <location>/servlet/MyErrorServlet2</location>

I corrected my MyErrorServlet2 servlet
-----------
public class MyErrorServlet2 extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
Integer scode = (Integer) req.getAttribute("javax.servlet.error.status_code");
System.out.println("Ha, status code is: " + scode ); //1

res.setContentType("text/html");
PrintWriter out = res.getWriter();
System.out.println(" content type is: " + req.getContentType() ); //2

out.write("<html><body>");
out.write("<br>Ha, status code is: " + scode );
out.write("</body></html>");
}
}

I can see consolue ouput status code at line 1, null at line 2. Still I am getting HTTP Internal Server Error -- website can not display the page.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using "/servlet" ? Are you using Tomcat's InvokerServlet ?
 
Grace Yang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using TomCat 5.

The study material says the using "/servel/.." in the location, and it must start with "/servelt", anyway I tried lots of version in the location, just would not work.
 
Grace Yang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anybody could help?

Thanks
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic