/* * GetGrade.java 97/12/04 * * Copyright (c) 1997 Ralph Freese * */ import java.io.*; import java.util.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; /** * Get Grade servlet. This servlet let students lookup their grades on * the net. * * There are no init parameters. We use a hidden field to specify * where the grade file is. * * fields: * * socsecno the students SS#. * filename file with grades (hidden) * * * @version 1.0, 97/12/04 * @author Ralph Freese */ public class GetGrade extends HttpServlet { private String gradeFile; private String line1 = null; private String line2 = null; public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doGet(req, res); } public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { gradeFile = req.getParameter("filename"); String socsecno = req.getParameter("socsecno"); res.setContentType("text/html"); ServletOutputStream out = res.getOutputStream(); if (!checkSS(socsecno)) { out.println(""); out.println("Bad SS"); out.println(""); out.println("

Bad Social Security Number:

"); out.println("You must fill in all 9 digits."); out.println("Do not put any dashes."); out.println("You can press your back button to go back and "); out.println("and fill in again.

"); out.println(""); } else { String gradeLine = getGradeLine(socsecno); if (gradeLine == null) { out.println(""); out.println("Grade Not Found"); out.println(""); out.println("

Grade Not Found

"); out.println("Sorry, your grade was not found."); out.println("Be sure your Social Security number is correct."); out.println("You can press your back button to go back and "); out.println("and fill in again.

"); out.println(""); } else { out.println(""); out.println("Grades"); out.println(""); out.println("

"); out.println(""); out.println(""); out.println(""); out.println("
" + line1 + "
" + line2 + "
" + gradeLine + "
"); out.println(""); } } } public boolean checkSS(String ss) { if (ss.length() != 9) { return false; } String ss_cap = ss.toUpperCase(); if (! ss.equals(ss_cap)) { return false; } return true; } public String getGradeLine(String ss) { FileReader f = null; try { f = new FileReader(gradeFile); } catch (FileNotFoundException e) { System.out.println("file not found"); } BufferedReader file = new BufferedReader(f); String line = null; boolean found = false; try { line1 = file.readLine(); if (line1 != null) { line2 = file.readLine(); } if (line2 != null) { while (!found && (line = file.readLine()) != null) { if (line.startsWith(ss)) { found = true; } } } else { line = "The grade file was defective---tell your teacher"; } } catch (IOException e) { System.out.println("IOException"); } return line; } }