我通过以下方式创建自定义jsp标记:
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
public class FooTag extends SimpleTagSupport
{
public void doTag() throws JspException, IOException
{
JspWriter out = getJspContext().getOut();
out.println( "some response" );
}
}
我的问题是,是否有我的方式来访问HttpServletRequest
或HttpServletResponse
在doTag()
方法?如果它在Java中不可用,我可以通过某种方式将它作为来自jsp的参数传递吗?请指教。
由于您正在扩展SimpleTagSupport
,因此您可以调用继承的方法getJspContext()
,该方法将返回一个JspContext
对象(该对象将使您能够访问页面,请求和会话对象),并将其转换为PageContext
:
PageContext context = (PageContext) getJspContext();
然后只需ServletRequest
从context
对象中检索即可:
ServletRequest request = (HttpServletRequest) context.getRequest();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句