Home > Mobile >  Servlets can't get attribute at jsp
Servlets can't get attribute at jsp

Time:02-27

I am making simple hello world jsp application and I can't get request attribute either by jsp or scriptlets in jsp Here is servlet class


@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setAttribute("test", 123);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
}

Here is index.jsp:

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>JSP - Hello World</title>
</head>
<body>
<h1>Hello world</h1>
<h2>attribute is: ${test}</h2>
<br/>
</body>
</html>

I am getting just attribute is: . It is empty. I tried also doing ${param.test} and other but nothing worked

CodePudding user response:

So far you do not access the request attribute.

Try request.getAttribute("test")

Here is an example that sets the attribute in one JSP and uses it in another: https://developpaper.com/jsp-request-setattribute-in-detail-and-an-example/

  • Related