This question already has an answer here:
How to access request in JspTags?
(1个答案)
5年前关闭。
我通过以下方式创建自定义jsp标记:
我的问题是,我是否可以通过
然后只需从
(1个答案)
5年前关闭。
我通过以下方式创建自定义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();
关于java - 如何在JSP标签内获取HttpServletRequest/Response? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22268836/