Pregunta

I'm using Eclipse (Java EE).

I have a Servlet (testServlet.java) under the "Java Resources" > "Logic" package in my Project Explorer.

I have a JSP page (sidebar.jsp) under WebContent. This sidebar is part of my web template, which means it is visible on every page of my web application.

What I'm trying do is to display data from the database in my sidebar jsp page, which reloads every time I move to another jsp page.

The question is - how would I do a

PrintWriter writer = response.getWriter();
writer.write("display this data on JSP page");

in the servlet and display it in the JSP page?

I understand that this is a bad practice but I'm wondering if it is possible. (Note: I am not using any framework either)

Right now, I'm doing something like this in my JSP page:

<jsp:include page="/testServlet" />

which gives the following message when I hover over the code: "Fragment /testServlet was not found at the expected path /AppName/WebContent/testServlet"

Could someone let me know how I can access my servlets which are not in my WebContent folder?

¿Fue útil?

Solución

Add the servlet mapping to web.xml under WEB-INF.

<webapp>
  <servlet>
    <servlet-name>testServlet</servlet-name>
    <servlet-class>Logic.testServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>testServlet</servlet-name>
    <url-pattern>/testServlet</url-pattern>
  </servlet-mapping>
</web-app>

Doing this helps the JSP page to "locate" the servlet.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top