如何使用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:
该程序的目的也是从我在Servlet中拥有的ArrayList中找到特定的类.您可以从左侧定义的课程中搜索所需的课程(例如,位于X位置,Y学期和Z老师的课程).到目前为止,使用此代码,我可以检索屏幕截图中显示的每个单个类:
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:
脚本:
//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());
}
我的问题是,我将如何开始发送用户在JSP页面上选择的数据,然后对ArrayList进行排序以找到它?我知道我可以使用<%//code%>直接将Java代码注入JSP页面,但是由于项目要求,我无法在页面主体中编写脚本,而脚本主体都是外部链接的.
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.
例如,如果用户在位置"下选中"Springfield",并在学期下说"Summer",则应在夏季生成Springfield中所有班级的表格. 我只是不知道如何/从哪里开始学习如何发送这些参数以在servlet中进行搜索.
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.
推荐答案
根据 $.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) {
// ...
});
它们通常通过HttpServletRequest#getParameter()
在servlet中.
They are in the servlet available by HttpServletRequest#getParameter()
the usual way.
String param1 = request.getParameter("param1"); // value1
String param2 = request.getParameter("param2"); // value2
如果您实际上是在破坏HTML <form>
,那么您也可以使用 $.serialize()
发送整个表单数据,而不是手动摆弄各个输入元素的松散值.这是一个假设它为<form id="search">
:
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) {
// ...
});
或
$('#search').submit(function() {
$.get('daoServlet', $(this).serialize(), function(responseText) {
// ...
});
});
另请参见:
- 如何使用Servlet和Ajax?
- How to use Servlets and Ajax?
See also:
这篇关于如何使用JSP页面中的参数在Java类中搜索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!