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

17
  • 1
    You have to store your object in a session. When you reload your JSP your object is instantiaded again with initial state. Commented Jan 10, 2015 at 19:18
  • 3
    Not an answer to your question, but please read: stackoverflow.com/questions/3177733/… Commented Jan 10, 2015 at 19:19
  • @JorgeCampos how can I do this? Could you give me an example? Commented Jan 10, 2015 at 19:23
  • Here are two: tutorialspoint.com/jsp/jsp_session_tracking.htm and jsptut.com/sessions.jsp Commented Jan 10, 2015 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. Commented Jan 10, 2015 at 19:35

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

3 Comments

1+ for the link that discourages scriptlets well, however <%! ... %> is used not for the static members but JSP declaration.
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.
@RomanC Oh, you are right. I don't know why I thought that this field needs to be static. Will edit my answer.
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.

3 Comments

it isnt request.getAttribute("myobject") ? get ?
Sorry maybe I expressed myself wrong. the jsp doesnt interact with the servlet, just uses it as a container for the myobject variable
"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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.