How to safely remove javascript from a jsp file to a separate js file?

Hi All,

I have to add more features to an existing jsp file.
But its so huge in size that _jspService method code is breachig 65kb limit and jsp is not getttin complied ,if I add more  code to it.
This jsp file is around 7 years old and involves heavy usage of javascript with in the same jsp file. There are lots of references of jsp scriplet variables in the javascript code.  So even if i try to move all the javascript to a separate file and use jsp:include then javascript functions are not able to find jsp scriplet variables in the scope. If I also move the jsp scriplets then those variables are not availbale to html elements on the original page.

So, how can i move all the javascript code to a separtae file safely and overcome this problem of 65kb limit??

Thanks in advance for any suggestions
PACGDevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
remove all the javascript functions into a file called myFunctions.js
Then on your jsp, you would need to import the file by sending the following to the browser:
<script type="text/javascript" src="myFunction.js"></script>

NOTE: make sure you supply the correct path to your myFunctions.js field for the src attribute.
0
PACGDevAuthor Commented:
@ Wizard:

Thanks for such an early suggestion.
I already tried that before but javascript also has embeded jsp scriplets in it. So it doesnt work.
0
Michel PlungjanIT ExpertCommented:
You need to do the following

1. take all vars set by jsp and assign them before the include:

<html>
<head>
<script>
var someJspVar1 = "<%= someJSPString1 %>";
var someJspVar2 = "<%= someJSPString2 %>";
var someJspVar3 = "<%= someJSPString3 %>";
</script>
<script src="js_stripped_of_jspvars.js"></script>


ALTERNATIVELY copy all the javascript to a jsp of its own:

<script src="jswritten_by_jsp.jsp?parm=1&parm2=bla" type="text/javascript"></script>

in that jsp, you set the content-type to text/javascript and that is it.

So before you had


<%@ page language="java" contentType="text/html" %>
<% some jsp stuff %>
<script>
<% some jsp stuff %>
var someJsvar = "<%= bla %>";
<% some jsp stuff %>
</script>
<% some jsp stuff %>

now you have
<%@ page language="java" contentType="text/html" %>
<% some jsp stuff %>
<script src="jswritten_by_jsp.jsp?parm=1&parm2=bla" type="text/javascript"></script>
<% some jsp stuff %>


and in the other

<%@ page language="java" contentType="text/javascript" %>
<% some jsp stuff %>
var someJsvar = "<%= bla %>";
<% some jsp stuff %>


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

hieloCommented:
>>@ Wizard:
Wizard is just my Rank. There lots of Wizards on EE :)

>>but javascript also has embeded jsp scriplets in it.
This important detail would have helped from the beginning!  There is not easy way to "fix" this. Based on your answer, it sounds like you have dynamically generated scripts within functions. Simply putting the dynamically generated javascript outside of functions (ie. at global scope), may have the unfortunate side effect of overriding code from other functions. So you really must check the dynamically generated javascript code manually. The "process" I described on my previous post will work for functions which do not have dynamically generated code.

On another note, if you decide to put the dynamically generated code outside the functions at global scope, then you need to include the javascript files after you declare this. Otherwise the functions that use these global variables will not "see" any global variable declared after the function definition.
0
Michel PlungjanIT ExpertCommented:
@Hielo - it was mentioned:
"There are lots of references of jsp scriplet variables in the javascript code.  So even if i try to move all the javascript to a separate file and use jsp:include then javascript functions are not able to find jsp scriplet variables in the scope"

and you are repeating what I said.
0
PACGDevAuthor Commented:
@mplungjan:

Thanks for the reply.
In your suggestion, I am not getting about what parameters (such as ?parm=1&parm2=bla") are you talking? Is it the way to pass jsp variable values  to the newly created jsp (having all javascript)?
Let me add here, the javascript to be moved is around 2500 lines...and almost every function have scriplet embeded in it..its like based on values of some jsp scriplet variables javascript function is getting generated dynamically....So I guess if you are talking abt passing all those variables as parameters to newly created jsp then it will be a quiet a task.

And if i move all the javascript to separate jsp then what willl happen to html element references from the javascript.. for eg: document.form1.<%field%>.value = '2'....will this still work properly because the html elements referred here are present in another (the original) jsp.
0
Michel PlungjanIT ExpertCommented:
Yes you can pass parameters:
<script src="jswritten_by_jsp.jsp?parm=1&parm2=bla" type="text/javascript"></script>

or

<script src="jswritten_by_jsp.jsp?parm=<%= parm1 %>&parm2=<%= parm2 %>" type="text/javascript"></script>

It sounds in general and from your code using document.form1.<%field%>.value

that your jsp and javascript could do with a general  cleanup - perhaps even a complete rewrite!
0
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.