Search HHH

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

Searchhh:

package com.Accio;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/Search")
public class Search extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
String keyword=request.getParameter("keyword");
Connection connection = DatabaseConnection.getConnection();
try {
PreparedStatement preparedStatement=connection.prepareStatement("Insert
into history values(?,?);");
preparedStatement.setString(1,keyword);
preparedStatement.setString(2,"http://localhost:8080/SearchEngine/
Search?keyword="+keyword);
preparedStatement.executeUpdate();

ResultSet resultSet = connection.createStatement().executeQuery("select


pageTitle,pageLink,(length(lower(pageText))-length(replace(lower(pageText),'" +
keyword + "','')))/length('" + keyword + "') as countOccurence from pages order by
countOccurence desc limit 30;");
ArrayList<SearchResult> results = new ArrayList<>();
while (resultSet.next()) {
SearchResult searchResult = new SearchResult();
searchResult.setTitle(resultSet.getString("pageTitle"));
searchResult.setLink(resultSet.getString("pageLink"));
results.add(searchResult);
}
for(SearchResult result:results){
System.out.println(result.getTitle()+"\t"+result.getLink()+"\n");
}

request.setAttribute("results",results);
request.getRequestDispatcher("search.jsp").forward(request,response);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
}
catch(SQLException | ServletException sqlException) {
sqlException.printStackTrace();
}
}
}
ChatGPT
This code snippet represents a Java servlet named Search in the com.Accio package.
Let's break down its functionality:
Servlet Declaration:

@WebServlet("/Search"): Annotation indicating that this servlet will handle HTTP


requests for the URL pattern "/Search".
Method Override:

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws IOException { ... }: Overrides the doGet method of the HttpServlet class to
handle HTTP GET requests.
Request Processing:

String keyword = request.getParameter("keyword");: Retrieves the value of the


"keyword" parameter from the HTTP request.
Connection connection = DatabaseConnection.getConnection();: Obtains a database
connection using the DatabaseConnection class.
Database Operations:

PreparedStatement preparedStatement = connection.prepareStatement("Insert into


history values(?,?);");: Prepares a SQL statement to insert the search keyword and
its corresponding URL into a database table named "history".
preparedStatement.setString(1, keyword);: Sets the first parameter of the prepared
statement to the value of the keyword.
preparedStatement.setString(2, "http://localhost:8080/SearchEngine/Search?keyword="
+ keyword);: Sets the second parameter of the prepared statement to the URL of the
search.
preparedStatement.executeUpdate();: Executes the SQL insert statement to store the
search history in the database.
Search Query Execution:

ResultSet resultSet = connection.createStatement().executeQuery("select pageTitle,


pageLink, (length(lower(pageText)) - length(replace(lower(pageText),'" + keyword +
"','')))/length('" + keyword + "') as countOccurence from pages order by
countOccurence desc limit 30;");: Executes a SQL query to retrieve search results
from a database table named "pages". The query calculates the relevance of each
page based on the number of occurrences of the keyword in the page text.
ArrayList<SearchResult> results = new ArrayList<>();: Initializes an ArrayList to
store the search results.
while (resultSet.next()) { ... }: Iterates over the result set and extracts data
for each search result.
SearchResult searchResult = new SearchResult();: Creates a new instance of the
SearchResult class for each search result.
searchResult.setTitle(resultSet.getString("pageTitle"));: Sets the title of the
search result.
searchResult.setLink(resultSet.getString("pageLink"));: Sets the link of the search
result.
results.add(searchResult);: Adds the search result to the ArrayList.
Forwarding Results:

request.setAttribute("results", results);: Sets the search results as an attribute


in the request.
request.getRequestDispatcher("search.jsp").forward(request, response);: Forwards
the request to the "search.jsp" page for further processing and rendering.
Response Handling:

response.setContentType("text/html");: Sets the content type of the response to


HTML.
PrintWriter out = response.getWriter();: Obtains a PrintWriter object to write HTML
content to the response.
Exception Handling:
catch(SQLException | ServletException sqlException) { ... }: Catches any SQL or
servlet-related exceptions that occur during request processing and prints the
stack trace.
Overall, this servlet handles incoming search requests, stores the search history
in the database, executes a search query to retrieve relevant search results, and
forwards the results to a JSP page for display to the user.

User
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.Accio.SearchResult" %>
<html>
<body>
<table border=2>
<tr>
<th>Title</th>
<th>Link</th>
</tr>
<%
ArrayList<SearchResult> results =
(ArrayList<SearchResult>)request.getAttribute("results");
for(SearchResult result:results){
%>
<tr>
<td><%out.println(result.getTitle());%></td>
<td><a href="<%out.println(result.getLink());%>"><
%out.println(result.getLink());%></a></td>
</tr>
<%
}
%>
</table>
</body>
</html>
ChatGPT
This JSP (JavaServer Pages) code snippet is responsible for rendering search
results in an HTML table format. Let's break down its functionality:

Import Statements:

<%@ page import="java.util.ArrayList" %>: Imports the ArrayList class from the
java.util package.
<%@ page import="com.Accio.SearchResult" %>: Imports the SearchResult class from
the com.Accio package.
HTML Structure:

<html> and <body> tags: Define the HTML document structure.


<table border=2>: Starts a table with a border attribute set to 2 for better
visibility.
**

User
package com.Accio;

public class SearchResult {


String title;
String link;

public String getTitle() {


return title;
}

public void setTitle(String title) {


this.title = title;
}

public String getLink() {


return link;
}

public void setLink(String link) {


this.link = link;
}
}
ChatGPT
The SearchResult class defines the structure of objects that represent search
results in the search engine application. Here's a breakdown of its components:

Attributes:

title: Represents the title of a search result.


link: Represents the URL link associated with the search result.
Methods:

getTitle(): Returns the title of the search result.


setTitle(String title): Sets the title of the search result.
getLink(): Returns the URL link of the search result.
setLink(String link): Sets the URL link of the search result.
This class follows a typical Java bean pattern where it encapsulates its attributes
with private access and provides public getter and setter methods to manipulate
those attributes. This structure allows for easy management and retrieval of search
result data within the application.

You might also like