简体   繁体   中英

java.lang.String cannot be cast to java.util.List

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>

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>

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() {
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM