将数据从servlet发送到jsp

编程入门 行业动态 更新时间:2024-10-28 05:14:00
本文介绍了将数据从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

本文发布于:2023-10-28 18:52:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537479.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   数据   servlet   jsp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!