Java Servlet Song Data Collection and Display
Song Survey
Enter the Song Data
Please fill in the following details:
- Song Name:
- Composer Name:
- Singer Name:
import java.io.IOException;import java.io.PrintWriter;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.*;import java.io.FileWriter;import java.util.*;import javax.servlet.http.*;@WebServlet(urlPatterns = {"/songlist"})public class NewServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String songname=request.getParameter("songname"); String cname=request.getParameter("cname"); String singer=request.getParameter("singer"); String song_file="C:\\Users\\5cs04\\Documents\\NetBeansProjects\\WebApplic"; song_file = song_file + "ation1\\web\\song.txt"; try { FileWriter fw=new FileWriter(song_file,true); fw.write("\n"); fw.write(songname); fw.write("#"); fw.write(singer); fw.write("##"); fw.write(cname); fw.write("\r\n"); fw.close(); } catch(Exception e) { System.out.println(e); } out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet Songlist</title>"); out.println("</head>"); out.println("<body>"); out.println("<h4>The contents are written to the file "+song_file + "</h4>"); out.println("<h4>The most popular songs are</h4>"); out.println("<h3> song name #singer ## composer</h3>"); FileReader fr=new FileReader(song_file); int i; while((i=fr.read())!=-1) out.println((char)i); fr.close(); out.println("</body>"); out.println("</html>"); }}