交互servlet-jsp出现问题 [英] Problem with interaction servlet-jsp

查看:51
本文介绍了交互servlet-jsp出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现问题.

我已经创建了一个jsp和一个servlet文件. 我有会话bean的remoteInterface. 我想在servlet中使用remoteInterface,然后在jsp上写入数据.

I have create a jsp and a servlet file. I have a remoteInterface of session bean. I want to use remoteInterface in servlet and after write the data on the jsp.

客户端只能看到结果页面.

The client must see only the result page.

例如:

会话bean的方法返回Collection. 我在servlet中使用了该集合,并在此标记了jsp中的所有元素之后.

A method of session bean return a Collection. I use this collection in the servlet and after this stamp all the element in the jsp.

您能帮我提供一个代码示例吗?

Can you help me with a code example.

谢谢

推荐答案

实施doGet()方法如下(使用Product作为现实世界实体的示例):

Implement doGet() method as follows (using Product as example of real world entity):

List<Product> products = yourRemoteInterface.list();
request.setAttribute("products", products); // Will be available as ${products}
request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response);

按如下方式实施JSP:

Implement the JSP as follows:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<table>
    <c:forEach items="${products}" var="product">
        <tr>
            <td>${product.name}</td>
            <td>${product.description}</td>
            <td>${product.price}</td>
            <td><img src="${product.image}" /></td>
        </tr>
    </c:forEach>
</table>

将servlet映射到例如/productsurl-pattern上的web.xml中,然后您就可以运行servlet并通过

Map the servlet in web.xml on an url-pattern of for example /products, then you'll be able to run the servlet and show the JSP by http://example.com/contextname/products.

这篇关于交互servlet-jsp出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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