将MongoDB块从servlet传递到JSP

时间:2017-09-21 19:48:27

标签: mongodb jsp mongodb-query

我有以下Block,它根据我应用的过滤器过滤数据;即它应该只打印那些与" name"

相匹配的数据
            Block<Document> printBlock = new Block<Document>() {
            @Override
            public void apply(final Document document) {
                System.out.println(document.toJson());
            }
       };



       collection.find(eq("name", name)).forEach(printBlock);

我希望将这些数据从当前的servlet发送到JSP,以便我可以显示数据。我应该怎么做呢?

编辑:

JSP页面:

 <form action="SearchName" method="post">
      Name:  <input name="name" size="20" />
     <input type="submit"   value="Search">
  </form>

的Servlet

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

     String name= request.getParameter("name");

    // Create Mongo connection to the DB
    MongoClient mongoClient = new MongoClient( "localhost", 27017);

    // Select the DB
    MongoDatabase database = mongoClient.getDatabase("myDatabase");

    // Select the collection
    MongoCollection<Document> collection = database.getCollection("myCollection");

   Block<Document> printBlock = new Block<Document>() {
        @Override
        public void apply(final Document document) {
            System.out.println(document.toJson());
        }
   };
   collection.find(eq("name", name)).forEach(printBlock);
  }

因此我只想要那些与&#34; name&#34;匹配的记录。我成功到达这里。接下来我如何将它们从servlet传递给jsp以及它们是如何在JSP中接收的?提前感谢您的帮助

0 个答案:

没有答案
相关问题