Test.jsp 页面:<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://wish.jsp.tag" prefix="abc"%>
<html>
<head>
<title>My JSP 'test.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<abc:test url="test.jsp"/>
dsgdfgdfgdfg <!-- 测试数据 -->
</body>
</html>
index.jsp 页面:<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<table width="909" border="1" cellpadding="0" cellspacing="0">
<tr>
<td height="181" colspan="2" valign="top">udydhehdydhydhydh</td>
</tr> <!-- 测试数据 -->
<tr>
<td width="247" height="421" valign="top"></td>
<td width="662" valign="top">
<a href="MyJsp.jsp">asd</a>
<c:if test="${!(empty requestScope.url)}"><!-- 判断是否为空-->dfsaf
<jsp:include page="${requestScope.url}" flush="true"></jsp:include>
<!-- 导入相关页面-->
</c:if>
</td>
</tr>
</table>
</body>
</html>
助手类:package myTags;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.TagSupport;public class MyTag extends TagSupport {
private String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int doStartTag()
throws JspException{
return super.doStartTag();
}
public int doEndTag()throws JspException{ ServletResponse response = pageContext.getResponse();
ServletRequest request = pageContext.getRequest();
request.setAttribute("url", this.url);
try {
request.getRequestDispatcher("index.jsp").forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return super.doEndTag();
}
}tld文件:<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>mytag</shortname>
<uri>http://wish.jsp.tag</uri>
<tag>
<name>test</name>
<tagclass>myTags.MyTag</tagclass>
<bodycontent>empty</bodycontent>
<attribute>
<name>url</name><!-- url属性用于保存要加载的页面 -->
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
意思是如果访问了test.jsp页面 就会加载我定义的标签 然后跳转到index.jsp页面 index.jsp页面会把test页面的内容加载进来程序运行起来是正常的 可是每次运行的时候都会出现异常:Caused by: java.lang.IllegalStateException: Cannot forward after response has been committed请问怎样解决啊 !是不是 这样的逻辑本来就是不对的!
用response.sendRedirect试试。