servlet结果显示在jsp页面中 [英] servlet result display in jsp page

查看:85
本文介绍了servlet结果显示在jsp页面中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将servlet输出转发到jsp页面?

How to forward the servlet output to jsp page?

这意味着结果将显示在JSP页面中.

That means the result will be displayed in the JSP page.

推荐答案

您通常不使用servlet来生成HTML输出.您通常为此使用JSP/EL.使用out.write和consorts流HTML内容被认为是不好的做法.更好地利用请求属性.

You normally don't use a servlet to generate HTML output. You normally use JSP/EL for this. Using out.write and consorts to stream HTML content is considered bad practice. Better make use of request attribtues.

例如:

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    Object data = "Some data, can be a String or a Javabean";
    request.setAttribute("data", data);
    request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
}

在web.xml中将其映射到例如/page<url-pattern>上.将JSP放在/WEB-INF中以防止直接访问.然后,您可以在JSP中使用EL(表达语言 )以访问范围内的属性:

Map this in web.xml on an <url-pattern> of for example /page. Place the JSP in /WEB-INF to prevent direct access. Then in the JSP you can use EL (Expression Language) to access scoped attributes:

<p>The data from servlet: ${data}</p>

通过http://example.com/context/page调用servlet.就那么简单.这样,您可以在一个位置(即JSP)控制输出和表示.

Call the servlet by http://example.com/context/page. Simple as that. This way you control the output and presentation at one place, the JSP.

这篇关于servlet结果显示在jsp页面中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆