Question

In: Computer Science

Explain how it is that we build the “processRequest()” method in a Servlet class, when the...

Explain how it is that we build the “processRequest()” method in a Servlet class, when the Servlet interface requires us to write 3 methods(init(), service() and destroy()). Why don’t we have to build these 3 methods?  

Solutions

Expert Solution

The "processReuest()" can be built without specifically writing the given 3 methods. The process for creation is mentioned below:

1. Add the following method declaration for processRequest(req, res). Add open and close parentheses ({, }) and position the cursor in between the parenthesis.

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

2. Declare a variable employeeList of the List type that contains Employee objects. Declare the variable gson to process Gson object.

List<Employee> employeeList = null;

3. Instantiate the employeeList object by invoking getEmployees method of EmployeeBean.

employeeList = employeeBean.getEmployees();

4. Check if the employeeList is not NULL

if (employeeList != null) {

5. Set the content type to “application/json”

response.setContentType("application/json");

6. Invoke the method toJson(…) and convert the employeeList to JSON.

Copygson.toJson(employeeList,
            new TypeToken<ArrayList<Employee>>() {}.getType(),
response.getWriter());

7. End of if condition. Close the parenthesis (})

8. Add an else condition to cover the error scenario when the employeeList is empty.

Copyelse {
  response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}

Related Solutions

Explain how it is that we build the “processRequest()” method in a Servlet class, when the...
Explain how it is that we build the “processRequest()” method in a Servlet class, when the Servlet interface requires us to write 3 methods(init(), service() and destroy()). Why don’t we have to build these 3 methods???
Part I – Build a simple Servlet called MyServlet using NetBeans. Add this Servlet to you...
Part I – Build a simple Servlet called MyServlet using NetBeans. Add this Servlet to you “ChattBank” Project. This MyServlet will display a message like “Go Braves” in a simple <h1> tag. Run this servlet from a Browser window by typing in the servlet name in the URL line. (ie. http://localhost:8080/ChattBank/MyServlet). Make sure that your Server is up and running before you test this Servlet. The best way to do this is just Run your “ChattBank” Project once before you...
Explain Design Build method
Explain Design Build method
IN JAVA: Build a class called ExamPractice from scratch. This class should include a main method...
IN JAVA: Build a class called ExamPractice from scratch. This class should include a main method that does the following: Prompt the user to enter a number of inches Read the number of inches entered from the console Convert the inches into an equivalent number of miles, yards, feet, and inches. Output the results to the console. For example, if a user entered 100000 inches, the program would output: 100000 inches is equivalent to: Miles: 1 Yards: 1017 Feet: 2...
When you put a Servlet in a Web Application on the Tomcat/Glassfish server, how do you...
When you put a Servlet in a Web Application on the Tomcat/Glassfish server, how do you go about running this Servlet? a. From the Browser’s URL(give path):
How do you schedule a Servlet to run, from an HTML file, when the User clicks...
How do you schedule a Servlet to run, from an HTML file, when the User clicks on the Submit button? Show html code here
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main...
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main method. In the main method you should read in all of the rows of data from the Instructors Table in the Registration Database, then print out this data. Follow the steps outlined in class. Remember to use try-catch blocks for all database access code. The example in the book does not use Try-catch blocks, but you should
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print all elements of a linked list in order. B. Method to print all elements of a linked list in reverse order. C. Method to print all elements of a linked list in order starting from specific position. D. Method to join two linked lists into the first list in the parameters. E. Method to clone a linked list. The copy list has to be...
Questions related to C#. When you build a class in C#.NET, how many sub classes can...
Questions related to C#. When you build a class in C#.NET, how many sub classes can inherit from the class that you just built? In Object Oriented Development, there are 3 primary concepts that define Object Oriented Programming. What are these concepts? Function/Method Overloading is when you build 2 or 3 functions with the same name but different argument lists. T/F?
Give an example of how to build an array of objects of a super class with...
Give an example of how to build an array of objects of a super class with its subclass objects. As well as, use an enhanced for-loop to traverse through the array while calling a static method(superclass x). Finally, create a static method for the class that has the parent class reference variable.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT