我用表单制作了 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);
} ;