Issue
Here my problem: I have a .jsp file with the following code:
form class="form" action="deleteServlet" method="post">
<%
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Query query = new Query("Notice").addSort("date", Query.SortDirection.DESCENDING);
PreparedQuery pq = datastore.prepare(query);
for (Entity notice : pq.asIterable()) {
String title = (String) notice.getProperty("title");
%>
<input type="checkbox" name="selected"
value=" <% KeyFactory.keyToString(notice.getKey()); %> " >
<%out.println(title);
} %>
<input type="submit" value="Delete!">
</form>
I do a query on AppEngine datastore to get a list of items. I need to show each of this item with a checkbox, in a way that user can select some of them and delete them from datastore. Logic is managed using a Servlet, in this way:
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
String[] noticesChecked = req.getParameterValues("selected");
for (int i=0; i<noticesChecked.length; i++) {
Key noticeKey = KeyFactory.stringToKey(noticesChecked[i]);
datastore.delete(noticeKey);
}
I get, using getParameterValues, an array of selected items. Then, I iterate on them and I try to delete them from datastore. Since I cannot pass to a servlet a Key object, I use KeyFactory.keyToString and KeyFactory.stringToKey to convert to a String and pass it.
My problem is that elements in noticesChecked are all empty String "". I checked and notice.getKey gives me correct key. Also keyToString and stringToKey seem to work well. A check on array size tell me that isn't null, but it's correctly related to items checked. However all elements in this array are "".
Someone with a similar problem? Any idea about the problem? Where is the mistake?
Thanks in advance.
Solution
Try replacing this part of the jsp code:
<% KeyFactory.keyToString(notice.getKey()); %>
by this one:
<%= KeyFactory.keyToString(notice.getKey()) %>
since you want to insert the value of the expression.
Answered By - David Levesque