一,using jsp
App Engine java提供對JSP,JSTL的幾乎完全支援。本課主要介紹如何在GAE for java中使用jsp及jstl。
開啟eclipse菜單->Preferences->Java->Installed JREs,添加jdk安裝目錄,並設為預設。預設eclipse僅指向了jre目錄。
開啟gapp_flexblog項目,在war目錄下添加jsp檔案greeting_via_jsp.jsp,如下:
<%@page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="sban.flexblog.HelloWorldUsingJPA"%>
<%@page import="sban.flexblog.GreetingEntity"%>
<%@page import="java.util.List"%>
<html>
<body>
<%
if (null != request.getParameter("message"))
{
String action = request.getParameter("message");
response.getWriter().write(action + "<br />");
}
%>
<form action="/gapp_flexblog/hello" method="post">
user:<input name="username" value="sban" maxlength="50" ></input><br />
greeting content:<input value="greeting content1" name="greetingContent" maxlength="250" ></input><br />
<input type="hidden" name="action" value="new" />
<input type="submit" value="Post Greeting" />
</form>
<div>
<form action="/gapp_flexblog/hello" method="post">
<input type="submit" value="Delete All Greetings" />
<input type="hidden" name="action" value="delall" />
</form>
</div>
<div>
<h3>Greetings list:</h3>
<ul style="list-style: decimal;">
<%
HelloWorldUsingJPA operator = new HelloWorldUsingJPA();
List<GreetingEntity> greetings = operator.getAllGreetings();
if (greetings.size() > 0)
{
for(GreetingEntity greeting : greetings)
{
%>
<li>
id:<%= greeting.getId() %>
user:<%= greeting.getUser() %>
greetingContent:<%= greeting.getGreetingContent() %>
date: <%=greeting.getDate().toString() %>
</li>
<%
}
}else{
%>
<li>Empty!</li>
<%
}
%>
</ul>
</div>
</body>
</html>