1 / 23

Collecting and storing data

Collecting and storing data. http://www.flickr.com/photos/maggiew/6145245962/. Overview of HTML forms. HTML forms enable your web application to collect information from your users. Web server. Browser. Server-side Programs. Type URL. Gimme HTML. HTML for form. Show form.

kiril
Download Presentation

Collecting and storing data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Collecting and storing data http://www.flickr.com/photos/maggiew/6145245962/

  2. Overview of HTML forms • HTML forms enable your web application to collect information from your users Web server Browser Server-side Programs Type URL Gimme HTML HTML for form Show form User fills out form Send values entered Do something withthese values, please

  3. In addition to FORMs, don’t forget that AJAX is another option! Web server Web page UI Your AJAX code Type or click Request action Update UI Send message to server User sees theaction started Server sends back some data Update UI User sees theaction is done

  4. Textbox fields <form action="myjsp.jsp" method="POST"> <input type="text" name="myfield"> <input type="submit"> </form> When your user types a value and hits submit, the form sends the value of myfield to the server. Notice the value appears on the URL.

  5. Buffet of choices • Text <input type="text"> • Password <input type="password"> • Hidden <input type="hidden"> • Textarea <textarea></textarea> • Radio <input type="radio" checked> • Checkbox <input type="checkbox" checked> • Dropdown <select><option value="1">A</option></select> • Multi-select list <select multiple size="3"> <option value="1">A</option></select> (And <input type="file">, but this requires special handling)

  6. Form methods (GET vs POST) • Difference in purpose • GET is for retrieving data from the server(or any other purpose that can safely be repeated an arbitrary number of times) • POST is for making changes to the server(or any other purpose that cannot be safely repeated an arbitrary number of times) • And for sending any confidential data (e.g., passwords)

  7. Examples of good ways to use GET • Retrieving an HTML table or list • Retrieving a form • Checking to see if the page still exists • Checking to see if the server has crashed • Checking to see fast the server is today All of these can safely be repeated lots of times. Repeating these won't mess up the server. These are called "idempotent operations."

  8. Examples of bad ways to use GET.For these, use POST instead. • Deleting data from the server • Updating data on the server • Logging in (changes state on the server) • Logging out (ditto) Each of these changes the state of the server, so repeating them an arbitrary number of times could mess up the server.

  9. Accessing data on the server • JSP provides a "request" object that you can use to access data sent to the server • And it provides an "out" object that you can use to write data back to the browser • Use <% … %> to enclose Java statements • NOTE: JSP also has some very nice tags that you can use instead. Using Java makes it relatively easy to copy & paste your code into a servlet if you ever want to do so. <% out.write(""+request.getParameter("myfield")); %>

  10. So now you've got the data… How to store it? • Old school: RDBMS • Nice tidy tables linked by keys • Problems: • Not particularly good scalability • Generally tuned for small, rare writes • All the rows in a table must have the same schema

  11. Imagine this instead… • An enormous table • With multiple rows per entity • One row per attribute of the entity • Only two columns • One for a key that includes the type of the entity and the name of a member variable • One for the value of that variable • And we store different parts of the table on different machines on a fast network

  12. Example (Illustrative)

  13. That’s the general idea of the data storage behind the GAE Datastore • Can be implemented with Google’s BigTable • Which also includes compression, intelligent grouping of rows, timestamps, type metadata, … • Wrapped with Java Data Object (JDO) layer • 1. Instantiate object • 2. Tell DataStore, “Save this for me till later” • 3. Use SQL-like queries to retrieve objects • This “Not Only SQL” (noSQL) approach is easily 100 times more scalable than RDBMS

  14. How to use GAE datastore • Configure the datastore with XML files • Instantiate a PersistenceManagerFactory • Hold that factory reference in memory • Ask the factory for a connection to datastore • Instantiate or query for some objects • Set values on those objects • Save the objects • Close the connection

  15. Connecting to the datastore package edu.oregonstate.mobilecloud; import javax.jdo.JDOHelper; import javax.jdo.PersistenceManagerFactory; public final class PMF { private static final PersistenceManagerFactorypmfInstance = JDOHelper.getPersistenceManagerFactory("transactions-optional"); public static PersistenceManagerFactorygetPMF() {return pmfInstance;} private PMF() {} } Instantiate factory Hold reference in static variable In JSP or servlet, request a manager (connection) Remember to close when finished (use try/finally) <%@ page import="javax.jdo.PersistenceManager" %> <%@ page import="edu.oregonstate.mobilecloud.PMF" %> <% PersistenceManagerpm = PMF.getPMF().getPersistenceManager(); try { out.write(""+pm); } finally { pm.close(); } %> test1.jsp

  16. Defining an object that can be stored package edu.oregonstate.mobilecloud.lectures.clouddatastore; import javax.jdo.annotations.PersistenceCapable; import javax.jdo.annotations.Persistent; import javax.jdo.annotations.PrimaryKey; @PersistenceCapable public class Test2Simple { @PrimaryKey @Persistent private String title; public String getTitle() { return title != null ? title : ""; } public void setTitle(String title) { this.title = title; } } Must be PersistenceCapable Declare a PrimaryKey that is Persistent (details later)

  17. Saving an object <%@ page import="javax.jdo.PersistenceManager" %> <%@ page import="edu.oregonstate.mobilecloud.PMF" %> <%@ page import="edu.oregonstate.mobilecloud.lectures.clouddatastore.*" %> <% PersistenceManager pm = PMF.getPMF().getPersistenceManager(); try { Test2Simple obj = new Test2Simple(); obj.setTitle("an object: " + Math.random()); pm.makePersistent(obj); } finally { pm.close(); } %> Get a connection Instantiate the object makePersistent Close connection test2.jsp

  18. Inspecting the datastore • Go to http://localhost:8888/_ah/admin/ • Browse through the data • The Full Key will be shown (more details later) • Blobs / clobs will not be shown

  19. Saving more fields package edu.oregonstate.mobilecloud.lectures.clouddatastore; … @PersistenceCapable public class Test3Members { @Persistent private intcoursenum; @Persistent private float difficulty; @Persistent private java.util.Date creation; @Persistent private com.google.appengine.api.datastore.Text description; // … getters and setters follow Members can be ints, longs, floats, Strings (< 500 chars), Text objects (clobs), serializable objects.

  20. Compound objects… serializable children • @PersistenceCapable • public class Test4Compound { • @Persistent(serialized = "true") • private Test4Child serializableChild; • …. Parent class declares that one of its members is serialized. Child class declares that it is serializable. import java.io.Serializable; @SuppressWarnings("serial") public class Test4Child implements Serializable { …

  21. Compound objects… arrays of children • package edu.oregonstate.mobilecloud.lectures.clouddatastore; • import java.util.ArrayList; • import java.util.Date; • import javax.jdo.annotations.IdGeneratorStrategy; • import javax.jdo.annotations.PersistenceCapable; • import javax.jdo.annotations.Persistent; • import javax.jdo.annotations.PrimaryKey; • @PersistenceCapable • public class Test4Compound { • @Persistent • private ArrayList<Date> importantDates = new ArrayList<Date>(); • public ArrayList<Date> getImportantDates() { • return importantDates; • } • public void setImportantDates(ArrayList<Date> importantDates) { • this.importantDates = importantDates; • } Parent should use ArrayList, HashSet, or TreeSet (or interface/abstract class equivalent)

  22. Changing the schema(aka “soft schemas”) • Not all entities of a certain kind must have the same member variables • If you add members to a class, instances loaded from the Datastore will lack those members. • So in this case, use nullable Object types (such as Long, Integer, Float) when you add members • If you delete members to a class, it’s ok. • Do not change the type of members. It is bad.

  23. A few final notes • If you don’t want a member persisted, use @NotPersistent • Remember to make member variables private • JDO might actually modify the variable’s bytecode

More Related