我用一个表单制作了JSP页面。用户可以写评论,它将以POST方式发送。 servlet将它存储在数据存储区中并列出所有注释并将其转发给jsp,jsp会将它们放在一个表中。问题是在第一次访问中没有任何东西会存在,因为doPOst没有被调用!这是我的代码
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// INSERT CODE HERE
System.out.println("received HTTP POST");
String postContent = request.getParameter("post_text");
System.out.println(postContent);
Date date= new Date();
Timestamp timeStamp= new Timestamp(date.getTime());
long lonTime= timeStamp.getTime();
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity post = new Entity("post");
post.setProperty("content", postContent);
post.setProperty("time", lonTime);
datastore.put(post);
System.out.println( post.getKey() );
int pageSize = 20;
FetchOptions fetchOptions = FetchOptions.Builder.withLimit(pageSize);
Query q = new Query("post").addSort("time", SortDirection.DESCENDING);
PreparedQuery pq = datastore.prepare(q);
QueryResultList<Entity> results = pq.asQueryResultList(fetchOptions);
System.out.println("-------------------");
for (Entity entity : results) {
System.out.println(entity.getProperty ("content"));
System.out.println(entity.getProperty ("time"));
}
request.setAttribute("postList",results);
String strViewPage="/tublr.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(strViewPage);
if (dispatcher != null){
dispatcher.forward(request, response);
} ;