In my program, I am taking two values from the user through a form in a JSP file:
- content
- name of the person (poster)
The action attribute of the form calls the Servlet. The Servlet with the help of an Entry class and PersistenceManagerFactory persists the object in the datastore using JDO and redirects it back to JSP. The JSP then queries and prints the content along with name of the person.
I can't use String datatype for "content" as it can take values upto 1500 bytes only and my requirement is more than that.
I tried using the "Text" datatype but it showed null values on querying as it is unindexed.
Here, I am trying to get the values in a List but it shows the error:
java.lang.String cannot be cast to java.util.List
Is this the correct way or is there any other way by which I can take values greater than 1500 bytes and display it back to the user by querying from a JSP?
Servlet file:
package com.pack;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
List<String> content = new ArrayList<String>();
content.add(request.getParameter("content"));
String poster = request.getParameter("poster");
Entry entry = new Entry(content, poster);
PersistenceManager pm = PMF.get().getPersistenceManager();
pm.makePersistent(entry);
pm.close();
response.sendRedirect("login.jsp");
}
}
Entry class:
package com.pack;
import java.util.List;
import javax.jdo.annotations.*;
import com.google.appengine.api.datastore.Key;
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Entry {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String poster;
@Persistent
private List<String> content;
public Entry() {
}
public Key getKey() {
return key;
}
public Entry(List<String> content, String poster) {
this.content = content;
this.poster = poster;
}
public List<String> getContent() {
return content;
}
public String getPoster() {
return poster;
}
}
JSP file:
<%@ page import="java.util.List"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="javax.jdo.PersistenceManager"%>
<%@ page import="javax.jdo.Query"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="com.pack.*"%>
<%@ page import="com.google.appengine.api.datastore.Text" %>
<html>
<head>
<style>
.entryList {
margin-left: 1100px;
margin-top: -325px;
overflow-y: scroll;
height: 630px;
}
</style>
</head>
<h1>Welcome</h1>
<div class="entry">
<form action="/LoginServlet" method="post">
Comments:<br> <br>
<textarea rows="4" cols="50" name="content">
</textarea>
<br> <br> Your name:<br> <br> <input name="poster"
type="text" value=""><br> <br> <input type="submit"
value="Post"> <br> <br>
</form>
</div>
<div class="entryList">
<h2>Updates</h2>
<%
List<Entry> entries = new ArrayList<Entry>();
PersistenceManager pm = PMF.get().getPersistenceManager();
Query query = pm.newQuery("SELECT FROM " + Entry.class.getName());
entries = (List<Entry>) query.execute();
%>
<%
if (entries.isEmpty()) {
%>
No entries
<%
} else {
%>
<%
for (Entry e : entries) {
%>
<%=e.getContent()%>
<br> posted by
<%=e.getPoster()%>
<br> <br>
<%
}
}
%>
</div>
</html>
Entry(String content){}constructor. Anyways, I have fixed the problem. Used "Text" data type of App Engine datastore and while printing in JSP, used<%=e.getContent().getValue()%>instead of<%=e.getContent()>. This prints the value of the whole large content which is entered by the user.