如何通过jsp会话在html页面上动态显示数据

时间:2016-06-16 07:51:39

标签: java jsp session java-ee

我的文本区域是子弹格式,我希望通过会话以子弹格式显示html中的数据。

那么如何发送该数据?每个用户数据包含和长度不同例如(1个用户使用3个子弹点和2个用户使用5个子弹点)如何在html页面和显示器上发送该动态数据。数据库未使用..

1 个答案:

答案 0 :(得分:0)

识别并解析每个文本区域的项目符号(比如发帖)

class BulletPoint{
  String content;
  public BulletPoint(String content){
        super();
        this.content=content;
     }
   //put public getters and setters here
}

class Post{
 List<BulletPoint> bulletPoints=new ArrayList<>();
 //put public getters and setters here
}

@WebServlet("/posts")
class DisplayPostServlet extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse  response)
        throws IOException {
    //parse and prepare text area contents to list
    //for each text area 
       //new Post p
        //create bulletin list for post
        //set bulletins to post
    //end for 

    //simple example 
    List<Post> posts=new ArrayList<>();
    for(int i=1;i<5;i++){
      Post newPost=new Post();
        for(int j=1;j<=i;j++){
          Bulletin bulletin=new Bulletin("bulletin content"+j);
          newPost.getBulletins().add(bulletin);
        }

       posts.add(newPost);
    }


   //add posts in request scope, and forward to jsp page
   request.setAttribute("posts",posts);
   request.getRequestDispatcher("/displayPosts.jsp").forward(request,                response);
  }


}

displayPosts.jsp

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

     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>


  <div>
     <c:forEach var="singlePost"  items="${posts}" varStatus="loopIndex">
      <ul> Post ${loopIndex}
        <c:forEach var="singleBulletin"  items="${singlePost.bulletins}">
            <li><c:out value="${singleBulletin.content}"/>
            </li>
         </c:forEach>
      </ul>
    </c:forEach>
 </div>



</body>

</html>