package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
/** Servlet that handles previewing and storing resumes
* submitted by job applicants.
*
* Taken from Core Servlets and JavaServer Pages 2nd Edition
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2003 Marty Hall; may be freely used or adapted.
*/
public class SubmitResume extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
if (request.getParameter("previewButton") != null) {
showPreview(request, out);
} else {
storeResume(request);
showConfirmation(request, out);
}
}
/** Shows a preview of the submitted resume. Takes
* the font information and builds an HTML
* style sheet out of it, then takes the real
* resume information and presents it formatted with
* that style sheet.
*/
private void showPreview(HttpServletRequest request,
PrintWriter out) {
String headingFont = request.getParameter("headingFont");
headingFont = replaceIfMissingOrDefault(headingFont, "Comic Sans MS");
int headingSize =
getSize(request.getParameter("headingSize"), 32);
String bodyFont = request.getParameter("bodyFont");
bodyFont = replaceIfMissingOrDefault(bodyFont, "Arial");
int bodySize =
getSize(request.getParameter("bodySize"), 18);
String fgColor = request.getParameter("fgColor");
fgColor = replaceIfMissing(fgColor, "BLACK");
String bgColor = request.getParameter("bgColor");
bgColor = replaceIfMissing(bgColor, "WHITE");
String name = request.getParameter("name");
name = replaceIfMissing(name, "Lou Zer");
String title = request.getParameter("title");
title = replaceIfMissing(title, "Loser");
String email = request.getParameter("email");
email =
replaceIfMissing(email, "contact@hot-computer-jobs.com");
String languages = request.getParameter("languages");
languages = replaceIfMissing(languages, "None");
//String languageList = makeList(languages);
String skills = request.getParameter("skills");
skills = replaceIfMissing(skills, "Not many, obviously.");
out.println
(ServletUtilities.DOCTYPE + "\n" +
"
Resume for " + name + "\n" +
makeStyleSheet(headingFont, headingSize,
bodyFont, bodySize,
fgColor, bgColor) + "\n" +
"\n" +
"\n" +
"\n"+
"" + name + "
\n" +
"" + title + "
\n" +
"" + email +
"\n" +
"
\n" +
"Programming Languages" +
"\n" +
makeList(languages) + "
\n" +
"Skills and Experience" +
"
\n" +
skills + "\n" +
"");
}
/** Builds a cascading style sheet with information
* on three levels of headings and overall
* foreground and background cover. Also tells
* Internet Explorer to change color of mailto link
* when mouse moves over it.
*/
private String makeStyleSheet(String headingFont,
int heading1Size,
String bodyFont,
int bodySize,
String fgColor,
String bgColor) {
int heading2Size = heading1Size*7/10;
int heading3Size = heading1Size*6/10;
String styleSheet =
"";
return(styleSheet);
}
/** Replaces null strings (no such parameter name) or
* empty strings (e.g., if textfield was blank) with
* the replacement. Returns the original string otherwise.
*/
private String replaceIfMissing(String orig,
String replacement) {
if ((orig == null) || (orig.trim().equals(""))) {
return(replacement);
} else {
return(orig);
}
}
// Replaces null strings, empty strings, or the string
// "default" with the replacement.
// Returns the original string otherwise.
private String replaceIfMissingOrDefault(String orig,
String replacement) {
if ((orig == null) ||
(orig.trim().equals("")) ||
(orig.equals("default"))) {
return(replacement);
} else {
return(orig);
}
}
// Takes a string representing an integer and returns it
// as an int. Returns a default if the string is null
// or in an illegal format.
private int getSize(String sizeString, int defaultSize) {
try {
return(Integer.parseInt(sizeString));
} catch(NumberFormatException nfe) {
return(defaultSize);
}
}
// Given "Java,C++,Lisp", "Java C++ Lisp" or
// "Java, C++, Lisp", returns
// "
// - Java
//
- C++
//
- Lisp
//
"
private String makeList(String listItems) {
StringTokenizer tokenizer =
new StringTokenizer(listItems, ", ");
String list = "\n";
while(tokenizer.hasMoreTokens()) {
list = list + " - " + tokenizer.nextToken() + "\n";
}
list = list + "
";
return(list);
}
/** Show a confirmation page when they press the
* "Submit" button.
*/
private void showConfirmation(HttpServletRequest request,
PrintWriter out) {
String title = request.getParameter("name") + ", Submission Confirmed.";
out.println(ServletUtilities.headWithTitle(title) +
"\n" +
"" + title + "
\n" +
"Your resume should appear online within\n" +
"24 hours. If it doesn't, try submitting\n" +
"again with a different email address.\n" +
"