Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Lab 10
Google App Engine
Tomas Lampo
November 11, 2010
Today, we will create a server that will hold information for the XML
parsing app we created on lab 8. We will be using Eclipse and Java, but we
will also use Objective C.
1. Open Eclipse and make sure that you have installed the Google Plugin
(http://code.google.com/appengine/docs/java/tools/eclipse.html).
2. Click File → New → Web Application Project and create a project
named “myNews”. Package name should be “mynews”.
3. Google generates automatically for you a dense folder structure. For
this project, we will ignore whatever is in the test package, as well as
the packages mynews.client, mynews.server and mynews.shared.
4. Right click the mynews package and add a New Class, called ArticlesServlet.
A servlet is nothing but a HTTP request manager, that receives infor-
mation and then yields a document in a given format. It should look
like this:
package mynews;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.jdo.PersistenceManager;
1
import javax.servlet.http.*;
public class ArticlesServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, World!");
}
}
What this class does, is that it receives a GET request and creates a
plain text document that says “Hello, World!”.
5. So now we have the servlet ready, but how do we access it on the
server? Well, we need to map an address on the server to it. The only
way to do that is by modifying the file war/WEB-INF/web.xml. Make
sure you’re working in Source Mode (click the Source tab), so it will be
easier to modify.
Again, Google already populated the file for you. We want to connect
the servlet we created with the url YOURSERVER/articles.

ArticlesServlet
mynews.ArticlesServlet


ArticlesServlet
/articles

Can you understand how it works?
6. Test your server. Go back to the servlet Class we created and then
select Run → Run As → Web Application.
2
If there are no errors in your code, this will create a local server in your
computer, in port 8888. Visit the url http://127.0.0.1:8888/articles
and you should see the “Hello, World!” string.
7. That’s all you need to create a basic website with the Google App
Engine! We will now create a new class that describes the Article
object, and tell the server that this class can actually hold objects,
acting like a database. Right click the mynews package and add a new
class. Call it Article.
8. You should already be familiar with Java, so here’s a quick explanation
of what the Article object has:
• String title
• String content
• String author
• Date date
But, since this class actually will act like a database, we need to add
some special tags:
package mynews;
import java.util.Date;
import com.google.appengine.api.datastore.Key;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
@PersistenceCapable
public class Article {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
3
@Persistent
private String author;
}
Can you see how author looks? Do the same for the title, content
and date.
Using Eclipse, generate the Getters for all the variables, and Setters
for all the variables except key.
You should also generate an Article creator method that receives the
author, title and content, and assigns the current date automatically
to the object.
9. Create a new class and call it PMF. This class will be the Database
Manager. Its contents should look like this:
package mynews;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;
public final class PMF {
private static final PersistenceManagerFactory pmfInstance =
JDOHelper.getPersistenceManagerFactory("transactions-optional");
private PMF() {}
public static PersistenceManagerFactory get() {
return pmfInstance;
}
}
Include this class whenever you need to make database calls in Google
App Engine.
10. From now on, you should be able to continue on your own. I will
provide clues for the complicated parts, but you should provide the
rest of the code yourself.
You are going to modify the ArticlesServlet class you created so it
generates an XML document with a format that can populate the app
you created in Lab8. For this, you will this:
4
PersistenceManager pm = PMF.get().getPersistenceManager();
resp.setContentType("text/xml"); //Our file will be in XML format
resp.getWriter().println("");
//Prints the XML document header.
String query = "select from " + Article.class.getName() + "";
//Query for getting all the articles
List
articles = (List
) pm.newQuery(query).execute(); //The articles container. /*Provide code that will print all the articles with the correct format for your app. Resulting XML should look like this: New 1 Thu, 11 Nov 2010 18:37:20 UTC Content 1 Tomas Lampo ... !!!Make sure you’re printing the date in the right format. Use a date formatter for that. */ 11. Create a Servlet that allows you to add new articles to the server. Whenever you make a call like this: http://127.0.0.1:8888/add?title=New&content=Content%20Test&author=Tomas%20Lampo It should save an article in the server with the given information. You will need to have this code: resp.setContentType("text/plain"); String title = req.getParameter("title"); 5 /* Get the author and the content.*/ Article a = new Article(author, title, content); String ans = "NO"; PersistenceManager pm = PMF.get().getPersistenceManager(); try { pm.makePersistent(a); } finally { ans = "YES"; } pm.close(); resp.getWriter().println(ans); } That’s it! Remember to add this servlet to the web.xml file and test your servlets! 12. Finally, upload your app to the Google server. Go to https://appengine.google.com/ and create a new application. Then, go back to Eclipse and select the little Airplane/Engine button. Whatever you do, do not click the Cancel button once the upload pro- cess started. You will block your app on the server forever. Believe me. Now that we know that, let’s upload the app to the server. Input your Google email and password and click the “App Engine Project Settings” blue link. Remember the App ID you used on the server? It must match the app ID on the setting. Click the Deploy button and watch how Eclipse uploads your app for you (Warning. This process is super slow. Again: Do not click the cancel button). 13. Let’s go back to programming the iPhone. You’re almost done! Dupli- cate your Lab8 folder and open your Lab8 project in XCode. Modify the AppDelegate Class so your app connects to the new URL in your server. Also modify the ParseOperation class to match the new tags you used in your XML document. 6 14. Upload 3 random articles in your server by modifying the URL to match your server’s address and having your name as the author, instead of mine. http://yourapp.appspot.com/add?title=New%201&content=Content%201&author=Tomas%20Lampo http://yourapp.appspot.com/add?title=New%202&content=Content%202&author=Tomas%20Lampo http://yourapp.appspot.com/add?title=New%203&content=Content%203&author=Tomas%20Lampo Did the articles appear in your app? 7