/** *****************************************************************
studInfoSysDemo.java SWE 642 Student Information
@author Quansheng Xiao & Jeff Offutt
@version 1.0 (10/06/2001)
@version 1.1 (10/11/2001)
Removes style sheet usage. Added background color.
Corrected English mistakes in messages.
Allow non-numeric PIN.
@version 1.2 (10/19/2001)
revised the "Submit New" so that when a new submission
actually with the same login name and PIN is submitted,
the data will be carried to the next page.
This is more convenient for users.
@version 1.3 (10/25/2001)
uses XML parser to parse XML file
@version 1.4 (01/31/2002)
store majors in a String array
add test for an empty xml file (add verifyEmptyXMLFile() method)
add sort-by-lastname function for student info by
sortByLastName() method
@version 1.5 (2/6/2002)
Jeff Offutt
Update for SWE 642
@version 1.6 (Jan 2009)
Jeff Offutt
Port to CS server
@version 1.7 (Jan 2009)
Yanyan Zhu
Added new fields: JSP, XML, JDBC, AJax, removed CGI
Prints a student information form page. After the student inputs
info, aggregates student information and emails to the professor.
The student can retrieve information and update it.
********************************************************************* */
package studInfoDemo;
// Import Servlet Libraries
import javax.servlet.*;
import javax.servlet.http.*;
// Import Java Libraries
import java.io.*;
import java.util.*;
import java.lang.*;
// Import SMTP Class
import sun.net.smtp.SmtpClient;
import org.xml.sax.*;
// studInfoSysDemo class
//
// CONSTRUCTOR: no constructor specified (default)
//
// **************** OPERATIONS ****************************************
// void doPost () --> Main method for gathering data and sending email.
// void doGet () --> Prints the Login Page.
// private void printForm () --> Prints the infomation form.
// private void appendStudInfo () --> Appends info to XML file for new student.
// private String getXML ()--> Puts the parameters into a string buffer as XML.
// Returns a string.
// private void writeToFile () --> Saves the information into a XML file.
// private void writePrefixToFile () --> Writes the prefix into the XML file
// private void writePostfixToFile () --> Writes the postfix into the XML file
// public void loadStudList() --> Read students' info from XML file to an Arraylist.
// public static boolean verifyEmptyXMLFile( ) --> Verify if the source XML file is empty.
// private ArrayList sortByLastName() --> Appends info to XML file for new student.
// private boolean verifyStud() --> Checks if the student's info already exists.
// private void updateStudInfo() --> Update student's info to XML file for returned student.
// private void printACK () --> Prints the acknowledgement page.
//***********************************************************************
public class studInfoSysDemo extends HttpServlet
{
private static String Servlet = "http://cs.gmu.edu:8080/offutt/servlet/studInfoDemo.studInfoSysDemo"; // CS
public static String classWebSiteURL = "http://www.cs.gmu.edu/~offutt/classes/642/";
public static String FileName = "/var/www/CS/webapps/offutt/WEB-INF/data/demo-info.xml"; // CS
//public static String FileName = "demo-info.xml"; // PC
// majors are stored in array majors
private static String Majors[] = {"SWE", "CS", "ISYS", "IT PhD", "CS PhD", "EE", "ECE", "ISA", "Other"};
private static String MAILTO = "offutt@gmu.edu";
private static String FName;
private static String LName;
private static String PIN;
private static String EmailAddress;
private static String Phone;
private static String WebSiteURL;
private static String Major;
private static String OtherMajor;
private static String LevelHTML;
private static String LevelJava;
private static String LevelJS;
private static String LevelServlets;
private static String LevelJSP;
private static String LevelXML;
private static String LevelJDBC;
private static String LevelAJax;
public static ArrayList StudList; // The list of students
/** *****************************************************
* Overrides HttpServlet's doGet().
* For completeness.
********************************************************* */
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType ("TEXT/HTML");
PrintWriter out = res.getWriter ();
printForm (req, out, "blank", "");
if (!(new File (FileName).exists()))
{
FileWriter datafile = new FileWriter (FileName);
datafile.close ();
}
out.close ();
}
// used as updating flag, if true, update available; if false, update not available
static boolean update = false;
/** *****************************************************
* Overrides HttpServlet's doPost().
********************************************************* */
public void doPost (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
String message;
res.setContentType ("TEXT/HTML");
PrintWriter out = res.getWriter ();
if (!(new File (FileName).exists()))
{
FileWriter datafile = new FileWriter (FileName);
datafile.close ();
}
String action = req.getParameter ("submit");
if (action.equals ("Submit New")) // new student's submission
{
FName = req.getParameter ("FName");
LName = req.getParameter ("LName");
PIN = req.getParameter ("PIN");
if (verifyEmptyXMLFile (FileName) == true)
{
loadStudList (FileName);
if (verifyStud (req, "new") == true)
printForm (req, out, "login", "A user with the same name already exists or you have already registered your information. "
+ "
If you want to retrieve your information, press the \"Retrieve\" button.");
else
{
appendStudInfo (req);
printACK (out, "Your information has been submitted.");
}
}
else
{
appendStudInfo (req);
printACK (out, "Your information has been submitted.");
// printACK (out, FName, LName, PIN, "Your information has been submitted.");
}
}
else if (action.equals ("Retrieve")) // returned student's retrieve
{
update = true;
FName = req.getParameter ("FName");
LName = req.getParameter ("LName");
PIN = req.getParameter ("PIN");
if (verifyEmptyXMLFile (FileName) == true)
{
loadStudList (FileName);
if (verifyStud (req, "retrieve") == false)
{
printForm (req, out, "login", "The combination of the name and PIN is incorrect. Please try again.");
}
else
{
printForm (req, out, "retrieve", "");
}
}
else
{
printForm (req, out, "login", "You are not registered yet. Please use \"Submit New\".");
}
}
else if (action.equals ("Submit Update")) // returned student's submission
{
if (verifyEmptyXMLFile (FileName) == false || update == false)
{
printForm (req, out, "login", "You are not registered yet. Please use \"Submit New\".");
}
else
{
loadStudList (FileName);
// avoids someone making changes to an existing user
if ( (!(req.getParameter ("FName").equals (FName)) || !(req.getParameter ("LName").equals (LName))
|| !(req.getParameter ("PIN").equals (PIN))) && verifyStud (req, "update") == true )
{
printForm (req, out, "login", "A user with the same name already exists. Please try again.");
}
else
{
update = false;
updateStudInfo (req);
printACK (out, "Your information has been updated.");
}
}
}
out.close ();
} // end of doPost() method
/** *****************************************************
* Prints the Information Page.
* flag = "blank", prints blank form
* = "login", prints form with FName and LName prefilled
* = "retrieve", prints form with retrieved data
********************************************************* */
//private void printForm (PrintWriter out, String flag, String errmsg)
//throws IOExceptionprivate
void printForm (HttpServletRequest req, PrintWriter out, String flag, String errmsg)
throws ServletException, IOException
{
out.println ("");
out.println ("");
out.println ("
");
out.println (" SWE 642 Information Form");
out.println ("");
out.println ("");
out.println ("");
out.println ("");
out.println ("");
out.println ("");
out.println ("");
out.println ("");
out.println ("");
out.println ("");
//Prints the information form
out.println ("");
out.println ("
");
out.println ("
");
out.println ("");
out.println ("
");
out.println ("");
out.println ("");
out.println ("");
out.println ("");
} // end of method printForm()
/** *****************************************************
* Sorts the array by last name.
********************************************************* */
private ArrayList sortByLastName (ArrayList sl)
{
int n = sl.size();
String tmp;
String f[]= new String [n];
String l[]= new String [n];
String pin[]= new String [n];
String email[]= new String [n];
String ph[]= new String [n];
String url[]= new String [n];
String m[]= new String [n];
String om[]= new String [n];
String lhtml[]= new String [n];
String ljava[]= new String [n];
String ljs[]= new String [n];
String ls[]= new String [n];
String ljsp[]= new String [n];
String lxml[]= new String [n];
String ljdbc[]= new String [n];
String lajax[]= new String [n];
for (int i = 0; i < n; i++)
{
studEntryDemo stud = (studEntryDemo) sl.get (i);
f[i] = stud.getFName();
l[i] = stud.getLName();
pin[i] = stud.getPIN();
email[i] = stud.getEmailAddress();
ph[i] = stud.getPhone();
url[i] = stud.getWebSiteURL();
m[i] = stud.getMajor();
om[i] = stud.getOtherMajor();
lhtml[i] = stud.getLevelHTML();
ljava[i] = stud.getLevelJava();
ljs[i] = stud.getLevelJS();
ls[i] = stud.getLevelServlets();
ljsp[i] = stud.getLevelJSP();
lxml[i] = stud.getLevelXML();
ljdbc[i] = stud.getLevelJDBC();
lajax[i] = stud.getLevelAJax();
}
//sort by last name
for (int i = 0; i < n-1; i++)
for (int j = i+1; j < n; j++)
{
if (l[i].compareTo (l[j]) > 0)
{
tmp = l[i]; l[i]= l[j]; l[j]= tmp;
tmp = f[i]; f[i]= f[j]; f[j]= tmp;
tmp = pin[i]; pin[i]= pin[j]; pin[j]= tmp;
tmp = email[i]; email[i]= email[j]; email[j]= tmp;
tmp = ph[i]; ph[i]= ph[j]; ph[j]= tmp;
tmp = url[i]; url[i]= url[j]; url[j]= tmp;
tmp = m[i]; m[i]= m[j]; m[j]= tmp;
tmp = om[i]; om[i]= om[j]; om[j]= tmp;
tmp = lhtml[i]; lhtml[i]= lhtml[j]; lhtml[j]= tmp;
tmp = ljava[i]; ljava[i]= ljava[j]; ljava[j]= tmp;
tmp = ljs[i]; ljs[i]= ljs[j]; ljs[j]= tmp;
tmp = ls[i]; ls[i]= ls[j]; ls[j]= tmp;
tmp = ljsp[i]; ljsp[i]= ljsp[j]; ljsp[j]= tmp;
tmp = lxml[i]; lxml[i]= lxml[j]; lxml[j]= tmp;
tmp = ljdbc[i]; ljdbc[i]= ljdbc[j]; ljdbc[j]= tmp;
tmp = lajax[i]; lajax[i]= lajax[j]; lajax[j]= tmp;
}
}
ArrayList sl_tmp = new ArrayList(); // temperary list of students
for (int i = 0; i < n; i++ )
{
sl_tmp.add (new studEntryDemo (f[i], l[i], pin[i], email[i], ph[i], url[i], m[i], om[i], lhtml[i], ljava[i], ljs[i], ls[i], ljsp[i], lxml[i], ljdbc[i], lajax[i]));
}
return sl_tmp;
} // end of method sortByLastName()
/** *****************************************************
* Removes a less than character '<' from a string
* XML does not like them. (It crashes tomcat.)
********************************************************* */
private String trimLT (String s)
{
if (s.indexOf ('<') >= 0)
{ // Found one.
return (s.replace ('<', '#'));
}
else
return s;
}
/** *****************************************************
* Appends info to XML file for new student.
********************************************************* */
private void appendStudInfo (HttpServletRequest req)
throws ServletException, IOException
{
writePrefixToFile (FileName);
//if StudList has not been initialized, do it here
if (StudList == null)
{
StudList = new ArrayList();
}
FName = trimLT (req.getParameter ("FName"));
LName = trimLT (req.getParameter ("LName"));
PIN = trimLT (req.getParameter ("PIN"));
EmailAddress = trimLT (req.getParameter ("EmailAddress"));
Phone = trimLT (req.getParameter ("Phone"));
WebSiteURL = trimLT (req.getParameter ("WebSiteURL"));
Major = trimLT (req.getParameter ("Major"));
if (Major.equals ("Other"))
OtherMajor = trimLT (req.getParameter ("OtherMajor"));
else
OtherMajor = "";
LevelHTML = req.getParameter ("LevelHTML");
LevelJava = req.getParameter ("LevelJava");
LevelJS = req.getParameter ("LevelJS");
LevelServlets = req.getParameter ("LevelServlets");
LevelJSP = req.getParameter ("LevelJSP");
LevelXML = req.getParameter ("LevelXML");
LevelJDBC = req.getParameter ("LevelJDBC");
LevelAJax = req.getParameter ("LevelAJax");
StudList.add (new studEntryDemo (FName, LName, PIN, EmailAddress, Phone, WebSiteURL,
Major, OtherMajor, LevelHTML, LevelJava, LevelJS, LevelServlets, LevelJSP, LevelXML, LevelJDBC, LevelAJax));
StudList = sortByLastName (StudList);
for (int i = 0; i < StudList.size(); i++ )
{
studEntryDemo stud = (studEntryDemo) StudList.get (i);
FName = stud.getFName();
LName = stud.getLName();
PIN = stud.getPIN();
EmailAddress = stud.getEmailAddress();
Phone = stud.getPhone();
WebSiteURL = stud.getWebSiteURL();
Major = stud.getMajor();
OtherMajor = stud.getOtherMajor();
LevelHTML = stud.getLevelHTML();
LevelJava = stud.getLevelJava();
LevelJS = stud.getLevelJS();
LevelServlets = stud.getLevelServlets();
LevelJSP = stud.getLevelJSP();
LevelXML = stud.getLevelXML();
LevelJDBC = stud.getLevelJDBC();
LevelAJax = stud.getLevelAJax();
String message = getXML();
writeToFile (FileName, message);
}
writePostfixToFile (FileName);
} // end of method appendStudInfo()
/** *****************************************************
* Verify if the source XML file is empty.
* if empty, returns false, else returns true.
********************************************************* */
public static boolean verifyEmptyXMLFile (String fileName) throws IOException, FileNotFoundException
{
BufferedReader fin = new BufferedReader (new FileReader (fileName));
String s;
try
{
while ((s = fin.readLine() ) != null)
{
s = s.trim();
if (!s.equals (""))
return true;
}
}
catch (IOException e)
{
System.out.println ("File error: " + fileName);
}
fin.close();
StudList = null; //reset StudList for empty file case
return false;
} // end of method verifyEmptyXMLFile()
/** *****************************************************
* Read students' info from XML file to an Arraylist.
********************************************************* */
public static void loadStudList (String fileName) throws IOException
{
try
{
parserXMLDemo xp = new parserXMLDemo();
xp.parse (xp.readFile (fileName));
StudList = xp.StudList;
}
catch (Exception e)
{
System.out.println ("Exception: " + e);
System.out.println ("XML SAXParse error in XMLParser.readDoc()");
}
} // end of method loadStudInfo()
/* ======================================================== */
static int existIndex; //store the student's index in StudList.
/* ======================================================== */
/** *****************************************************
* checks if the student's info already exists.
********************************************************* */
private boolean verifyStud (HttpServletRequest req, String newOrUpdate)
throws ServletException, IOException
{
if (newOrUpdate.equals ("new"))
{
for (int i = 0; i < StudList.size(); i++ )
{
studEntryDemo stud = (studEntryDemo) StudList.get (i);
if (stud.getFName().equals (FName) && stud.getLName().equals (LName))
{
return true;
}
}
}
else if (newOrUpdate.equals ("retrieve"))
{
for (int i = 0; i < StudList.size(); i++ )
{
studEntryDemo stud = (studEntryDemo) StudList.get (i);
if (stud.getFName().equals (FName) &&
stud.getLName().equals (LName) &&
stud.getPIN().equals (PIN))
{
EmailAddress = stud.getEmailAddress();
Phone = stud.getPhone();
WebSiteURL = stud.getWebSiteURL();
Major = stud.getMajor();
OtherMajor = stud.getOtherMajor();
LevelHTML = stud.getLevelHTML();
LevelJava = stud.getLevelJava();
LevelJS = stud.getLevelJS();
LevelServlets = stud.getLevelServlets();
LevelJSP = stud.getLevelJSP();
LevelXML = stud.getLevelXML();
LevelJDBC = stud.getLevelJDBC();
LevelAJax = stud.getLevelAJax();
existIndex = i;
return true;
}
}
}
else if (newOrUpdate.equals ("update"))
{
for (int i = 0; i < StudList.size(); i++ )
{
studEntryDemo stud = (studEntryDemo) StudList.get (i);
if (stud.getFName().equals (req.getParameter ("FName"))
&& stud.getLName().equals (req.getParameter ("LName"))
&& stud.getPIN().equals (req.getParameter ("PIN")))
{
return true;
}
}
}
return false;
} // end of verifyStud() method
/** *****************************************************
* Update student's info to XML file for returned student.
********************************************************* */
private void updateStudInfo (HttpServletRequest req) throws ServletException, IOException
{
FName = trimLT (req.getParameter ("FName"));
LName = trimLT (req.getParameter ("LName"));
PIN = trimLT (req.getParameter ("PIN"));
EmailAddress = trimLT (req.getParameter ("EmailAddress"));
Phone = trimLT (req.getParameter ("Phone"));
WebSiteURL = trimLT (req.getParameter ("WebSiteURL"));
Major = trimLT (req.getParameter ("Major"));
OtherMajor = trimLT (req.getParameter ("OtherMajor"));
LevelHTML = req.getParameter ("LevelHTML");
LevelJava = req.getParameter ("LevelJava");
LevelJS = req.getParameter ("LevelJS");
LevelServlets = req.getParameter ("LevelServlets");
//replace the element at index existIndex
StudList.set (existIndex, new studEntryDemo (FName, LName, PIN, EmailAddress, Phone, WebSiteURL,
Major, OtherMajor, LevelHTML, LevelJava, LevelJS, LevelServlets, LevelJSP, LevelXML, LevelJDBC, LevelAJax));
StudList = sortByLastName (StudList);
writePrefixToFile (FileName);
for (int i = 0; i < StudList.size(); i++ )
{
studEntryDemo stud = (studEntryDemo) StudList.get (i);
//this part is replaced by using StudList.set (index, (Object)element)
/*
if (i == existIndex)
{
FName = req.getParameter ("FName");
LName = req.getParameter ("LName");
PIN = req.getParameter ("PIN");
EmailAddress = req.getParameter ("EmailAddress");
Phone = req.getParameter ("Phone");
WebSiteURL = req.getParameter ("WebSiteURL");
Major = req.getParameter ("Major");
OtherMajor = req.getParameter ("OtherMajor");
LevelHTML = req.getParameter ("LevelHTML");
LevelJava = req.getParameter ("LevelJava");
LevelCGI = req.getParameter ("LevelCGI");
LevelJS = req.getParameter ("LevelJS");
LevelServlets = req.getParameter ("LevelServlets");
}
else
*/
{
FName = stud.getFName();
LName = stud.getLName();
PIN = stud.getPIN();
EmailAddress = stud.getEmailAddress();
Phone = stud.getPhone();
WebSiteURL = stud.getWebSiteURL();
Major = stud.getMajor();
OtherMajor = stud.getOtherMajor();
LevelHTML = stud.getLevelHTML();
LevelJava = stud.getLevelJava();
LevelJS = stud.getLevelJS();
LevelServlets = stud.getLevelServlets();
LevelJSP = stud.getLevelJSP();
LevelXML = stud.getLevelXML();
LevelJDBC = stud.getLevelJDBC();
LevelAJax = stud.getLevelAJax();
}
String message = getXML();
writeToFile (FileName, message);
}
writePostfixToFile (FileName);
} // end of method saveStudInfo()
/** *****************************************************
* Puts the parameters into a string buffer as XML.
* Returns a string.
********************************************************* */
private String getXML() throws IOException
{
StringBuffer tempStringBuffer = new StringBuffer (4096);
tempStringBuffer.append (" \n");
// Format the contact info.
tempStringBuffer.append (" ");
tempStringBuffer.append (FName);
tempStringBuffer.append ("\n");
tempStringBuffer.append (" ");
tempStringBuffer.append (LName);
tempStringBuffer.append ("\n");
tempStringBuffer.append (" ");
tempStringBuffer.append (PIN);
tempStringBuffer.append ("\n");
// Handle several email addresses, separated by a carriage
// return and line feed.
// Loop through the email address looking for the next
// carriage return, stop when there is none.
int ret = EmailAddress.indexOf ('\r'); // Return
while (ret != -1) // handle several email addresses
{
String curemail = EmailAddress.substring (0, ret);
tempStringBuffer.append (" ");
tempStringBuffer.append (curemail);
tempStringBuffer.append ("\n");
EmailAddress = EmailAddress.substring (ret+2, EmailAddress.length());
ret = EmailAddress.indexOf ('\r'); // Return
}
tempStringBuffer.append (" ");
tempStringBuffer.append (EmailAddress);
tempStringBuffer.append ("\n");
tempStringBuffer.append (" ");
tempStringBuffer.append (WebSiteURL);
tempStringBuffer.append ("\n");
tempStringBuffer.append (" ");
tempStringBuffer.append (Phone);
tempStringBuffer.append ("\n");
tempStringBuffer.append (" ");
tempStringBuffer.append (Major);
tempStringBuffer.append ("\n");
tempStringBuffer.append (" ");
tempStringBuffer.append (OtherMajor);
tempStringBuffer.append ("\n");
// Now print the levels of knowledge of the various subjects.
tempStringBuffer.append (" ");
tempStringBuffer.append (LevelHTML);
tempStringBuffer.append ("\n");
tempStringBuffer.append (" ");
tempStringBuffer.append (LevelJava);
tempStringBuffer.append ("\n");
tempStringBuffer.append (" ");
tempStringBuffer.append (LevelJS);
tempStringBuffer.append ("\n");
tempStringBuffer.append (" ");
tempStringBuffer.append (LevelServlets);
tempStringBuffer.append ("\n");
tempStringBuffer.append (" ");
tempStringBuffer.append (LevelJSP);
tempStringBuffer.append ("\n");
tempStringBuffer.append (" ");
tempStringBuffer.append (LevelXML);
tempStringBuffer.append ("\n");
tempStringBuffer.append (" ");
tempStringBuffer.append (LevelJDBC);
tempStringBuffer.append ("\n");
tempStringBuffer.append (" ");
tempStringBuffer.append (LevelAJax);
tempStringBuffer.append ("\n");
tempStringBuffer.append (" \n");
return (tempStringBuffer.toString());
} // end of getXML() emthod
/** *****************************************************
* Writes the Prefix into the XML file
********************************************************* */
private void writePrefixToFile (String fileName)
{
try
{
FileWriter datafile = new FileWriter (fileName);
datafile.write ("\n");
datafile.write ("\n");
datafile.close ();
}
catch (IOException e)
{
log ("Error occurred while writing to file", e);
}
}
/** *****************************************************
* Writes the Postfix into the XML file
********************************************************* */
private void writePostfixToFile (String fileName)
{
try
{
FileWriter datafile = new FileWriter (fileName, true);
datafile.write ("");
datafile.close ();
}
catch (IOException e)
{
log ("Error occurred while writing to file", e);
}
}
/** *****************************************************
* Saves the information into a file
********************************************************* */
private void writeToFile (String fileName, String Mes)
{
try
{
FileWriter datafile = new FileWriter (fileName, true);
datafile.write (Mes);
datafile.write ("\n");
datafile.close ();
}
catch (IOException e)
{
log ("Error occurred while writing to file", e);
}
}
/** *****************************************************
* Prints the acknowledgement page.
********************************************************* */
private void printACK (PrintWriter out, String message)
throws IOException
{
out.println ("");
out.println ("");
out.println (" 642 Student Information Acknowledgement");
out.println ("");
out.println ("");
out.println ("642 Student Information Acknowledgement
");
out.println ("
");
// Send ack
out.println ("
" + message + "
");
// Sometimes this printed the PREVIOUS info, I don't know why. Jeff Offutt, January 2009
// out.println ("
Remember your name and PIN in case you need to update this information later:");
// out.println ("
– " + FName);
// out.println ("
– " + LName);
// out.println ("
– " + PIN + "
");
out.println ("
Thank you!
");
out.println ("Class web site");
out.println ("
Student information system form");
out.println ("");
out.println ("");
out.close ();
}
} // end of StudinfoSysDemo class