При добавлении Cookie в код возникает ошибка 500
Создала проект в Intellij Idea Ultimate с
Generations: JavaEE
Template: Web Application
Language: Java
Build System: Maven
Specifications: Web profile
Создала страницу сервлета и index.jsp, всё работало. Но после добавления в код cookie при запуске выдает ошибку:
org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: [14] in the generated java file: [C:\Users\a_sen\AppData\Local\JetBrains\IntelliJIdea2024.1\tomcat\8bfcd653-8989-4d5a-9f06- 1e8b34e35303\work\Catalina\localhost\Ex2\org\apache\jsp\index_jsp.java] Only a type can be imported. jakarta.servlet.http.Cookie resolves to a package
Надеюсь на вашу помощь
Код:
Сервлет:
package servlets;
import java.io.PrintWriter;
import java.io.IOException;
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 javax.servlet.http.Cookie;
@WebServlet( urlPatterns = "/degree_elements")
public class MassIndexServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8; pageEncoding=UTF-8");
PrintWriter writer = response.getWriter();
String arrayInput = request.getParameter("array");
String[] array = arrayInput.split(",");
String LastIndexArray = null;
try
{
writer.println("<h4>Элементы:</h4>");
for (int i = 0; i < array.length; i++)
{
if ((i & (i - 1)) == 0 && i != 0)
{
writer.println("<li>" + array[i].trim() + "</li>");
LastIndexArray+=array[i].trim()+",";
}
}
}
finally
{ writer.close();
if (LastIndexArray == null)
{
LastIndexArray ="нет";
}
Cookie cookie = new Cookie("LastIndexArray", LastIndexArray);
cookie.setMaxAge(-1);
response.addCookie(cookie);
}
}
}
index.jsp
<%@ page import="jakarta.servlet.http.Cookie" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Пользовательская форма</title>
</head>
<body>
<form action="degree_elements" method="POST">
Введите массив элементов: <input name="array"/>
<br><br>
<input type="submit" value="Отправить" />
</form>
<br><br>
Элементы прошлого запроса:
<ul>
<%
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("LastIndexArray".equals(cookie.getName())) { %>
<li><%= cookie.getValue() %></li>
<%
}
}
}
else {
%>
нет
<%}%>
</ul>
</body>
</html>
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>servlets</groupId>
<artifactId>Ex2</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Ex2</name>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<junit.version>5.9.2</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-web-api</artifactId>
<version>9.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin> </plugins>
</build>
</project>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0">
</web-app>