Actually the question in the subject. Below is the Servlet and JSP code.

Servlet

 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; @WebServlet("/") public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; public TestServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setAttribute("name", "test"); getServletContext().getRequestDispatcher("index.jsp").forward(request, response); } } 

Jsp

 <?xml version="1.0" encoding="ISO-8859-1" ?> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>http</title> </head> <body> <% String name = (String) request.getAttribute("name"); %> <h1> Hello <%out.print(name); %> </h1> </body> </html> 
  • why String name = (String) request.getAttribute("name"); and just <h1> Hello ${name} </h1> not tried? - Saidolim
  • And try using jstl, and output via <c: out value = "$ {name}" /> without first requesting.getAttribute (...); - carapuz
  • The problem is that it outputs null. - vAleksashin
  • @vAleksashin, I have such a structure works fine. Are you sure this jsp is the one that the servlet returns? Try erasing the jsp interior and launch the application. You need to make sure that the servlet returns this particular file. - LEQADA

1 answer 1

in jsp try so

 <?xml version="1.0" encoding="ISO-8859-1" ?> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>http</title> </head> <body> <% // String name = (String) request.getAttribute("name"); String name = (String) session.getAttribute("name"); %> <!-- <h1> Hello <%out.print(name); %> </h1> --> <h1> Hello <%=name %> </h1> </body> </html>