dimanche 28 juillet 2019

Java Servlet reflection gridlock

I've been working with Java servlets and noticed something peculiar. To my knowledge, servlet communication with an associated .jsp file is established via reflection. Reflection is also leveraged for value-retrieval from the server side.

With that in mind, I came across an interesting problem in the following code:

protected void doPost(HttpServletRequest request, HttpServletResponse 
    response) throws ServletException, IOException {

    response.setContentType("text/html");

    String url = "/Display.jsp";

    CSVFileOperations csvfo = new CSVFileOperations();

    String header = csvfo.getHeaders().remove();
    System.out.println(header);

    request.setAttribute("header", header);

    request.getServletContext().getRequestDispatcher(url).forward(request, 
    response);

In particular, this line:

request.setAttribute("header", header);

I set both the String identifier and the variable name as the same. When I call this variable in my .jsp file via ${header}, I get the following garbage output:

{accept-language=en-US, ua-cpu=AMD64, 
cookie=JSESSIONID=1E0C2784352A46D6EFDE0F8A522F4, host=localhost:8080, 
connection=Keep-Alive, cache-control=no-cache, accept-encoding=gzip, 
deflate, accept=image/gif, image/jpeg, image/pjpeg, application/x-ms- 
application, application/xaml+xml, application/x-ms-xbap, */*, user- 
agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64; Trident/7.0; rv:11.0) like 
Gecko}

However, when I change the String identifier from "header" to "head" and call ${head} in the .jsp page, I get the expected output.

My question is, what is going on here?





Aucun commentaire:

Enregistrer un commentaire