将数据从servlet发送到jsp [英] Send data from servlet to jsp

查看:246
本文介绍了将数据从servlet发送到jsp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将servlet中的列表发送到jsp页面.这是servlet代码:

I tried to send a list from my servlet to a jsp page. This is the servlet code:

Query q = new Query("post").addSort("time", SortDirection.DESCENDING);
PreparedQuery pq = datastore.prepare(q);

QueryResultList<Entity> results = pq.asQueryResultList(fetchOptions);
for (Entity entity : results) {
    System.out.println(entity.getProperty ("content"));
    System.out.println(entity.getProperty ("time"));
}
req.setAttribute("postList",results);
req.getRequestDispatcher("/tublr.jsp").forward(req, resp);

jsp代码:

<%
    QueryResultList<Entity> result = request.getAttribute("postList");
    for (Entity entity : results) {           
        <b> IT WORRRKKKK !!! </b> <br>
    }
%>

但是我得到一个错误

我添加了

<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ page import="java.util.List,com.google.appengine.api.datastore.Query.SortDirection,com.google.appengine.api.datastore.*" %>

现在我收到一个新错误

jsp文件:/tublr.jsp中的第37行出现错误 不匹配:无法从Object转换为QueryResultList ..... 造成原因:

An error occurred at line: 37 in the jsp file: /tublr.jsp Type mismatch: cannot convert from Object to QueryResultList ..... Caused by:

org.apache.jasper.JasperException:无法为JSP编译类:

org.apache.jasper.JasperException: Unable to compile class for JSP:

我是在学校里做的,我们现在必须这样,我们必须在jsp页面中使用java.

I m do it for the school and we have to di it like this now , we have to use java in the jsp page.

推荐答案

1)您需要在JSP的顶部添加import语句.

1) You need to add import statements at top of the JSP.

示例:

<%@ page import="java.util.List" %>

2)将Java代码直接嵌入JSP中是的好习惯

2) It is NOT good practice to have Java code directly embedded in JSP

SO Wiki

这篇关于将数据从servlet发送到jsp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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