0

I was trying to populate collection(collection of items) from servlets to my jsp page..

In my servlet code I store the items in collection:

String itemsJson = new Gson().toJson(items);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(itemsJson);

In my jsp page I am tryin to populate the items onto a jquery resizable box.The code is as follows:

$(document).ready(function() {
    $("#resizable").resizable();
    $.getJSON('resizable', function(itemsJson) {
        var $resizable = $('#resizable');
        $.each(itemsJson, function(index, itemcatalog) {
            $('<option>').text(itemcatalog.item).appendTo($resizable);
        });
    });
});

<div class="demo">  
<div id="resizable" class="ui-widget-content">
<h3 class="ui-widget-header">Item List</h3>
 <option></option>

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @Bozho- GET http://localhost:8015/Inventory/resizable 404 Not Found –  May 05 '11 at 18:01
  • Please don't change the entire question into something different. It would make existing answers entirely useless. The 404 is been solved. The NPE is a separate problem and should be asked in a different question. The JSP/jQuery code is then irrelevant. Only the Java code where the NPE occurs and the stacktrace is relevant. I've rolledback the edit. – BalusC May 05 '11 at 19:59

1 Answers1

2

Then the URL is plain wrong. You're using a relative URL in $.getJSON()

$.getJSON('resizable', function(itemsJson) {

Imagine that this code is been placed in a JSP file which is opened by the following URL

http://localhost:8080/context/pages/page.jsp

then you need to ensure that the servlet is available on

http://localhost:8080/context/pages/resizable

JavaScript/jQuery will namely map relative URLs to the document's base URL.


Or when the servlet is actually listening on

http://localhost:8080/context/resizable

(test it by opening its address straight in browser address bar)

then you need to change the $.getJSON() URL as follows

$.getJSON('../resizable', function(itemsJson) {

or (domain-relative)

$.getJSON('/context/resizable', function(itemsJson) {

or (works only if this is inside JSP)

$.getJSON('${pageContext.request.contextPath}/resizable', function(itemsJson) {

or to move the JSP file to

http://localhost:8080/context/page.jsp

Or if the URL is actually correct, then it simply means that the servlet failed to startup. Read the server logs for the exception/stacktrace and fix the code accordingly. Or maybe the servlet is not mapped on an URL at all.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @balusC-But resizable is the div s id..My servlet is GetitemsServlet.. I am not getting it..Its goin above my head –  May 05 '11 at 17:54
  • No, `$.getJSON('resizable')` points to the servlet URL. It's getting JSON from the servlet by the given URL. The div id is in the `$('#resizable')`. It's returning `
    `. I'd suggest to take a break and redo the basic JSP/Servlet/jQuery tutorials individually. This may be a good starting point: http://stackoverflow.com/tags/servlets/info and http://stackoverflow.com/questions/4112686/update-current-page-with-a-servlet
    – BalusC May 05 '11 at 17:55
  • Sorry if the answer looks overwhelming, but since you gave very little detail about the real URLs, a more specific answer can't be given. If you give the real full URL of both the JSP page and the JSON servlet (exactly the URL as you have in the browser address bar when they returns the expected response), then we'll be able to tell you the correct relative URL to the servlet for use in `$.getJSON()`. – BalusC May 05 '11 at 17:59
  • @BalusC- jsp page:getItemList.jsp...Servlet:GetItemListServlet.. the url when the response got is :http://localhost:8015/Inventory/getItemList.jsp –  May 05 '11 at 18:19
  • And the URL where you get the JSON servlet response? Is it http://localhost:8015/Inventory/GetItemListServlet ? If so, then you need to change `$.getJSON()` URL to `'GetItemListServlet'`. – BalusC May 05 '11 at 18:29
  • @BalusC I am not getting GetItemListServlet...All I get is the jsp page url once I run the app...:( Am using post method... –  May 05 '11 at 18:55
  • The job should of course be done in `doGet()`. And the servlet should be mapped on URL pattern of `/GetItemListServlet` by `@WebServlet` or in `web.xml`. This is again evidence that you didn't read the two links at all which I posted in my first comment on this answer. I strongly encourage you to read them. – BalusC May 05 '11 at 18:57
  • @BalusC-I read the above links and made the changes..I have updated the code above...But its throwing Null pointer exception!!! –  May 05 '11 at 19:49
  • That should be an easy fix. What exactly is `null`? Just make sure that it is not `null` at the point you're trying to access/invoke it. The 1st line of the stacktrace should contain information about the class, method and line number where this occurs. – BalusC May 05 '11 at 19:55
  • @BalusC-well it is null at the point I am invoking...The very part where I get the items is throwing null pointer exception –  May 05 '11 at 20:20
  • If you find NPE really hard to understand/fix, start a new question wherein you post the relevant Java code (no JSP, no jQuery, they are not relevant to this problem). Your 404 problem is been fixed. The servlet is now listening on the URL as desired (but has just a programming bug). For NPE fix hints, check this answer: http://stackoverflow.com/questions/2140959/java-error-exception-in-thread-main-java-lang-nullpointerexception/2141013#2141013 – BalusC May 05 '11 at 20:22
  • @BalusC..Got it...The data was not being retrevied from DB..Correct the query..Thanks!!!:):) –  May 06 '11 at 04:12