|
|
| |||||||
| |||||||||
|
| 2.2 A Form Processing ServletThis section shows how to
The next Servlet that we are going to write provides a user interface to a mailing list through HTML forms. A user should be able to enter an email address in a text field and press a button to subscribe to the list or another button to unsubscribe. The Servlet consists of two major parts: Data managment and client interaction. Data managementThe data management is rather straight-forward for an
experienced Java programmer. We use a The following parts of the Servlet are related to data management: 8: private Vector addresses; 9: private String filename; 10: 11: public void init(ServletConfig config) throws ServletException 12: { 13: super.init(config); 14: filename = config.getInitParameter("addressfile"); 15: if(filename == null) 16: throw new UnavailableException(this, 17: "The \"addressfile\" property "+ 18: "must be set to a file name"); 19: try 20: { 21: ObjectInputStream in = 22: new ObjectInputStream(new FileInputStream(filename)); 23: addresses = (Vector)in.readObject(); 24: in.close(); 25: } 26: catch(FileNotFoundException e) { addresses = new Vector(); } 27: catch(Exception e) 28: { 29: throw new UnavailableException(this, 30: "Error reading address file: "+e); 31: } 32: } 104: private synchronized boolean subscribe(String email) throws IOException 105: { 106: if(addresses.contains(email)) return false; 107: addresses.addElement(email); 108: save(); 109: return true; 110: } 111: 112: private synchronized boolean unsubscribe(String email) throws IOException 113: { 114: if(!addresses.removeElement(email)) return false; 115: save(); 116: return true; 117: } 118: 119: private void save() throws IOException 120: { 121: ObjectOutputStream out = 122: new ObjectOutputStream(new FileOutputStream(filename)); 123: out.writeObject(addresses); 124: out.close(); 125: } In [API 2.1]
Version 2.1 of the Servlet API offers a no-args Note that even though code that uses the no-args The methods The Client interactionThe client interaction is handled by two of the standard
As usual, the Servlet extends 1: import java.util.Vector; 2: import java.io.*; 3: import javax.servlet.*; 4: import javax.servlet.http.*; 5: 6: public class ListManagerServlet extends HttpServlet 7: { 8: private Vector addresses; 9: private String filename; 10: 11: public void init(ServletConfig config) throws ServletException 12: { 13: super.init(config); 14: filename = config.getInitParameter("addressfile"); 15: if(filename == null) 16: throw new UnavailableException(this, 17: "The \"addressfile\" property "+ 18: "must be set to a file name"); 19: try 20: { 21: ObjectInputStream in = 22: new ObjectInputStream(new FileInputStream(filename)); 23: addresses = (Vector)in.readObject(); 24: in.close(); 25: } 26: catch(FileNotFoundException e) { addresses = new Vector(); } 27: catch(Exception e) 28: { 29: throw new UnavailableException(this, 30: "Error reading address file: "+e); 31: } 32: } 33: 34: protected void doGet(HttpServletRequest req, 35: HttpServletResponse res) 36: throws ServletException, IOException 37: { 38: res.setContentType("text/html"); 39: res.setHeader("pragma", "no-cache"); 40: PrintWriter out = res.getWriter(); 41: out.print("<HTML><HEAD><TITLE>List Manager</TITLE></HEAD>"); 42: out.print("<BODY><H3>Members:</H3><UL>"); 43: for(int i=0; i<addresses.size(); i++) 44: out.print("<LI>" + addresses.elementAt(i)); 45: out.print("</UL><HR><FORM METHOD=POST>"); 46: out.print("Enter your email address: <INPUT TYPE=TEXT NAME=email><BR>"); 47: out.print("<INPUT TYPE=SUBMIT NAME=action VALUE=subscribe>"); 48: out.print("<INPUT TYPE=SUBMIT NAME=action VALUE=unsubscribe>"); 49: out.print("</FORM></BODY></HTML>"); 50: out.close(); 51: } 52: 53: protected void doPost(HttpServletRequest req, 54: HttpServletResponse res) 55: throws ServletException, IOException 56: { 57: String email = req.getParameter("email"); 58: String msg; 59: if(email == null) 60: { 61: res.sendError(res.SC_BAD_REQUEST, 62: "No email address specified."); 63: return; 64: } 65: if(req.getParameter("action").equals("subscribe")) 66: { 67: if(subscribe(email)) 68: msg = "Address " + email + " has been subscribed."; 69: else 70: { 71: res.sendError(res.SC_BAD_REQUEST, 72: "Address " + email + " was already subscribed."); 73: return; 74: } 75: } 76: else 77: { 78: if(unsubscribe(email)) 79: msg = "Address " + email + " has been removed."; 80: else 81: { 82: res.sendError(res.SC_BAD_REQUEST, 83: "Address " + email + " was not subscribed."); 84: return; 85: } 86: } 87: 88: res.setContentType("text/html"); 89: res.setHeader("pragma", "no-cache"); 90: PrintWriter out = res.getWriter(); 91: out.print("<HTML><HEAD><TITLE>List Manager</TITLE></HEAD><BODY>"); 92: out.print(msg); 93: out.print("<HR><A HREF=\""); 94: out.print(req.getRequestURI()); 95: out.print("\">Show the list</A></BODY></HTML>"); 96: out.close(); 97: } 98: 99: public String getServletInfo() 100: { 101: return "ListManagerServlet 1.0 by Stefan Zeiger"; 102: } 103: 104: private synchronized boolean subscribe(String email) throws IOException 105: { 106: if(addresses.contains(email)) return false; 107: addresses.addElement(email); 108: save(); 109: return true; 110: } 111: 112: private synchronized boolean unsubscribe(String email) throws IOException 113: { 114: if(!addresses.removeElement(email)) return false; 115: save(); 116: return true; 117: } 118: 119: private void save() throws IOException 120: { 121: ObjectOutputStream out = 122: new ObjectOutputStream(new FileOutputStream(filename)); 123: out.writeObject(addresses); 124: out.close(); 125: } 126: } |
| ||||||
|
This page's content was last updated 1999-11-04. © 1997-2005 Stefan Zeiger (http://www.novocode.com/contact.html). All rights reserved. Page Location: http://www.novocode.com/doc/servlet-essentials/chapter2b.html |
| |||||