java web开发中自定义标签内容返回Exception的问题
本人新学web开发,就遇到了这个问题,求各位大神帮忙taglib程序代码如下:
程序代码:
package szy.shopping.taglib; import java.util.List; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.TagSupport; import szy.shopping.entity.Goods; public class SearchGoodsTag extends TagSupport{ @Override public void setPageContext(PageContext pageContext) { System.out.println("set pageContext"); PageContext ctx = pageContext; } public int doStartTag() throws JspException { List<Goods> goodsList=(List<Goods>)pageContext.getRequest().getAttribute("goodsList"); try { pageContext.getOut().print("<br><br><br>"); if(goodsList.isEmpty()){ pageContext.getOut().print("找不到和您的查询 相符的商品。"); } else { pageContext.getOut().print("◆ 共找到<font color='red'>"+goodsList.size()+"</font>种符合您查询的商品<br><br>"); pageContext.getOut().print("<table border='0'>"); for (Goods goods : goodsList) { pageContext.getOut().print("<tr>"); pageContext.getOut().print("<td>"); pageContext.getOut().print("<a href='GoodsDetailsServlet?goodsID="+goods.getGoodsID()+"'><font size='+1' color='blue'> ·"+goods.getGoodsName()+"</font></a>"); pageContext.getOut().print("</td>"); pageContext.getOut().print("</tr>"); } pageContext.getOut().print("</table>"); } } catch (IOException e) { System.err.println("SearchGoodsTag.doEndTag ERROR ## "+e.getMessage()); e.printStackTrace(); } return TagSupport.EVAL_BODY_INCLUDE; } public int doEndTag() throws JspException { System.out.println("do end tag"); return TagSupport.EVAL_PAGE; } }
tld文件:
程序代码:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java. <tlibversion>1.0</tlibversion> <jspversion>1.2</jspversion> <shortname>my</shortname> <uri>http:// <tag> <name>searchResult</name> <tagclass>szy.shopping.taglib.SearchGoodsTag</tagclass> <bodycontent>JSP</bodycontent> </tag> </taglib>
jsp页面代码:
程序代码:
<%@ page language="java" contentType="text/html; charset=GBK" %> <%@ taglib uri="http:// prefix="mytags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www. http-equiv="Content-Type" content="text/html; charset=GBK"> <title>在线购物系统</title> <link href="include/style.css" rel="stylesheet" type="text/css" /> </head> <body> <table width="780" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td align="left" valign="top"> <div class="title">查询结果</div> <mytags:searchResult></mytags:searchResult> </td> </tr> </table> </body> </html>
返回的Exception如下:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /searchResult.jsp at line 30
27: </td>
28: <td align="left" valign="top">
29: <div class="title">查询结果</div>
30: <mytags:searchResult></mytags:searchResult>
31: </td>
32: </tr>
33: </table>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
szy.shopping.servlet.customer.SearchGoodsServlet.doPost(SearchGoodsServlet.java:49)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.lang.NullPointerException
szy.shopping.taglib.SearchGoodsTag.doStartTag(SearchGoodsTag.java:23)
org.apache.jsp.searchResult_jsp._jspx_meth_mytags_005fsearchResult_005f0(searchResult_jsp.java:140)
org.apache.jsp.searchResult_jsp._jspService(searchResult_jsp.java:102)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
szy.shopping.servlet.customer.SearchGoodsServlet.doPost(SearchGoodsServlet.java:49)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /searchResult.jsp at line 30
27: </td>
28: <td align="left" valign="top">
29: <div class="title">查询结果</div>
30: <mytags:searchResult></mytags:searchResult>
31: </td>
32: </tr>
33: </table>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
szy.shopping.servlet.customer.SearchGoodsServlet.doPost(SearchGoodsServlet.java:49)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.lang.NullPointerException
szy.shopping.taglib.SearchGoodsTag.doStartTag(SearchGoodsTag.java:23)
org.apache.jsp.searchResult_jsp._jspx_meth_mytags_005fsearchResult_005f0(searchResult_jsp.java:140)
org.apache.jsp.searchResult_jsp._jspService(searchResult_jsp.java:102)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
szy.shopping.servlet.customer.SearchGoodsServlet.doPost(SearchGoodsServlet.java:49)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.