V
V
Viktoria Smirnova2018-03-04 15:38:26
MySQL
Viktoria Smirnova, 2018-03-04 15:38:26

HTTP Status 500 cannot be cast to?

Community, help needed!

Browser throws an error:

HTTP Status 500 - java.util.ArrayList cannot be cast to masters.User
________________________________________
type Exception report
message java.util.ArrayList cannot be cast to masters.User
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.ClassCastException: java.util.ArrayList cannot be cast to masters.User
masters.masterservlet.MastersServlet.doGet(MastersServlet.java:52)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
javax .servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.56 logs.
____________________________________________
Apache Tomcat/7.0.56


My Code
in index.jsp:

<script>
stationList = [

<c:forEach items="${usersB}" var="userB">
       {"latlng":[<c:out value="${userB.barberlat}" />, <c:out value="${userB.barberlng}" />],
           name:"<c:out value="${userB.barbername}" />",
           url:"/MastersServlet?action=listUser&userId=<c:out value="${userB.barbername}"/>",
           address:"Dostyk str"},
        </c:forEach>
    ];
...
<script>


in Servlet:

....
private static String INSERT_OR_EDITB = "user.jsp";
....
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String forward = "";
        String action = request.getParameter("action");
else if (action.equalsIgnoreCase("listUser")) {
            forward = INSERT_OR_EDITB;
            String userId = request.getParameter("userId");
            User user = (User) dao.getAllUsersM(userId);
            request.setAttribute("user", user);


in DAO:

public List<User> getAllUsersM(String userId) {
        List<User> users = new ArrayList<User>();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("select * from master_table where barbername=?");
            preparedStatement.setString(1, userId);
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
               User user = new User();
               System.out.println(rs.getString("mastername"));
                user.setMastername(rs.getString("mastername"));
                user.setMasterlastname(rs.getString("masterlastname"));
                user.setMastersphone(rs.getString("mastersphone"));
                user.setMasteravatar(rs.getString("masteravatar"));
              /* user.setMasteregistration(rs.getDate("masterregistration"));*/
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }


in user.jsp

<body>
<table border=1 style="width: 100%">
    <thead>
    <tr>
        <th>name</th>
        <th>avatar</th>
        <th>lastname</th>
        <th>phone</th>

    </tr>
    </thead>
    <tbody>
    <c:forEach items="${users}" var="user">
        <tr style="cursor:pointer " onMouseOver="this.style.background='#FFCC33'"  onMouseOut="this.style.background='white'"
            onClick="window.open('MasterServlet?action=listUser&userId=<c:out value="${user.mastername}"/>','newwindow',
                    'width=600,height=650')">
            <td><c:out value="${user.mastername}" /></td>
            <td><c:out value="${user.masterlastname}" /></td>
            <td><c:out value="${user.mastersphone}" /></td>
            <td><c:out value="${user.masteravatar}" /></td>
            
        </tr>
    </c:forEach>
</tbody>
</table>
</body>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2018-03-04
@Vika7

user = (User) dao.getAllUsersM(userId);
Obviously getAllUsersM returns a list of users.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question