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

READ ALL INSTRUCTIONS BEFORE SOLVING...I NEED BOTH A DATA FILE AND A SERVLET TO

ID: 3561857 • Letter: R

Question

READ ALL INSTRUCTIONS BEFORE SOLVING...I NEED BOTH A DATA FILE AND A SERVLET TO BE CREATED. DONT FORGET TO SET THE CORRECT, ABSOLUTE PATH ACCORDING TO THE INSTRUCTIONS. I NEED COMMENTS FOR THIS OR I WONT AWARD BEST ANSWER.

Create a servlet that reads an ascii file and sends the output to the browser that invokes the servlet. Name your ASCII file c: empservlet1.dat. You must locate it in the c: emp directory to receive credit. This is so I can run and grade it on my computer without modifying your source code. Hint: use the file I/O examples we covered previously and use absolute file paths. A good file to read is the source code to your program! Attach your servlet source code file and data file to this assignment in one zipped file. Do not copy (cut and paste) any example code, create your own code and use the examples as a guide.

Explanation / Answer

public class NewServlet extends HttpServlet {
  
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        InputStream ist = new FileInputStream("c: empservlet1.dat");
        BufferedReader istream = new BufferedReader(new InputStreamReader(ist));
        String text = istream.readLine(); //just read the first line in the text file
        out.println("<html>");
       out.println("<body>");
            while(text!=null)
            {
               out.println(text);
               text= istream.readLine();
            }
         out.println("</body>");
         out.println("</html>");
           
      
        }
    }

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

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

       public String getServletInfo() {
        return "Short description";
   

}