/* * Get206LGrade.java 97/12/30 * * Copyright (c) 1997 Ralph Freese * */ import java.io.*; import java.util.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; /** * Get 206L 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) * section * * * @version 1.0, 97/12/30 * @author Ralph Freese */ public class Get206LGrade extends HttpServlet { private String gradeFile; private String section; final private String head1 = "Lab 1Lab 2Lab 3Lab 4Lab 5Lab 6" + "Lab 7Lab 9 Lab 10"; final private String head2 = "Quiz 1Quiz 2Quiz 3"; public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doGet(req, res); } public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { section = req.getParameter("section"); gradeFile = "/home/ralph/private_html/206L/section" + section + ".txt"; 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("Also, make sure you choose the right section.

"); out.println(""); } else { Vector grades = new Vector(9); Vector quizzes = new Vector(3); int count = 0; StringTokenizer st = new StringTokenizer(gradeLine,"\t",true); String tok = null; st.nextToken(); // name st.nextToken(); // tab st.nextToken(); // ss# st.nextToken(); // tab while (st.hasMoreTokens() && count < 9) { count++; tok = st.nextToken(); // lab hw grade if (!tok.equals("\t")) { grades.addElement(tok); if (st.hasMoreTokens()) { st.nextToken(); } } else { grades.addElement(" "); } } while (st.hasMoreTokens()) { tok = st.nextToken(); // quiz grade if (!tok.equals("\t")) { quizzes.addElement(tok); if (st.hasMoreTokens()) { st.nextToken(); } } else { quizzes.addElement(" "); } } out.println(""); out.println("206L Grades"); out.println(""); out.println("

Your Grades

"); out.println("

Math 206L, Section " + section + "

"); out.println("

Student Number: " + socsecno + "


"); out.println("

Homework Grades

"); out.println(""); out.println(head1); out.print("\n "); Enumeration elems = grades.elements(); String grade, color; while (elems.hasMoreElements()) { grade = (String)elems.nextElement(); if (grade.equals(" ")) { color = "\"#FF0000\""; } else { color = "\"#00FFFF\""; } out.print(""); out.println("
" + grade); } out.println("


"); out.println("

Quiz Grades

"); out.println(""); out.println(head2); elems = quizzes.elements(); out.print("\n "); while (elems.hasMoreElements()) { grade = (String)elems.nextElement(); if (grade.equals(" ")) { color = "\"#FF0000\""; } else { color = "\"#00FFFF\""; } out.print(""); out.println("
" + grade); } out.println("
"); 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; String tok; StringTokenizer st = null; boolean found = false; try { while (!found && (line = file.readLine()) != null) { st = new StringTokenizer(line,"\t",false); st.nextToken(); // First field is name; second is ss# tok = st.nextToken(); if (ss.equals(tok)) { found = true; } } } catch (IOException e) { System.out.println("IOException"); } return line; } }