1

I have a class

public class MyServlet extends HttpServlet {

...

       private static MyObject myobject;

       public static MyObject getMyObject(){
           return myobject;
       }
}

and a jsp in which I have

<%! MyObject my_jsp_object = MyServlet.getMyObject();%>

myobject has an initial status, which I can modify and save.

The problems occur when I modify myobject ON THE SERVER. I would expect that, if I modify myobject and then reload the jsp, my_jsp_object is modified too, but it isn't.

Where am I wrong? How can I obtain this behaviour? Thanks

EDIT

For the sake of clarity, this is the behaviour I would like to obtain

  1. Start the server
  2. Get the jsp with the initial value of myobject stored in my_jsp_object
  3. Someone somewhere someway modify myobject
  4. When i reload the jsp, I have the new value of myobject stored in my_jsp_object

Until now, when i reload the jsp, I still have the old value of myobject stored in my_jsp_object

ssnape
  • 37
  • 9
  • 1
    You have to store your object in a session. When you reload your JSP your object is instantiaded again with initial state. – Jorge Campos Jan 10 '15 at 19:18
  • 3
    Not an answer to your question, but please read: http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files?rq=1 – Matt Ball Jan 10 '15 at 19:19
  • @JorgeCampos how can I do this? Could you give me an example? – ssnape Jan 10 '15 at 19:23
  • Here are two: http://www.tutorialspoint.com/jsp/jsp_session_tracking.htm and http://www.jsptut.com/sessions.jsp – Jorge Campos Jan 10 '15 at 19:26
  • If you want to save the state of an object you should either use the session, cookie or another persistent storage such as a DB. – Nir Alfasi Jan 10 '15 at 19:35
  • @JorgeCampos I'm sorry, maybe I expressed myself wrong. When I said that i can modify myobject, I meant to say that I can modify it on the server. Once modified, I would like, after reloading the jsp, to obtain the new value of myobject – ssnape Jan 10 '15 at 19:36
  • `MyObject my_jsp_object = MyServlet.getMyObject();` doesn't seem right. To be able to invoke `getMyObject()` on `MyServlet` class this method would also have to be `static`. – Pshemo Jan 10 '15 at 20:06
  • Anyway your scenario seems to be working fine for me. http://pastebin.com/FgANuBYc – Pshemo Jan 10 '15 at 20:31
  • @Pshemo yes the method is static. Anyway your scenario is not the same of mine. You modify the value of my_jsp_object and, in the same page, you retrieve it. I said that, if the value of myobject is modified on the server, when i reload the jsp that has no effect on client side, which still gets the original value of myobject. Only restarting the server the client seems able to obtain the new value. – ssnape Jan 10 '15 at 20:54
  • I am not sure where is the difference. "*if the value of myobject is modified on the server, when i reload the jsp*" that is what happens in my example. Also "*You modify the value of my_jsp_object and, in the same page, you retrieve it*" yes, but my object also contains older values, not only the new one, so it seems to be what you are trying to accomplish. – Pshemo Jan 10 '15 at 21:06
  • Please update your question with more details and simple but complete code which will let us reproduce your problem. – Pshemo Jan 10 '15 at 21:07
  • "*3. Someone somewhere someway **modify myobject***" is not clear, do you mean change state of already assigned object or assign new instance to `myobject`? Do you mean something like `myObject.setName("someOtherName")` or `myobject = new MyClass()`? In most cases *modify* means *change state*. If you want to use `myobject = new MyClass()` then you are describing *reassigning*. – Pshemo Jan 10 '15 at 21:27
  • @Pshemo In your example you copy list in my_jsp_list, and then do something with this copy in the jsp. In my scenario first I copy myobject in my_jps_object, then myobject is modified someway, but when i reload the jsp I have the old value of myobject stored in my_jsp_object. Anyway I've updated my question. – ssnape Jan 10 '15 at 21:28
  • @Pshemo is there any difference? Anyway I do something like `myObject.setName("someOtherName")` – ssnape Jan 10 '15 at 21:29
  • "is there any difference" yes, very big one. Static fields (and `<%! ... %>` defines *static* members of JSP) are initialized only once, when class is loaded. In that case maybe you are searching for simple `<% MyObject my_jsp_object = MyServlet.getMyObject();%>` (without `!`) which means that it will be executed in `service()` method of your JSP each time it will be requested to browser. – Pshemo Jan 10 '15 at 21:36
  • @Pshemo Thank you very much that was the problem – ssnape Jan 10 '15 at 21:51
  • In that case I will try to post an answer with more explanation. – Pshemo Jan 10 '15 at 21:54

2 Answers2

1

You need to know that

  • <%! ... %> is used to create member (field/method) in servlet which will be generated from JSP code (and executed whenever your jps page will be requested).
  • in most cases servers create one instance of servlet, which means that code in <%! ... %> will be executed only once (in constructor), and you will reuse same instance to handle many requests.

What you seem to need is code which will execute MyServlet.getMyObject(); each time your jps will be requested. In that case you want to make sure that this code will be executed in service() method, which means you need to use <% ... %> (notice lack of !).

But best choice is to avoid Java code in JSP. For more informations read: How to avoid Java code in JSP files?

Community
  • 1
  • 1
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 1+ for the link that discourages scriptlets well, however `<%! ... %>` is used not for the *static* members but JSP declaration. – Roman C Jan 18 '15 at 12:06
  • In the first code you have declared `counter` that is non-static why it should become static in servlet? I think it's your mistake. – Roman C Jan 18 '15 at 12:51
  • @RomanC Oh, you are right. I don't know why I thought that this field needs to be static. Will edit my answer. – Pshemo Jan 18 '15 at 12:58
0

JSP is client technology and Servlet is server side technology. You can not communicate with this technologies in this way.

User RequestDispatcher to send data from Servlet to JSP.

RequestDispatcher rd = getServletContext().getRequestDispatcher("/path/to/page.jsp");
request.setAttribute("myobject", myobject); rd.forward(request, response);

In JSP use request.getAttribute("myobject") method to get your object.

 <% MyObject myobject = request.getAttribute("myobject") %>

you can also use cookies, session.

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
  • it isnt request.getAttribute("myobject") ? get ? – Jorge Campos Jan 10 '15 at 19:55
  • Sorry maybe I expressed myself wrong. the jsp doesnt interact with the servlet, just uses it as a container for the myobject variable – ssnape Jan 10 '15 at 20:57
  • "*JSP is client technology and Servlet is server side technology*" no both technologies are Server side technologies, but they have different tasks. JSP should be responsible for generating output, servlets for providing making sure that JSP will have all data required. That is why instead of simply *redirecting* client to JSP, we dispatch request for different part of server. – Pshemo Jan 10 '15 at 21:47