Create a servlet that reads an ascii file and sends the output to the browser th
ID: 3743306 • Letter: C
Question
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
// SERVLET THAT CAN READ ASCII FILE AND SEND THE DATA TO THE BROWSER
1. The InputStreamReader class is used to read the file in servlets program.
2. Get the file InputStream using ServletContext.getResourceAsStream() method.
3. If input stream is not equal to null, create the object of InputStreamReader
and pass it to the BufferedReader.
4. A variable text is defined of String type.
5. Read the file line by line using the while loop ((text = reader.readLine()) != null).
6. Then the writer.println(text) is used to display the content of the file
asci.html
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p> <h1> Click ont he folowign Button, you can get ascii code from a remote file</h1>
<from action="ReadTextFileServlet" method="get">
<input type="submit" value="Clickhere" />
</from>
</body>
</html>
ReadTextFile.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ReadTextFile extends HttpServlet
{
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
String filename = "/WEB-INF/message.properties";
ServletContext context = getServletContext();
InputStream inp = context.getResourceAsStream(filename);
if (inp != null) {
InputStreamReader isr = new InputStreamReader(inp);
BufferedReader reader = new BufferedReader(isr);
PrintWriter pw = response.getWriter();
pw.println("<html><head><title>Read Text File</title></head>
<body bgcolor='cyan'></body></html>");
String text = "";
while ((text = reader.readLine()) != null) {
pw.println("<h2><i><b>"+text+"</b></i></b><br>");
}
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.