3

I'm working on the project that requires reading database in JSP, but the data is used by javascript to render the google map(the map points). I don't know how to access the NoSQL database by javascript, so I'm thinking about embed JSP in javascript to access the data and as a way to feed them to javascript.

I've searched a lot about these features, I'd like to have code like:

var a = <%=data %>

How could I control the script(since it's .js), and index.jsp ?

Thanks

i3wangyi
  • 2,279
  • 3
  • 15
  • 12
  • 2
    You are thinking about this wrong. Think of these two things your serverside code and your front end code as parallel "things". Your JSP will have your database logic. You can feed it to your javascript since the serverside code triggers first. Or you can set your JSP/DB code to accept asynchronous calls. Were you wait for someone to submit something and without post-back you send it to your serverside code. Separate your concerns here, meaning figure out wait the serverside code needs to do with your DB stuff, then deal with your front end code to interface with it. – Frank Tudor Apr 15 '14 at 14:49
  • You can embed JS in a JSP that returns data. – Harvey A. Ramer Apr 15 '14 at 14:59

1 Answers1

7

Java code runs on server, this means, it runs on your application server and helps to render the view (in this case, the JSP). JavaScript runs on client side, this means, it runs on the client browser (Internet Explorer [ugh], Firefox, Chrome, and on...). So, based from your current code:

var a = <%=data %>;

Assuming data is a String and has a value of "Hello World", the generated HTML/JS would be:

var a = Hello World;

Which will produce an error. So, you would need to quote the variable:

var a = '<%=data %>';

Now this will produce:

var a = 'Hello World';

For more complex communication, like passing a list or a complex structure or a list of complex structures from server to client, it would be better using a common format to exchange data like JSON. You can marshall the data to JSON string from server (preferably in a Servlet) and then pass it to JavaScript side easily. For example, using Jackson Library for Java:

@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
        List<ComplexObject> complexObjectList = ... //get your data
        request.setAttribute("complexObjectList", OBJECT_MAPPER.writeValueAsString(complexObjectList));
        //forward to the desired view...
        request.getRequestDispatcher("/WEB-INF/theView.jsp").forward(request, response);
    }
}

Then in your JSP (using Expression Language because you should avoid using scriptlets):

<script type="text/javascript">
    var complexObjectList = JSON.parse('${complexObjectList}');
    //play with your new object in JavaScript side...
</script>
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • thanks, using json is a good choice however will it be a big overhead for reading data from database then write them to json, read by javascript again? – i3wangyi Apr 15 '14 at 15:18
  • @user3307896 it is not so much overhead. Try it first, then measure it. – Luiggi Mendoza Apr 15 '14 at 15:18
  • I used to see lot of SO qns trying to club JSP and JS. Well explained, If I come across next similar qn, will route to this explanation. Good. +1 – spiderman Apr 15 '14 at 15:29