package com.google.launchpod.servlets;

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.launchpod.data.UserFeed;

@WebServlet("/display-feed")
public class DisplayFeedServlet extends HttpServlet{

  private static final long serialVersionUID = 1L;
  private static final String ID = "id";

  protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException{
    res.setContentType("text/html");
    //Get ID passed in request
    String id = req.getParameter(ID);
    Query query = new Query(FormHandlerServlet.USER_FEED).addSort(FormHandlerServlet.TIMESTAMP, SortDirection.ASCENDING);
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    PreparedQuery results = datastore.prepare(query);

    //Turn ID back into a key
    Key desiredFeedKey = KeyFactory.stringToKey(id);

    //Search datastore for matching key, then display its XML string
    for(Entity entity: results.asIterable()){
      if(entity.getKey() == desiredFeedKey){
          UserFeed desiredUserFeed = UserFeed.fromEntity(entity);
          res.getWriter().println("<p>" + desiredUserFeed.xmlString + "</p>");
          return;
      }
    }
  }
}
@@ -414,16 +414,33 @@
 e.Entity
+NotFoundException
 ;%0Aimport
@@ -538,177 +538,8 @@
 ry;%0A
-import com.google.appengine.api.datastore.PreparedQuery;%0Aimport com.google.appengine.api.datastore.Query;%0Aimport com.google.appengine.api.datastore.Query.SortDirection;%0A
 impo
@@ -662,11 +662,14 @@
 vlet
+ 
 %7B%0A%0A
+  
   pr
@@ -717,16 +717,18 @@
 = 1L;%0A  
+ 
+ 
 private 
@@ -759,16 +759,18 @@
  %22id%22;%0A%0A
+  
   protec
@@ -851,18 +851,23 @@
 xception
+ 
 %7B%0A
+    
     res.
@@ -899,18 +899,23 @@
 %22);%0A    
+  
+  
 //
+ 
 Get ID p
@@ -931,16 +931,20 @@
 request%0A
+    
     Stri
@@ -981,126 +981,8 @@
     
-Query query = new Query(FormHandlerServlet.USER_FEED).addSort(FormHandlerServlet.TIMESTAMP, SortDirection.ASCENDING);%0A
     
@@ -1061,69 +1061,20 @@
 ();%0A
+%0A
     
-PreparedQuery results = datastore.prepare(query);%0A%0A
     //
+ 
 Turn
@@ -1097,16 +1097,20 @@
 key%0A    
+   
+ 
 Key desi
@@ -1151,22 +1151,27 @@
 y(id);%0A%0A
+    
     //
+ 
 Search d
@@ -1233,93 +1233,50 @@
     
-for(Entity entity: results.asIterable())%7B%0A      if(entity.getKey() == desiredFeedKey)
+    UserFeed desiredUserFeed;%0A        try 
 %7B%0A  
@@ -1279,32 +1279,25 @@
 %7B%0A          
-UserFeed
+ 
  desiredUser
@@ -1327,17 +1327,42 @@
 ity(
-entity
+datastore.get(desiredFeedKey)
 );%0A
+  
     
@@ -1448,24 +1448,165 @@
     
-  return
+%7D catch (EntityNotFoundException e) %7B%0A            e.printStackTrace();%0A            res.getWriter().println(%22%3Cp%3ESorry. This is not a valid link.%3C/p%3E%22)
 ;%0A      
 %7D%0A  
@@ -1605,15 +1605,23 @@
     
+  
 %7D%0A    
-%7D
+return;
 %0A  %7D

Add try catch block and improve code and check when requested link is not valid, remove unecessary imports + loops + unecessary code