顯示具有 JSP 標籤的文章。 顯示所有文章
顯示具有 JSP 標籤的文章。 顯示所有文章

2012年11月13日 星期二

JSP or Struts simply show error page and exception inforamtion

1. Problem
In develop the web application by struts or jsp,
it has the requirement to show the error message,
but use the struts default exception handler method is complex to show data,
because it involve the bean, action setting for exception type.

I study the inline error page show and share it.


2. Solution
I use simple jsp handle object,
pageContext.exception and
pageContext.errorData


3. Example

Notice: if you use struts,
don't write the <global-exceptions> tag,
we will transfer the exception to the tomcat handle

A. in web.xml write
=============================================
<error-page>
        <error-code>404</error-code>
        <location>/error.jsp</location>
</error-page>
 
<error-page>
     <exception-type>java.lang.Throwable</exception-type>
      <location>/error.jsp</location>
</error-page>
=============================================



B. error.jsp
=============================================

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page isErrorPage="true" %>
<html>
<head>
<title>Error Page</title>

 <style type="text/css">
h2{background:darkblue;color:white}
h3{background:darkblue;color:white}
.style1 {
    color: #66CCFF;
    font-size: x-large;
    font-weight: bold;
}
</style>


</head>
<body>
<span class="style1">Oops...</span><br>
<div>
A system error has occured. <br>
If the
problem persists, please report this information to the System Administrator.
</div>

<br/>
[<a href="./index.jsp">GoGack to index</a>]
[<a href="javascript:showDetails()">Show details</a> ]
[<a href="javascript:hideDetails()">Hide details</a> ]
<br/>


<table id="showDetailsLinkDiv" style="{display:none}"   cellspacing="0" width="100%" border="1">
 
    <tr valign="top">
        <td width="40%"><b>Error:</b></td>
        <td>${pageContext.exception}</td>
    </tr>
 
    <tr valign="top">
        <td><b>URI:</b></td>
        <td>${pageContext.errorData.requestURI}</td>
    </tr>
 
    <tr valign="top">
        <td><b>Status code:</b></td>
        <td>${pageContext.errorData.statusCode}</td>
    </tr>
 
    <tr valign="top">
    <td><b>Stack trace:</b></td>
    <td>
        <c:forEach var="trace"
                 items="${pageContext.exception.stackTrace}">
        <p>${trace}</p>
    </c:forEach>
    </td>
    </tr>

</table>

<script language="javascript">
   function showDetails() {
     document.getElementById("showDetailsLinkDiv").style.display = "inline";
   }
   function hideDetails() {
     document.getElementById("showDetailsLinkDiv").style.display = "none";
   }
</script>
</body>
</html>


=============================================


Reference:
http://geekexplains.blogspot.tw/2008/06/errorexception-handling-in-jsp-using.html

2010年5月21日 星期五

Tomcat log

=== Log Discuss ===
* Tomcat 會有兩種 log: log4j, logging.properties(/opt/apache-tomcat-5.5.27/conf), 而若要看 GAP log 其方式之一是將 .level 設定為 ALL
{{{
....
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatteri
#open the level you will see the all package information
.level = ALL
....
}}}
Reference:
[http://www.laliluna.de/articles/log4j-tutorial.html log4j_tutorial]
[http://blog.xuite.net/chihho32/blog/9121126 log4j_tutorial_2]
[http://tomcat.apache.org/tomcat-5.5-doc/logging.html Tomcat_log_intro]

2010年4月2日 星期五

Attribute 討論

若在 JSP 中要儲存一個變數到下次還能使用可利用 setAttribute on session class,
當要取用時可用 getAttribute, 但要注意 get 時要給定型態, 否則會發生型態錯誤, 理由是 setAttribute 可能用 hash table 存 key value, 而 value 則是 primitive object.

ex.
String dmPath = "/home/Andy", dmPath2;
session.setAttribute("dmPath", dmPath);

dmPath2 = (String) session.getAttribute("dmPath");

2010年3月22日 星期一

Import 討論

1. import 可有兩種方式
a. Using the jar file putting in the WEB-INF/lib/ directory
b. Using the .class putting in the WEB-INF/classes/

2. Then you will restart the tomcat to setting the classpath .


[reference about the import discuss]
http://www.javaworld.com.tw/jute/post/view?bid=6&id=47626&sty=3
若要將自己寫的class放在web應用程式的classes目錄下
則該class必須設定package
否則在jsp中 不論是直接new or 用
都會發生不認得該class的錯誤

第二點:
classes目錄以及其子目錄的class Tomcat並不會"自動幫你import"
但是tomcat會自動幫你將web應用程式下的classes目錄"自動加入classpath"
"自動幫你import" 和 "自動加入classpath" 是不同的
所以 若你在jsp中要使用你自訂在classes目錄下的class 但是你並沒有使用<%@ page import="..." %>的話
則 你在使用該class時 就必須指定該class的全名