0

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>
4
  • Please post complete stacktrace. If I've to do a wild guess, the Persistence framework is calling the public Entry(String content) constructor with the List value retrieved from the database and erroring out. Not sure why you even have this constructor. Again, stack trace should help. Commented Apr 10, 2017 at 14:00
  • I have removed the 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. Commented Apr 10, 2017 at 17:44
  • @PrakharSaxena it is recommended you post your solution as the answer to better help those community members who may also be experiencing this same issue. Commented Apr 21, 2017 at 18:30
  • @Jordan I have posted my solution. Commented Apr 22, 2017 at 9:37

2 Answers 2

1

Fixed the problem. Used Text datatype of App Engine datastore and while retrieving in JSP, used getValue(). Now I can take values greater than 1500 bytes from the user and display it back by querying from a JSP.

The problem was with the expression tag of JSP (<%= %>).

A JSP expression is used to insert the value of a scripting language expression, converted into a string, into the data stream returned to the client. When the scripting language is the Java programming language, an expression is transformed into a statement that converts the value of the expression into a String object and inserts it into the implicit out object.

JSP Expressions

When getContent() was used, it showed null values. Therefore, to insert the value of "Content" into the output stream, used getContent().getValue(). This returned the value of the "Content" including those whose sizes were greater than 1500 bytes as "Text" objects are unlimited in size.

Text

Below is the code for the same.

Servlet File:

package com.pack;
import java.io.IOException;
import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import com.google.appengine.api.datastore.Text;

public class LoginServlet extends HttpServlet {

/**
 * 
 */
private static final long serialVersionUID = 1L;

public void doPost(HttpServletRequest request, HttpServletResponse response) 
throws IOException, ServletException {

    Text content = new Text(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 javax.jdo.annotations.*;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
 public class Entry {

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;

@Persistent
private String poster;

@Persistent
private Text content;

public Entry() {

}

public Key getKey() {
    return key;
}

public Entry(Text content, String poster) {

    this.content = content;
    this.poster = poster;

}

public Text 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="com.pack.*"%>




<html>
<head>
<style>
#entryList {
margin-left: 1100px;
margin-top: -325px;
overflow-y: scroll;
height: 630px;
}

</style>
</head>
<body>




<div id="entry" class="entry">
    <h1>Welcome</h1>

    <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 id="entryList">


    <h2>Updates</h2>

    <%
        PersistenceManager pm = PMF.get().getPersistenceManager();
        Query q1 = pm.newQuery("SELECT FROM " + Entry.class.getName());
        q1.setOrdering("l desc");

        entries = (List<Entry>) q1.execute();
    %>

    <%
        if (entries.isEmpty()) {
    %>

    No entries

    <%
        } else {
    %>

    <%
            for (Entry e : entries) {

    %>

    <%=e.getContent().getValue()%>

    <br> posted by

    <%=e.getPoster()%>

    <br>
    <br> <br>


    <%

                }
            }

    %>

</div>

</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

ORM frameworks use the zero-argument constructors and you did not provide any zero-argument constructors for your Entry class. The problem is with your Entry(String content) (variable naming is important when you use tools/frameworks and content name is being used for List type as well) constructor of the Entry class which needs to be changed as shown below:

public Entry() {
}

1 Comment

I changed my constructor from public Entry(String content) { } to public Entry() { } It still shows the same error: java.lang.String cannot be cast to java.util.List

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.