KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > espada > bugtracker > servlets > StartPage


1 /*
2 ====================================================================
3 Project Name: bugtracker
4 File Name: /src/com/espada/bugtracker/servlets/StartPage.java
5 Author: Kishan Peiris <kishan@espadanet.com>
6 Description: The Initial page for bugtracker
7 CVS Module: bugtracker
8 ====================================================================
9
10 */

11
12 package com.espada.bugtracker.servlets;
13
14 // webmacro resources
15
import org.webmacro.*;
16 import org.webmacro.broker.*;
17 import org.webmacro.resource.*;
18 import org.webmacro.servlet.WebContext;
19
20 // servlet libraries
21
import javax.servlet.http.*;
22 import javax.servlet.*;
23
24 // bugtracker java apps
25
import com.espada.bugtracker.app.*;
26 import com.espada.bugtracker.util.*;
27
28 // log4j
29
import org.apache.log4j.*;
30
31 public class StartPage extends BTServlet
32 {
33
34    /**
35      * This is the core WebMacro interface which we use to create Contexts,
36      * load Templates, and begin other WebMacro operations.
37      */

38
39    /** the default template to use **/
40    private static final String JavaDoc defaultTemplate="index.wm";
41    
42    // initializing the log4j logger for this class.
43
static Category logger = Category.getInstance( StartPage.class.getName() );
44
45
46
47    private void initConfiguration()
48    {
49
50        //******************************************************************************/
51
//********** INITIALIZATION *************************/
52
//
53
// Let us initialize the configuration.
54
// This would only have to be done *once* every time the server is started!!
55
// -----------------------------------------------------------------------------
56

57        // let us initialize it.
58

59        try{
60        if ( ! PropertyFactory.isInitialized() )
61                PropertyFactory.readProperties(
62                     getServletContext().getRealPath(
63                         getServletContext().getInitParameter("btpropfile")
64                         )
65                     );
66     
67        } catch (java.io.IOException JavaDoc ioex) { System.out.println("Cannot create props! Big problemo!"); }
68        
69                     
70                     
71                // we assume that nothing is init'd. we even need to init log4j.
72

73            PropertyConfigurator.configure(
74               PropertyFactory.getInstance() );
75          
76                             
77                     
78
79
80
81    } //end of method
82

83
84    /**
85      */

86    public void doGet(HttpServletRequest req, HttpServletResponse resp)
87    {
88       // initialize WebMacro.
89
initConfiguration();
90     
91        try
92        {
93          try
94          {
95
96             // create a context for the current request
97
WebContext c = _wm.getWebContext(req,resp);
98
99             /******************************************************************/
100
101             HttpSession session = req.getSession();
102             
103             logger.debug("Invalidated HttpSession.");
104             
105             session.invalidate(); //discard the old values..
106

107             session = req.getSession(true); //create a new session..
108
boolean loggedIn = false;
109             String JavaDoc s = "http://" + req.getServerName();
110
111             session.setAttribute("loggedIn",String.valueOf(loggedIn));
112             c.put("serverName", s);
113             c.put("loggedIn",String.valueOf(loggedIn));
114
115             /*****************************************************************/
116
117             // get the template we intend to execute
118
Template t = _wm.getTemplate(defaultTemplate);
119
120             // Create FastWriter for fast output encoding
121
FastWriter fw = new FastWriter(resp.getOutputStream(),resp.getCharacterEncoding());
122
123             // write the template to the output, using our context
124
t.write(fw, c);
125             fw.close();
126
127
128
129           }
130              catch (org.webmacro.NotFoundException e)
131              {
132
133                 FastWriter out = new FastWriter(resp.getOutputStream(),resp.getCharacterEncoding());
134
135                 out.write("ERROR! Could not locate template " + defaultTemplate + ", check that your template path is set properly in WebMacro.properties");
136
137                 out.close();
138
139              }
140              catch (org.webmacro.ContextException e)
141              {
142
143                 FastWriter out = new FastWriter(resp.getOutputStream(),resp.getCharacterEncoding());
144
145                 out.write("ERROR! Could not locate required data in the Context.");
146
147                 out.close();
148              }
149       }
150          catch (java.io.IOException JavaDoc e)
151          {
152
153             // what else can we do?
154
System.out.println("ERROR: IOException while writing to servlet output stream.");
155
156          }
157
158     } //end of method
159

160
161 } //end of class
Popular Tags