-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchServlet.java
More file actions
40 lines (31 loc) · 1.3 KB
/
Copy pathSearchServlet.java
File metadata and controls
40 lines (31 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//This Servlet handles the HTTP request, processes the search query using your Java-based search engine, and returns the results.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
public class SearchServlet extends HttpServlet {
private SimpleSearchEngine searchEngine;
@Override
public void init() throws ServletException {
searchEngine = new SimpleSearchEngine();
searchEngine.setup();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String query = request.getParameter("query");
List<String> results = searchEngine.search(query);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>Search Results for '" + query + "':</h1>");
if (!results.isEmpty()) {
for (String result : results) {
out.println("<p>" + result + "</p>");
}
} else {
out.println("<p>No results found.</p>");
}
out.println("<a href='Index.html'>Back to search</a>");
out.println("</body></html>");
}
}