简体   繁体   English

将数据从servlet发送到jsp

[英]Send data from servlet to jsp

I tried to send a list from my servlet to a jsp page. 我试图将列表从servlet发送到jsp页面。 This is the servlet code: 这是servlet代码:

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);

The jsp code: jsp代码:

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

But I get an error 但我得到一个错误

EDIT : I added 编辑:我添加了

<%@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.*" %>

And now i get a new error 现在我得到一个新的错误

An error occurred at line: 37 in the jsp file: /tublr.jsp Type mismatch: cannot convert from Object to QueryResultList ..... Caused by: 在jsp文件中的第37行出现错误:/tublr.jsp类型不匹配:无法从Object转换为QueryResultList .....由以下原因引起:

org.apache.jasper.JasperException: Unable to compile class for JSP: org.apache.jasper.JasperException:无法为JSP编译类:

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. 我是在学校里做的,现在我们必须像这样,我们必须在jsp页面中使用java。

1) You need to add import statements at top of the JSP. 1)您需要在JSP顶部添加import语句。

Example: 例:

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

2) It is NOT good practice to have Java code directly embedded in JSP 2)将Java代码直接嵌入JSP中不是一个好习惯

Read more here on SO Wiki SO Wiki上了解更多信息

Don't do any coding on JSP page. 不要在JSP页面上进行任何编码。 There is a JSTL library for this kind of stuff, and to iterate and display stuff you should use forEach tag: 有一个用于此类内容的JSTL库,要迭代和显示您应使用forEach标签的内容:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

and for loop 和for循环

<x:forEach select="${postList}" var="item">
    ... code
</x:forEach>

您是否已在jsp中导入QueryResultList

You forgot <% %> for the html code 您忘记了HTML代码的<%%>

<%
   QueryResultList<Entity> result = request.getAttribute("postList");

   for (Entity entity : results) {           

          %> <b> IT WORRRKKKK !!! </b> <br><%

   }

%>

You need to cast list obtained from request.getAttribute("postList") to QueryResultList . 您需要将从request.getAttribute("postList")获得的列表request.getAttribute("postList")QueryResultList

<%
    QueryResultList<Entity> result =(QueryResultList)request.getAttribute("postList");
    for (Entity entity : result) {           
       // Your code goes here  You can use <%= %> to print values. 
       // <b> IT WORRRKKKK !!! </b> <br>

    }
%>

For more about expression 有关表达的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM