• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 514
  • Last Modified:

JSP JSTL Log Database Errors

I have a JSTL web application.  I need to log database errors to a file and not output to browser.

What's the simplest/best way?
0
lcor
Asked:
lcor
  • 4
  • 4
  • 3
  • +2
8 Solutions
 
cmalakarCommented:
You should do all database operations in controller and its utilities.

Then log the errors to the log file using log4j.

JSP and your JSTL should not be aware of any database operations.
0
 
for_yanCommented:
I guess this discussion could be of some help:
http://stackoverflow.com/questions/614666/logging-java-web-applications
0
 
mrcoffee365Commented:
cmalakar is right in general.

Are you asking how to prevent webapp errors from going to the user's browser?  Then you have to create custom error pages, which is easy to do.  Add the custom error section to your web.xml, something like:
  <error-page>
        <error-code>500</error-code>
      <location>/jsp/myapp500.jsp</location>
  </error-page>

and create that .jsp error page.

Add logging to handle errors such as database errors.

Let us know more about what you're trying to do and we can help more.  For example, what logging does your webapp do now?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
lcorAuthor Commented:
I catch a JSTL exception and then use the error object to output error to screen.  

I was thinking Tomcat can probably be modified to do this.
Is there a way to take the error object and stream to afile using Tomcat Apache 5.5?
0
 
for_yanCommented:
Would not Tomcat write the exception to catalina.out anyway?
0
 
lcorAuthor Commented:
for yan,

Yea but it goes to the browser.  I can't show users the exceptions.
0
 
mrcoffee365Commented:
Then you do want my answer for the custom error.  Have you tried it?
0
 
rrzCommented:
mrcoffee>Add the custom error section to your web.xml
I agree with mrcoffee. But you could use  
<error-page>
        <exception-type>java.lang.Throwable</exception-type>
      <location>/jsp/myapp500.jsp</location>
  </error-page>  

instead. That will catch everything.  

But, if you want to do it your way, then try this demonstration JSP. If you don't want all that java code on your page, then it could be put into a custom tag or a tag file or a custom EL function.
<%@ page import="java.io.*,javax.el.ELException" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Testing!
<%!
  public String recordError(ServletContext app, ELException err){
          try{
              String fileName = app.getRealPath("/myError.log");
              FileWriter fw = new FileWriter(fileName, true);
              BufferedWriter bw = new BufferedWriter(fw);
              bw.write(err.toString());
              bw.newLine();
              bw.close();
               return "Error recorded!";
          } catch(IOException ioe){return "recording error";}
  }
%>
<c:catch var="myError">
        ${2//0}
</c:catch>
<c:if test="${myError != null}">
   <% String s = recordError(application,(ELException)pageContext.getAttribute("myError")); out.print(s);%>
</c:if>

Open in new window

0
 
rrzCommented:
The file myError.log  is created in your web app's root folder.
0
 
rrzCommented:
New version.
<%@ page import="java.io.*,javax.el.ELException" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Testing!
<%!
  public String recordError(ServletContext app, Throwable err){
          try{
              String fileName = app.getRealPath("/myError.log");
              FileWriter fw = new FileWriter(fileName, true);
              BufferedWriter bw = new BufferedWriter(fw);
              bw.write(err.toString());
              bw.newLine();
              bw.close();
               return "Error recorded!";
          } catch(IOException ioe){return "recording error";}
  }
%>
<c:catch var="myError">
        ${2//0}
</c:catch>
<c:if test="${myError != null}">
   <%=recordError(application,(Throwable)pageContext.getAttribute("myError"))%>
</c:if>

Open in new window

0
 
mrcoffee365Commented:
rrz@871311:

1) Putting an error page around Throwable results in odd behavior on certain kinds of error conditions and too much logging in others.  Using the HTTP codes like 500 is more consistent with web server behavior.  Using the HTTP codes for custom error pages is recommended practice for Tomcat (and of course, all other Web servers).

2) creating your own file rather than using a logging system is also odd.
0
 
rrzCommented:
mrcoffee>1) Putting an error page around Throwable results in odd behavior on certain kinds of error conditions and too much logging in others.
The code within the <catch> tag is the only code being monitored and the only thing that is printed is the result of the toString method on the exception.  
>2) creating your own file rather than using a logging system is also odd.  
I agree, it is not standard.  But Icor asked as part of his question;
>What's the simplest/best way?  
The best way I assume is to use cmalakar's suggestion(log4j).
I posted my code to suggest a simple way.  
0
 
cmalakarCommented:
JSP's are only meant for presentation.

if you have the control of JSTL code, I suggest to move the db related operations into some controller code.
0
 
lcorAuthor Commented:
good discussion
0
 
lcorAuthor Commented:
wiil use a combination of all solutions
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now