1

I want when my page such as index.jsp loading(calling get method) send data for index.jsp page such as title. I checked this question but this question solution(How to call servlet on jsp page load) has error in my web application.

This my index.jsp page code(I want send title from changeTitle.java class):

<title><c:import url="/sysAdmin/changeTitle.java" /> 
<c:out value="${message }"></c:out></title>

This my changeTitle.java class get method:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setAttribute("message", "hello");
}

And I don't know how addressing to class in index.jsp of my project and bellow is my project directories:

enter image description here

Community
  • 1
  • 1
Farshid Shekari
  • 2,391
  • 4
  • 27
  • 47
  • "*has error in my web application.*" What is that? The request attribute `message` would not be available in the target resources (the JSP page), if you redirected the request instead of forwarding/dispatching it. What is the rest of the code in the `doGet()` method? – Tiny Dec 20 '14 at 17:06
  • I want this page every time load, title set from sent message from class – Farshid Shekari Dec 20 '14 at 17:09
  • Taken. How do you reach the JSP page from your Servlet class (and how is your Servlet mapped to the JSP page)? Do you redirect the request to the JSP page or dispatch/forward the request to it (or you do nothing)? – Tiny Dec 20 '14 at 17:14
  • I do nothing!! and I want just send message! – Farshid Shekari Dec 20 '14 at 17:19
  • Really? It is hard to believe that you are doing nothing. Anyway you need to dispatch the request to the target JSP page such as `request.getRequestDispatcher("/path/to/jsp/index.jsp").forward(request, response);` (it is basically the last line in your `doGet()` method). Consider changing the path according to the JSP page location. Make sure that you appropriately mapped the Servlet to your desired JSP page. – Tiny Dec 20 '14 at 17:27

2 Answers2

0

Try setting the response, not the request

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setAttribute("message", "hello");
}
0

Try using this code:

<c:out value="<%=request.getAttribute('message')%>">
trejder
  • 17,148
  • 27
  • 124
  • 216
Renny
  • 114
  • 10