1

can't find the cause of my problem the second day. I set attribute to request and forward to a jsp file. But when i try to get it from requestScope, i find out that there is no such attribute. Look at my code:

servlet:

package api.servlets;

import api.model.Task;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@WebServlet("/")
public class GetStartPageServlet extends HttpServlet {
    private Map<Integer, Task> tasks;

    @Override
    public void init() {
        final Object tasks = getServletContext().getAttribute("tasks");

        if (tasks instanceof ConcurrentHashMap){
            this.tasks = (ConcurrentHashMap<Integer, Task>) tasks;
        }else {
            throw new IllegalStateException("Your repo does not initialize!");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("tasks", tasks.values());
        getServletContext().getRequestDispatcher("/WEB-INF/view/start-page.jsp").forward(request, response);
    }
}

jsp:

<%@ page contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>Сервис задач</title>
    <style>
        <%@include file="/WEB-INF/css/style.css" %>
    </style>
</head>
<body>
<h2>Все задачи</h2>
<c:forEach var="task" items="${requestScope.tasks}">
    <ul>
        Название: <c:out value="${task.title}"/> <br>
        Описание: <c:out value="${task.description}"/> <br>

        <form method="get" action="<c:url value='/update-task'/>">
            <label>
                <input type="number" hidden name="id" value="${task.id}">
            </label>
            <input type="submit" value="Редактировать">
        </form>
        <form method="post" action="<c:url value='/delete-task'/>">
            <label>
                <input type="number" hidden name="id" value="${task.id}">
            </label>
            <input type="submit" value="Удалить">
        </form>
    </ul>
    <hr/>
</c:forEach>
<h2>Создание новой задачи</h2>
<form method="post" action="<c:url value='/add-task'/>">
    <label>Название <input type="text" name="title"/></label><br>
    <label>Описание <input type="text" name="description"/></label><br>
    <input type="submit" value="Ok" name="Ok"/>
</form>
<h2>Получить JSON задачи по id</h2>
<form method="get" action="<c:url value='/get-task'/>">
    <label>ID задачи <input type="number" name="id"></label><br>
    <input type="submit" value="Получить данные задачи" name="Ok"><br>
</form>
</body>
</html>

requestScope doesn't have field like tasks, and what i see in browser: enter image description here

Matisiuk
  • 23
  • 2
  • could you show a sample of your hasmap tasks ? I'm not sure of your tasks.values in the request. Maybe only tasks ? – pirela Aug 02 '22 at 18:48
  • When you iterate over the map, you get a sequence of `Map.Entry`, not a sequence of `Task`. See @BalusC's answer in the duplicate. – user207421 Aug 03 '22 at 00:11
  • 1
    @user207421 The attribute is assigned as `tasks.values()` so the iteration is over Task not `Map.Entry`? – DuncG Aug 03 '22 at 08:18

1 Answers1

1

Probably your problem could be with your web.xml header, try using this to make it evaluate EL correctly:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

Aside this, I'm not in touch with jsp by a long time, but I guess you can access those request attributes just calling them, without this requestScope, calling just tasks or getting them from the request request.getAttribute("tasks")

Kaneda
  • 722
  • 5
  • 16
  • Thank you, man, can you explain, why that didn't work without this code? – Matisiuk Aug 03 '22 at 11:55
  • Of course, you header needs to be declared conform Servlet 2.4 or newer and everything should be fine, is more about having jstl and your servlet version compatible, and too using the correct namespaces on your header. I learned that is just about the correct configurations to have everything compatible. Can't say more than this, hope that this can help you. – Kaneda Aug 03 '22 at 14:05