admin管理员组

文章数量:1279188

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

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

Share Improve this question asked Apr 15, 2014 at 14:44 i3wangyii3wangyi 2,4193 gold badges17 silver badges12 bronze badges 2
  • 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 Commented Apr 15, 2014 at 14:49
  • You can embed JS in a JSP that returns data. – Harvey A. Ramer Commented Apr 15, 2014 at 14:59
Add a ment  | 

1 Answer 1

Reset to default 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 plex munication, like passing a list or a plex structure or a list of plex structures from server to client, it would be better using a mon 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> plexObjectList = ... //get your data
        request.setAttribute("plexObjectList", OBJECT_MAPPER.writeValueAsString(plexObjectList));
        //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 plexObjectList = JSON.parse('${plexObjectList}');
    //play with your new object in JavaScript side...
</script>

本文标签: htmlHow to embed JSP in javascriptStack Overflow