1

Here is a screen shot of my project thus far:

enter image description here

The purpose of this program is too find specific classes from an ArrayList that I have in my servlet. You search for the classes you want from what you have defined on the left (i.e. a class in X location, and Y semester, and Z teacher). So far using this code, I can retrieve every single class as displayed in the screen shot:

Scripting:

//Serves up the data THIS IS THE WHAT GETS THE TABLE DATA
$('#btnData').click(function() {

    $("#searchAnimation").fadeTo(0,1, function(){
        $.get('daoServlet', function(responseText) {
            $('#dataDisp').html(responseText);
        }).complete(function(){$("#searchAnimation").fadeTo(0,0);});
    });

});

Java:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
{
    //String that gets returned as HTML
    StringBuilder returnAsHTML = new StringBuilder();

    //See if the class is closed, has a lab, or is just a regular class
    for(ClassInfo classes : allClassListings)
    {
        //Class is full, style accordingly
        if(classes.getSectionMeetingInfo().contentEquals("LEC") && classes.getSectionEnrolled().contentEquals("0"))
        {
            returnAsHTML.append(closedClass(classes));
        }
        else if(classes.getSectionMeetingInfo().contentEquals("LAB")) //These are labs, style accordingly
        {
            returnAsHTML.append(labClass(classes));
        }
        else //These are normal classes without lab components
        {
            returnAsHTML.append(openClass(classes));
        }

    }

    response.setContentType("text/html");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(returnAsHTML.toString());
}

My question is this, how would I begin to send the data the user has selected on the JSP page, and then sort through the ArrayList to find it? I know I can straight up inject java code into the JSP page using <% //code %>, but due to project requirements I cannot have scripting in the body of the page itself, its all linked externally.

For instance, if a user has checked "Springfield" under Location, and say "Summer" under semester, it should produce a table of all the classes in Springfield during summer. I just don't know how/where to start to learn how to send those parameters to search by in the servlet.

Jason Renaldo
  • 2,802
  • 3
  • 37
  • 48

1 Answers1

1

As per the $.get() documentation, you can pass a JS object as 2nd argument (called data in the documentation).

So, the following example should do it for you:

// Do your thing to prepare them.
var param1 = "value1";
var param2 = "value2";

// Now send it as JS object.
$.get('daoServlet', { param1: param1, param2: param2 }, function(responseText) {
    // ...
});

They are in the servlet available by HttpServletRequest#getParameter() the usual way.

String param1 = request.getParameter("param1"); // value1
String param2 = request.getParameter("param2"); // value2

If you're actually ajaxifying a HTML <form>, then you can also use $.serialize() to send the entire form data instead of manually fiddling with loose values of individual input elements. Here's an example assuming that it's a <form id="search">:

$.get('daoServlet', $('#search').serialize(), function(responseText) {
    // ...
});

or

$('#search').submit(function() {
    $.get('daoServlet', $(this).serialize(), function(responseText) {
            // ...
    });
});

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • This is great information! Thanks! I just needed to know where to even start to be able to communicate between the two. – Jason Renaldo Jan 22 '13 at 18:59
  • That "Java class" is called a servlet. Using this term in your query would have yielded more positive hits. – BalusC Jan 22 '13 at 19:01
  • Thanks again for the proper terminology, I'm young in my career and have a lot to learn and appreciate observations such as these. – Jason Renaldo Jan 22 '13 at 19:05