/* * GetAddressServlet.java 97/10/23 * * Copyright (c) 1997 Ralph Freese * */ import java.io.*; import java.util.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; /** * Get Address servlet. This servlet saves the information from a from * to a log file and serves up a file provided the required fields were * entered. If not a page with a message to go back and give the required * fields is served. * * There are 2 init parameters: * * servedfile the file served if the info is ok * logfile file where the info is logged * * servedfile must either start with http: (and be a * valid URL in this case) or be an absolute path starting with a /. * In either case it must be a valid html page. * * * @version 1.0, 97/09/06 * @author Ralph Freese */ public class GetAddressServlet extends HttpServlet { private String servedFile; private String logFileName; public void init(ServletConfig conf) throws ServletException { super.init(conf); servedFile = getInitParameter("servedfile"); logFileName = getInitParameter("logfile"); } public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doGet(req, res); } public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String name = req.getParameter("name"); String department = req.getParameter("department"); String schoolorcompany = req.getParameter("schoolorcompany"); String streetaddress = req.getParameter("streetaddress"); String city = req.getParameter("city"); String state = req.getParameter("state"); String zip = req.getParameter("zip"); String country = req.getParameter("country"); String nstudents = req.getParameter("nstudents"); String email = req.getParameter("email"); String emailme = req.getParameter("emailme"); InetAddress iaddr = InetAddress.getByName(req.getRemoteAddr()); res.setContentType("text/html"); ServletOutputStream out = res.getOutputStream(); // reject unless both name and email are entered if (name == null || email == null || !(email.indexOf('@') > 0)) { out.println(""); out.println("Incomple Form"); out.println(""); out.println("

Incomplete Form:

"); out.println("You must fill in (at least) your name and"); out.println("full email address"); out.println("You can press your back button to go back and "); out.println("and fill in the rest.

"); out.println(""); } else { // write info to the log file Date date = new Date(); try { FileWriter logFile = new FileWriter(logFileName, true); PrintWriter log = new PrintWriter(logFile); log.println("Date:\t" + date.toString()); log.println("Host:\t" + iaddr.getHostName()); log.println("Name:\t" + name); if (department != null && department.length() > 0) log.println("Dept:\t" + department); if (schoolorcompany != null && schoolorcompany.length() > 0) log.println("Sch/Co:\t" + schoolorcompany); if (streetaddress != null && streetaddress.length() > 0) { log.println("Addr:\t" + streetaddress); if ((city != null && city.length() > 0) || (state != null && state.length() > 0)) { log.println("\t" + city + ", " + state + " " + zip); } } else { if ((city != null && city.length() > 0) || (state != null && state.length() > 0)) { log.println("Addr:\t" + city + ", " + state + " " + zip); } } if (country != null && country.length() > 0) log.println("\t" + country); if (nstudents != null && nstudents.length() > 0) log.println("Stds:\t" + nstudents); log.println("Email:\t" + email); if (emailme != null && emailme.length() > 0) log.println("Wants email:\t" + emailme); log.println(""); logFile.close(); out.close(); res.getOutputStream().close(); } catch (IOException e) { System.out.println( "IO Exception writing the log file"); } // serve up the page. if (servedFile.startsWith("http:")) { res.sendRedirect(servedFile); } else { loadFile(servedFile, res); } } } public void loadFile (String file, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); ServletOutputStream out = res.getOutputStream(); FileReader in = new FileReader(file); char [] cbuf = new char[100]; String str; int len; while ((len = in.read(cbuf,0,100)) != -1) { str = new String(cbuf,0,len); out.print(str); } in.close(); } }