The object is suddenly missing from the HttpServletRequest

I print the list directly in the servlet using a printing device and print the list.

When I try to insert jsp, the list does not print whether I use JSTL or scriptlets.

I tried to test the JSTL and scriptlet if the object is null and it turns out that it is!

Why is this happening and how can I fix it?

Servlet code that works

for (Artist artist:artists){ resp.getWriter().println(artist.getName()); } 

The code for the servlet that places the object in the request

 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { ApplicationContext ctx = new ClassPathXmlApplicationContext("com/helloworld/beans/helloworld-context.xml"); ArtistDao artistDao = (ArtistDao) ctx.getBean("artistDao"); List<Artist> artists = null; try { artists = artistDao.getAll(); } catch (SQLException e) { e.printStackTrace(); } req.setAttribute("artists", artists); try { req.getRequestDispatcher("index.jsp").forward(req, resp); } catch (ServletException e) { e.printStackTrace(); } 

script code that suddenly finds a null object

 <% List<Artist> artists = (List<Artist>) request.getAttribute("artists"); if (artists == null) { out.println("artists null"); } else { for (Artist artist: artists){ out.println(artist.getName()); } } %> 

Even jstl code seems to agree

 <c:if test="${artists eq null}"> Artists are null </c:if> <c:forEach var="artist" items="${artists}"> ${artist.name} </c:forEach> 

For my application, I use weblogic, spring 2.5.6 and ibatis.

+4
source share
3 answers

I just discovered inadvertently while trying to fix my directory structure in WebContent /

My previous directory structure was

WEB-CONTENT /
- META-INF /
- WEB-INF /
index.jsp

Then I tried to create a jsp folder in WEB-CONTENT and placed index.jsp there. He works!

My current directory structure is now

WEB-CONTENT /
- META-INF /
- WEB-INF /
- jsp /
-index.jsp

I do not know why this works, but it is.

Can anyone here think why?

0
source

Perhaps the application server will reset your request object. You can work around this by creating a new request object that wraps your initial request and passes it to the reqest dispatcher.

eg. MyHttpRequest myRequest = new MyHttpRequest (req); myRequest.setAttribute (...); req.getRequestDispatcher ("index.jsp"). forward (myRequest, resp);

And the code MyHttpReqest:

  class MyHttpRequest extends HttpServletRequestWrapper { Map attributes = new HashMap(); MyHttpRequest(HttpRequest original) { super(original); } @Override public void setAttribute(Object key, Object value) { attributes.put(key, value); } public Object getAttribute(Object key) { Object value = attributes.get(key); if (value==null) value = super.getAttribute(key); return value; } // similar for removeAttribute } 
+1
source

I think it depends on the web server. But without changing the previous directory structure

try putting a list in a session like this

 req.getSession(false).setAttribute("artists", artists); 

and in your jsp,

to write

 List<Artist> artists = (List<Artist>) request.getSession(false).getAttribute("artists"); 

I think my approach will work for all web servers.

+1
source

Source: https://habr.com/ru/post/1309955/


All Articles