0

I'm trying to pass a object of a specific class from my Servlet to a JSP-File. In the JSP-File i want to cast this object to his specific class (because request.getAttribute("car") returns only a object from type Object. But at this point i get a "Car cannot be resolved to a type"-Error.

(I don't know if there is maybe a betther or modern way to do this, because I'm new to Servlets/JSP and want to lern the basics).

Here is my code

@WebServlet(
    name = "Test",
    urlPatterns = {"/test"}
)
public class Test extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) {

    try {
        Car car = new Car();
        car.company = "BWMW";
        request.setAttribute("car1", car);
        request.getRequestDispatcher("test.jsp").forward(request, response);

    } catch (ServletException | IOException e) {
        e.printStackTrace();
    }



}

}

test.jsp:

<!DOCTYPE html>
<html>
<body>
    <% Car car = (Car) request.getAttribute("car");
        out.println(car.company); 
        %>
 </body>
 </html>
user3057184
  • 133
  • 1
  • 1
  • 8

0 Answers0