/Users/lyon/j4p/src/bookExamples/ch05ControlStructs/AddressDispatchProcessor.java

1    /** 
2     * Created by IntelliJ IDEA. 
3     * User: dlyon 
4     * Date: Nov 6, 2003 
5     * Time: 7:31:53 PM 
6     * To change this template use Options | File Templates. 
7     */ 
8    package bookExamples.ch05ControlStructs; 
9     
10   import javax.servlet.http.HttpServletResponse; 
11   import javax.servlet.http.HttpServletRequest; 
12   import javax.servlet.http.HttpSession; 
13   import java.io.PrintWriter; 
14   import java.io.IOException; 
15    
16   // examples.stringswitch.DispatchProcessor.doLogin 
17    
18   public class AddressDispatchProcessor { 
19       private final static ServletDispatcher sd = initServletDispatcher(); 
20    
21       public static void doLogin(HttpServletRequest request, 
22                                  HttpServletResponse response) 
23               throws IOException { 
24           PrintWriter out = response.getWriter(); 
25           HttpSession hs = request.getSession(); 
26           hs.putValue("loggedIn", "false"); 
27           out.println("<h1>here is the login page</h1>"); 
28       } 
29    
30       public static void doLogout(HttpServletRequest request, 
31                                   HttpServletResponse response) { 
32           try { 
33               PrintWriter out = response.getWriter(); 
34               HttpSession hs = request.getSession(); 
35               hs.putValue("loggedIn", "false"); 
36               out.println("<h1>here is the logout page</h1>"); 
37               out.println("<h1>goodbye world</h1>"); 
38           } catch (IOException e) { 
39               e.printStackTrace();  //To change body of catch statement use Options | File Templates. 
40           } 
41       } 
42    
43       // examples.stringswitch.ServletDispatcherTest.getServletDispatcher(); 
44       public static ServletDispatcher initServletDispatcher() { 
45           ServletDispatcher sd = new ServletDispatcher(); 
46           sd.add(new DoLoginDispatchCommand()); 
47           sd.add(new DispatchCommand("doLogout") { 
48               public void run(HttpServletRequest request, HttpServletResponse response) { 
49                   doLogout(request, response); 
50               } 
51           }); 
52           return sd; 
53       } 
54    
55       public static ServletDispatcher getServletDispatcher() { 
56           return sd; 
57       } 
58    
59       private static class DoLoginDispatchCommand extends DispatchCommand { 
60           public DoLoginDispatchCommand() { 
61               super("doLogin"); 
62           } 
63    
64           public void run(HttpServletRequest request, HttpServletResponse response) { 
65               try { 
66    
67                   doLogin(request, response); 
68               } catch (IOException e) { 
69                   e.printStackTrace();  //To change body of catch statement use Options | File Templates. 
70               } 
71           } 
72       } 
73   } 
74