Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java When responding to a user request, the servlet should extract the search st

ID: 3602166 • Letter: J

Question

Java

When responding to a user request, the servlet should extract the search string from the Request object and use the DataSource object to find all films that have the search string in their title. If none are found, an appropriate message should be displayed. If one or more films are found, they should be displayed as shown below.

I'm working on the following project. I have set up the server and seems to work fine. However, I'm stuck on part 2, Can someone please explain what I'm supposed to do? Create a Dynamic Web Application to Search for Movies by Title 1. In this assignment, you will use the Tomcat server to host the first iteration of a CyberFlix, a dynamic web application. This iteration will allow a user to search for movies by title. Data for the movies will be supplied in·CSV files along with the Java classes that manage the data. 2. The CyberFlixServlet The search page should submit the form information to a servlet, CyberFlixServlet, as a GET request. The CyberFlixServlet should invoke ServletUtils.setAbsolutePath to establish the path for data files and URLS. This should be done in the servlet's init method. The DataSource class should also be initialized within this method. When responding to a user request, the servlet should extract the search string from the Request object and use the DataSource object to find all films that have the search string in their title. If none are found, an appropriate message should be displayed. If one or more films are found, they should be displayed as shown below.

Explanation / Answer

In part 2, you have to do the following:-

1. Create s Servlet class called "CyberFlixServlet", this class should override at least init() and doGet() methods of its parent class "HttpServlet". Your code should look similar to the below code:-

import java.io.IOException;

import java.io.PrintWriter;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@WebServlet("/CyberFlixServlet")

public class CyberFlixServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

private DataSource dataSource;

public CyberFlixServlet() {

super();

  

// Use ServletUtils.setAbsolutePath() to set paths and urls to csv data files

String csvFilePath = "";

// initialize DataSource object with csv file path

dataSource = new DataSource(csvFilePath);

  

}

/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// Get search term from the querystring

String searchTerm = request.getQueryString().substring(request.getQueryString().indexOf('=')+1);

// Get search results from datasource object's search method

List<String> lstResults = dataSource.searchFilms(searchTerm);

PrintWriter wrt = response.getWriter();

// Show response

if(!lstResults.isEmpty()){ // is results list is not empty, then show results

wrt.print("<table><tr><td>FILMS FOUND</td></tr>");

for(String film: lstResults){

wrt.print("<tr><td>" + film + "</td></tr>");

}

wrt.print("</table>");

}else{ // otherwise, show a message

wrt.print("<h2>No films found matching "" + searchTerm + ""!</h2>");

}

}

}

2. ServletUtils and other classes like DataSource should be provided by your teacher/instructor. Sample DataSource class is provided below, here I am using a sample data list but you should load data from CSV file here.

import java.util.ArrayList;

import java.util.List;

public class DataSource {

// sample film data

private String[] films;

public DataSource(String csvFilePath){

// read csv file and load data to a list

// sample data

films = new String[] { "Harry Potter", "Avatar", "Mission Impossible", "Mission Impossible 2",

"Mission Impossible 3" };

}

// search method

public List<String> searchFilms(String title) {

// create empty list

List<String> lstFilms = new ArrayList<>();

// convert search title to lowercase for ease in searching

title = title.toLowerCase();

// search films

for (int i = 0; i < films.length; i++) {

if (films[i].toLowerCase().contains(title)) {

lstFilms.add(films[i]);

}

}

return lstFilms;

}

}

3. Create an HTML file (name it index.html) and write following code in it and save it in the WebContent folder of your web project.

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>CyberFlix</title>
</head>
<body>
<form action="CyberFlixServlet" method="GET">
Name: <input type="text" name="title"/>
<input type="submit" value="Search" />
</form>
</body>
</html>

3. Above code takes user input and sends it to the CyberFlixServlet as a GET request.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote