views:

200

answers:

2

I have entries in a text file which I want to load into a drop down box.

I want this to happen on loading a jsp page. I could easily do this if I had a button that triggered the passing of a request object to the servlet. How do I send a request object on page load - do I need to use Javascript or is this something I can do with just jps.

+1  A: 

Where you can populate it on the serverside. By that I mean that when you create the select box on your JSP, populate it then. For example:

<select id="sel1" name="animal">
<c:forEach var="animal" items="${model.animals}">
<option value="<c:out value="${animal.id}"/><c:out value="${animal.name}"/></option>
</c:forEach>
</select>

If that isn't possible, practical or desired, you'll need to use some form of AJAX method. Personally I use jQuery for this. For example:

<select id="sel1" name="animal">
</select>

<script type="text/javascript">
$(function() {
  $.get('/server/getanimals', function(data, textStatus) {
    var sel = $("#sel1");
    for (var i=0; i<data.length; i++) {
      sel.append('<option value="' + data[i].id + '">' + data[i].name + '</option>');
    }
  }, 'json');
});
</script>

The above calls /server/getanimals on page ready. It is expecting to be returned a JSON object with a list of animals that it then uses to populate the select box.

There are lots of ways to skin this particular cat.

cletus
Thanks, do you have any good jQuery references for this, I will do some Googling but if you can help that will be much appreciated.
Ankur
What Java Web framework are you using?
cletus
Not using any framework because I don't know how to - I am learning Spring - but in the meantime I am just using jsp pages called from a servlet. When I get stuck I write to a session object.
Ankur
If you're learning Spring, you might want to take a look at Spring MVC but learning the fundamentals of servlets doesn't hurt either.
cletus
This might sound a really simple question, but is it an either or choice between Spring and Servlets, or does Spring tie servlets together.
Ankur
Spring and servlets are complementary. Spring MVC is a Web MVC framework built on top of Java servlets (most if not all Java Web frameworks are ultimately built upon servlets). Spring can be used in desktop apps, Web apps and probably any kind of app really.
cletus
A: 

You can use Servlet's doGet() method to preprocess data on GET requests. The doPost() is to be used to postprocess data on POST requests (when you submit the form).

Example:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    List<Option> options = optionDAO.list();
    request.setAttribute("options", options);
    request.getRequestDispatcher("page.jsp").forward(request, response);
}

Where the Option class is just your own javabean class containing the properties value and label. You can also use a List<Map<String, String>> instead if you like.

In JSP you can use the JSTL c:forEach to iterate over a list

<select name="foo">
    <c:forEach items="${options}" var="option">
        <option value="${option.value}">${option.label}</option>
    </c:forEach>
</select>

Finally map the servlet in web.xml and invoke the request so that it matches its url-pattern, e.g. http://example.com/contextroot/page.

BalusC