Reputation: 1
My servelet.
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter( "action" );
String forward ="";
if( action.equalsIgnoreCase( "search" ) ) {
forward = FIND_COURSE;
String coursename = request.getParameter("coursename");
dao.findCourse(coursename);
}
RequestDispatcher view = request.getRequestDispatcher( forward );
view.forward(request, response);
}
The DAO has the prepared statements etc.
How can I call this in a jsp?
This is my attempt
<form action="search" method="GET">
<input type="text" name="coursename" />
<c:forEach var="course" items="${courses}">
<td><a href="${pageContext.request.contextPath}/FindCourse?coursename=${course.coursename}">${course.coursename}</a></td>
Upvotes: 0
Views: 43
Reputation: 2119
Since you never showed us how you defined the courses variable, I'll just assume it's defined as (just for the sake of demonstration)
public class Course{
public String coursename;//just to match your variable
public int numOfStudents; //students attending a particular course
}
List<Course> coursesList;
Provided that your JDBC code retrieves all the courses and stores them in a List object, you still need to add that list to the current request as either an attribute or a session (read up on JSP scoped objects)
In your servlet add
request.setAttribute("courses", coursesList);//coursesList is defined as List<Course> and has already been populated by the DAO/JDBC code
Now your table will get populated with all the courses fetched from the database.
If you want to search by course name, then simply have your DAO/JDBC code filter by course name. In your servlet just add code to check that a course name isn't empty and then add the necessary filtering logic.
Also, your form suggests that you want to call http://some_url/search (search is not a parameter but rather a part of the URL address)
Upvotes: 1