Issue
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
Solution
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")
Answered By - Kaneda
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)