Oscar Reynes
Oscar Reynes

Reputation: 123

How do you minify/obfuscate JavaScript code in a JSP that has JSP/JSTL variables mixed into it?

arrays.jsp:

//...
var x = <c:out value="${x}"/>
<c:if test="${empty doExternal}">
processExternalArrays();
</c:if>
//...

I want to minify/obfuscate JavaScript contained in a large JSP file in which numerous JSP/JSTL variables are mixed into the JavaScript code such as in the snippet above.

The code relies on variables populated using server-side logic and then passed to the client-side code, as above.

I'm already minifying my JS files using YUI compressor but I don't know what to do about the JavaScript code in my JSPs.

Is it possible to minify/obfuscate this code, given that it is dynamically created?

Upvotes: 9

Views: 9032

Answers (5)

Andre
Andre

Reputation: 31

Probably the best solution for you would be use Granule JSP tag. You can download it at http://code.google.com/p/granule/

code sample is:

<g:compress>
  <script type="text/javascript" src="common.js"/>
  <script type="text/javascript" src="closure/goog/base.js"/>
  <script>
       goog.require('goog.dom');
       goog.require('goog.date');
       goog.require('goog.ui.DatePicker');
  </script>
  <script type="text/javascript">
      var dp = new goog.ui.DatePicker();
      dp.render(document.getElementById('datepicker'));
  </script>
</g:compress>
...

Upvotes: 3

kschneid
kschneid

Reputation: 5694

If you can't, or don't want to, move your JavaScript out of your HTML, one possibility would be to create a tag handler that wraps the content of your <script> tags:

<script type="text/javascript"><js:compress>
    ...
</js:compress></script>

The handler could probably extend SimpleTagSupport. You'd then have to investigate the Java APIs for compressors/minifiers, like YUI Compressor or dojo ShrinkSafe, and use them to process the tag body.

Edit: Sorry, I skimmed the other answers and it appears that Zack Mulgrew might be referencing a taglib that already does exactly what I'm suggesting...

Edit2: Yup, JavaScriptCompressorTag. Guess I'll have to up-vote his answer ;-)...

Upvotes: 0

Ruan Mendes
Ruan Mendes

Reputation: 92274

Ensure that your output is gzip encoded (apache mod_deflate). Minimizing the html/js first may make it a bit smaller, but not by much.

Upvotes: 0

Zack The Human
Zack The Human

Reputation: 8481

Have you taken a look at htmlcompressor? In short it's a:

Java HTML/XML Compressor is a very small, fast and easy to use library that minifies given HTML or XML source by removing extra whitespaces, comments and other unneeded characters without breaking the content structure.

It's main function is so compress HTML and XML, but it also comes with JSP tags that can be used to compress inline JavaScript blocks by leveraging YUI Compressor. Check out the Google Code page, especially the Compressing selective content in JSP pages section.

Upvotes: 2

BalusC
BalusC

Reputation: 1108692

I don't see other ways than fully delegating the job to pure JS with help of Ajaxical powers in combination with a Servlet which returns the desired information on an Ajax request (in flavor of JSON?).

E.g. in Servlet

Map<String, Object> data = new HashMap<String, Object>();
data.put("doExternal", doExternal);
data.put("x", x);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(new Gson().toJson(data)); // Gson is a Java-JSON converter.

and in JS (with little help of jQuery since it makes the Ajax works less verbose)

$.getJSON('servleturl', function(data) {
    var x = data.x;
    if (!data.doExternal) {
        processExternalArrays();
    }
});

This way you end up with clean JS without server-side specific clutter.

Upvotes: 0

Related Questions