简体   繁体   English

如何使用JSP页面中的参数在Java类中搜索数据?

[英]How to use parameters from JSP page to search data in Java class?

Here is a screen shot of my project thus far: 到目前为止,这是我的项目的屏幕截图:

在此处输入图片说明

The purpose of this program is too find specific classes from an ArrayList that I have in my servlet. 该程序的目的也是从Servlet中的ArrayList中找到特定的类。 You search for the classes you want from what you have defined on the left (ie a class in X location, and Y semester, and Z teacher). 您可以从左侧定义的课程中搜索所需的课程(例如,X位置,Y学期和Z老师的课程)。 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: 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? 我的问题是,我将如何开始发送​​用户在JSP页面上选择的数据,然后对ArrayList进行排序以找到它? 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. 我知道我可以使用<%// code%>直接将Java代码注入JSP页面,但是由于项目需求,我无法在页面本身的主体中编写脚本,所有脚本都在外部链接。

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. 例如,如果用户在“位置”下选中“ Springfield”,并在学期下说“ Summer”,则应在夏季生成Springfield中所有班级的表格。 I just don't know how/where to start to learn how to send those parameters to search by in the servlet. 我只是不知道如何/从哪里开始学习如何发送这些参数以在servlet中进行搜索。

As per the $.get() documentation , you can pass a JS object as 2nd argument (called data in the documentation). 根据$.get()文档 ,您可以将JS对象作为第二个参数传递(在文档中称为data )。

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. 它们位于HttpServletRequest#getParameter()可用的servlet中,这是通常的方式。

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. 如果实际上是在破坏HTML <form> ,那么还可以使用$.serialize()发送整个表单数据,而不用手动摆弄各个输入元素的松散值。 Here's an example assuming that it's a <form id="search"> : 这是一个假设它是<form id="search">的示例:

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

or 要么

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

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM