-1

Servlet code:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpSession session = request.getSession();
    Facade f = new Facade();
    //f.hotspots() return a list of object used as marker***
    session.setAttribute("hotspots", f.hotspots());
    prossimaPagina = "/map.jsp";
    ServletContext application = getServletContext();
    RequestDispatcher rd = application.getRequestDispatcher(prossimaPagina);
    rd.forward(request, response);
    return;
}

java script code:

<script type="text/javascript">
var map;
function initialize() {
  var myLatlng = new google.maps.LatLng(10,10);
  var mapOptions = {
    zoom: 4,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
  placeMarker();
}

function placeMarker() {
    var icon = 'icon/hs.png';
    var hotspotList = <%=session.getAttribute("hotspots")%>;
    for ( var h in hotspotList ) {
    var marker = new google.maps.Marker({
        position: (h.getLat(),h.getLng()),
        map: map,
        icon: icon
    });
    }
}
    google.maps.event.addDomListener(window, 'load', initialize);
</script>

I know that <%=session.getAttribute("hotspots")%> expression is wrong, but i'd like to do something like that. How can I achieve this?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Oxenarf
  • 200
  • 6
  • 23
  • you need to revise your question you say jsp while the code is javascript – Muhammad Bekette May 20 '13 at 15:31
  • 2
    JSP generates HTML output. JS is part of HTML output. JSP and JS does not run in sync at all. All you need to do is to write JSP code in such way that it produces exactly the desired HTML output which you can verify by rightclick, *View Soruce* in browser. – BalusC May 20 '13 at 15:33
  • I guess that you can return the list of objects in `hotspot` in JSON format, so it can be easily converted to a JavaScript object and you could use it as provided in your JS code. – Luiggi Mendoza May 20 '13 at 19:10

1 Answers1

-2

Use like this

First set a variable in jsp

<% 
    List<String> param= List<String>session.getAttribute("hotspots");
 %>

Then use like this.

 <script>
    var h = '<%=param%>';
 </script>
NullPointerException
  • 3,732
  • 5
  • 28
  • 62
  • The `List#toString()` outcome of a `List` does not represent a valid JS string array at all. – BalusC May 20 '13 at 15:32
  • @LuiggiMendoza I know that. But if you have to read the java variable in your javascript that is the only way. – NullPointerException May 20 '13 at 15:35
  • 2
    Really? This does in effects exactly the same as `var h = '${hotspots}';`, which is still the wrong answer. Please experiment with it yourself first if you can't reliably answer from top of head. – BalusC May 20 '13 at 15:36
  • There's no right way since it depends on your design, but this approach won't solve the problem either. – Luiggi Mendoza May 20 '13 at 20:19