0

a little back story:

I am working on a file that got to be very large, eventually resulting in the following error:

The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes limit.

In order to get around this, I have been "modularising" the file by utilizing the jsp:include tags. I have successfully passed objects from the main file to the included file by serializing the objects and using the jsp:param tag and then deserializing in the jsp:include file. However, being as though these objects are used, modified and reused and re-modified in the main file and in multiple included files. I am wondering if there is a way to pass back the object to the main file after rendering the included file OR if there is a way to access these objects by reference so that they can be modified within one included file and the right instance of the modified object could be reused other included files below it?

So far I have considered pagecontext.setAttribute() (which does not seem to be by ref and does not seem to let me pass the value back after being modified to the main file) and jsp:param (pretty much the same thing as as the pagecontext.setAttribute()).

This is what I have so far:

Code Sample below: (I hope this doesn't confuse anyone. I'm not looking for syntax corrections, I'm just looking for a solution that will allow the same object to be accessed by reference (such as a global variable along with the include tags) or for me to pass the object back to the main.jsp so that the next jsp:include can access it after it's been modified.

Main.jsp

    //Serialized Objects, Google Gson object
       Gson gson = new Gson();

       String serializedObject = gson.toJson( objectName );


    <jsp:include page="/includes/includedFileName1.jsp">
       <jsp:param name="serializedObject" value="<%= serializedObject %>" />
    </jsp:include>

   <!-- Is there a way to ensure includedFileName2 .jsp gets the modified version of object from includedFileName1.jsp? -->

    <jsp:include page="/includes/includedFileName2.jsp">
       <jsp:param name="serializedObject" value="<%= serializedObject %>" />
    </jsp:include>

   <!-- Is there a way to ensure includedFileName3 .jsp gets the modified version of object from includedFileName2.jsp? -->

    <jsp:include page="/includes/includedFileName3.jsp">
       <jsp:param name="serializedObject" value="<%= serializedObject %>" />
    </jsp:include>

includedFileName1.jsp

    //Gson object used to convert serialized strings to java objects
    Gson gson = new Gson();

    ObjectType object = null;
    if (null != request.getParameter("serializedObject")) {
       object = gson.fromJson(request.getParameter("serializedObject"), ObjectType.class);
    }

    if (x = y) {object = somethingNew1;}

//is there a way to pass object back to the main.jsp?

includedFileName2.jsp

//Gson object used to convert serialized strings to java objects
Gson gson = new Gson();

ObjectType object = null;
if (null != request.getParameter("serializedObject")) {
   object = gson.fromJson(request.getParameter("serializedObject"), ObjectType.class);
}

if (x = y) {object = somethingNew2;}

//is there a way to pass object back to the main.jsp?

includedFileName3.jsp

//Gson object used to convert serialized strings to java objects
Gson gson = new Gson();

ObjectType object = null;
if (null != request.getParameter("serializedObject")) {
   object = gson.fromJson(request.getParameter("serializedObject"), ObjectType.class);
}

if (x = y) {object = somethingNew3;}

//is there a way to pass object back to the main.jsp?

The object may or may not be modified but has to be accessible from all three includes and the objects latest changes has to be accesible.

Thank you for your time.

AlexScript
  • 809
  • 8
  • 18
  • 1
    I do not understand the problem, the object will not change if you access it via `request.getAttribute()` and `request.setAttribute()`. All modifications will be visible during the request. Did I miss anything? – home Aug 23 '11 at 15:04
  • 1
    how did you pass objects using jsp:params tag! I am afraid there doesnt seem to be a direct solution to your problem.. you would have to use request.getAttribute and request.setAttribute and ensure they are consistently handled. If some one has a better solution please let us know.. i would liek to know too – Ash Aug 23 '11 at 15:09
  • I'll try to clarify I am either looking for solution a) main file -> object instantiated -> object passed to jsp:include -> object modified -> object passed back to main file -> object passed to another jsp:include -> object modified -> object passed back to main file .... OR b) main file -> object instantiated -> object referenced in jsp:include -> object modified -> object refernced in another jsp:include -> new modified values are accessible as it was accessed by reference. – AlexScript Aug 23 '11 at 15:19
  • @Ashley: I passed through java objects through jsp:param by serializing the object. I used com.google.gson.Gson as it was extremely easy to implement. – AlexScript Aug 23 '11 at 15:20
  • 1
    You should not really be modifying objects in your jsp files, you should be doing it in you servlets. The point of jsps is to separate content generation from presentation. I would consider rethinking how you are generating your pages. – Andrew Aug 23 '11 at 15:43
  • @Andrew: do you know any good references that say how to implement servlets in this kind of scenario. Will the objects be accessible in all the jsp:includes at that point? Please remember I cannot use <%include tags as this will make the 65535 bytes limit error return. Thank you for your recommendation. – AlexScript Aug 23 '11 at 15:55
  • `<% %>` are evil. How about instead you give us an example of how you are passing objects into your included files and modifying them so we can help fix the incorrect implementation. – Andrew Aug 23 '11 at 15:59
  • @Andrew: I've added the sample code. – AlexScript Aug 23 '11 at 16:26
  • Thank you for all the comments. I was able to resolve my issue with `request.setAttribute()` and `request.getAttribute()` . – AlexScript Aug 23 '11 at 17:17
  • @AlexScript : hehe.. so shud I assume my comment helped you a bit pity it wasn't an answer :p – Ash Aug 24 '11 at 10:15

1 Answers1

0

Thanks @home, @Ashley ...

I re-thought the way this was implemented and resolved my issue using request.setAttribute() and request.getAttribute().

<% 
    //set new parameters to be passed through to the jsp:include
    request.setAttribute("objectName", objectInstance);
%>
    <jsp:include page="/includes/requisitionClinicalConditionContent.jsp"/>
<%
    //get parameters passed from the jsp:include
    objectInstance = (object) request.getAttribute("objectName");
%> 

I pass through the object using request.setAttribute() and receive it in my jsp:include. If it was modified within the jsp:include (which it isn;t in most cases, but I do have 1 or two cases which may modify the value), I return the object from the jsp:iclude using request.setAttribute(0 and recive it back in my parent jsp page.

Thanks for all the help, I'm sorry I didn't reply back sooner.

AlexScript
  • 809
  • 8
  • 18