5

Background:

I have a servlet in which I am dynamically generating javascript and putting into a variable script. Then I set the response content type as text/javascript and send the script over to the client:

resp.setContentType("text/javascript");
resp.getWriter().println(script);

Problem:

The browser does download the javascript file but it doesn't recognize the functions inside the file. If I create a static javascript file and use it instead, it works fine.

Question:

What should be done so that browser treats response from the servlet as a regular javascript file?

Thank you for help.

10
  • 1
    It should work fine. How exactly are you including it? Does the response look fine if you request it directly by entering servlet's URL in browser address bar? Commented May 27, 2011 at 18:23
  • It sounds like your problem might be on the client side, how are you including the script? Commented May 27, 2011 at 18:29
  • Is the response valid JavaScript? Check your browser error log; I tend to have unit tests that parse generated scripts with Rhino just as a sanity check. Is that Content-Type header being received by clients? I've had situations where resource managers/VIPs/reverse proxies would rewrite my carefully crafted headers and inject all sorts of rule-based junk in them. If so, try ending the servlet mapping with .js. Mentioning the browser, servlet container, etc. might help. Commented May 27, 2011 at 18:32
  • @Nicklas Here's how I am including the script: <script type="text/javascript" src="localhost:8888/my-generated-script"></script> Commented May 27, 2011 at 18:33
  • @BalusC Yes it looks fine. Only problem is that everything appears in one line without any line breaks. Commented May 27, 2011 at 18:34

4 Answers 4

6

It should work fine. I suspect that you're just including it the wrong way or calling the function too early or that the response content is malformed.

I just did a quick test:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>SO question 6156155</title>
        <script src="javaScriptServlet"></script>
        <script>test()</script>
    </head>
    <body>
    </body>
</html>

with

@WebServlet(urlPatterns={"/javaScriptServlet"})
public class JavaScriptServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/javascript");
        response.getWriter().write("function test() { alert('peek-a-boo'); }");
    }

}

and I get

Screenshot of browser alert showing "peak-a-boo" message in it.

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

3 Comments

Thanks a lot once again, @BalusC!
When I am using a simple alert as you did then it is working but when I write large javascript code then it is not working. Please help me.
@snj: if you have a new question, just press Ask Question button on right top.
1

How do you refer to this servlet from your browser ?

If you want to include this with a HTML page (existing one), you should refer to it from the tag of your page.

Ex.

<html>
<head>
<script type='text/javascript' src='URL_TO_YOUR_SERVLET'></script>
</head>
</html>

Or if you want it to be executed as part of a Ajax call, just pass the response to eval function.

Or else, if you just want to send the output and get it executed in browser, you need to send the HTML segment as well. Then include your JS with in the body tags, as a script tag.

ex. Your servlet sends the following, using content type 'text/html' :

<html>
<body>
 <script type='text/javascript'>
     <!-- write your generated JS here -->
 </script>
</body>
</html>

Comments

0

You could always write the script 'in-line' to the web page.

Comments

0

I think this way is better.

<%@ page language="java" contentType="text/javascript; charset=UTF-8" pageEncoding="UTF-8"%>
alert('Pure JavaScript right here!');

Set content type in JSP:

contentType="text/javascript; charset=UTF-8"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.