|
app2.a.java |
|
1 package sutut1;
2
3 import javax.servlet.*;
4 import javax.servlet.http.*;
5 import java.io.*;
6 import java.util.*;
7
8 import us.oh.state.common.utility.*;
9
10 public class app2 extends HttpServlet {
11
12 // actions
13 private static final String LOGIN = "Proceed";
14 private static final String MORE_DATA = "Enter More";
15 private static final String CHART = "Chart";
16 private static final String LOGOUT = "Cancel";
17
18 // keys
19 private static final String ACTION = "action";
20 private static final String USERNAME = "username";
21 private static final String START_YEAR = "starting_year";
22 private static final String MORE_YEAR = "more_year";
23 private static final String SALARY_COUNT = "salary_count";
24
25 private Properties configOptions = null;
26
27 public void init(ServletConfig cfg) throws ServletException {
28 String confFileName = cfg.getInitParameter("configFile");
29 if ((confFileName == null) || (confFileName.length() == 0))
30 throw new ServletException("No configuration information found!");
31 configOptions = new Properties();
32 try {
33 FileInputStream propFile = new FileInputStream(confFileName);
34 configOptions.load(propFile);
35 propFile.close();
36 }
37 catch (IOException e) {
38 throw new ServletException("Can't read configuration from " + confFileName);
39 }
40 String dataFileName = configOptions.getProperty("cpidata");
41 if ((dataFileName == null) || (dataFileName.length() == 0))
42 throw new ServletException("No cpidata file name specified");
43 }
44
45 public void doGet(HttpServletRequest request, HttpServletResponse response)
46 throws ServletException, IOException {
47 HttpSession ses = request.getSession(true);
48 if (ses == null) {
49 HtmlPage page = new HtmlPage(response);
50 String s = "Sorry...";
51 page.title(s);
52 page.body();
53 page.center(true);
54 page.heading(1, s);
55 page.heading(2, "There are too many users to let you sign in");
56 page.out.println("Come back later");
57 page.close();
58 return;
59 }
60 String username = (String)ses.getValue(USERNAME);
61 if (ses.isNew() || (username == null)) signIn(request, response);
62 else acceptValues(request, response, ses);
63 }
64
65 public void doPost(HttpServletRequest request, HttpServletResponse response)
66 throws ServletException, IOException {
67 String action = request.getParameter(ACTION);
68 HttpSession ses = request.getSession(true);
69 String username = (String)ses.getValue(USERNAME);
70 if (LOGIN.equals(action)) {
71 ses.putValue(USERNAME, request.getParameter(USERNAME));
72 try {
73 Integer year = new Integer(request.getParameter(START_YEAR));
74 ses.putValue(START_YEAR, year);
75 ses.putValue(MORE_YEAR, year);
76 }
77 catch (NumberFormatException e) {
78 ses.putValue(START_YEAR, new Integer(1985));
79 ses.putValue(MORE_YEAR, new Integer(1985));
80 }
81 acceptValues(request, response, ses);
82 return;
83 }
84 if (ses.isNew() || (username == null)) signIn(request, response);
85 else if (MORE_DATA.equals(action)) {
86 appendData(request, response, ses);
87 acceptValues(request, response, ses);
88 }
89 else if (CHART.equals(action)) {
90 makeChart(request, response, ses);
91 }
92 else {
93 ses.invalidate();
94 signIn(request, response);
95 }
96 }
97
98 // Send the user here if there isn't any session data or it has become invalid
99 private void signIn(HttpServletRequest request, HttpServletResponse response)
100 throws ServletException, IOException {
101 HtmlPage page = new HtmlPage(response);
102 String s = "Compare salary to cost of living";
103 page.title(s);
104 page.body();
105 page.center(true);
106 page.heading(1, s);
107 page.heading(2, "Please sign in");
108 page.form(request.getServletPath());
109 page.table(0);
110 page.tField("Name:", USERNAME, "to print on report", 20, 40);
111 page.tField("Starting year:", START_YEAR, "1985", 20, 40);
112 page.row();
113 page.col();
114 page.col();
115 page.submit(ACTION, LOGIN);
116 page.table();
117 page.form();
118 page.close();
119 }
120
121 private void acceptValues(HttpServletRequest request, HttpServletResponse response,
122 HttpSession ses) throws ServletException, IOException {
123 HtmlPage page = new HtmlPage(response);
124 String s = "Data entry - " + (String)ses.getValue(USERNAME);
125 page.title(s);
126 page.body();
127 page.center(true);
128 page.heading(1, s);
129 int count = 0;
130 int start = 0;
131 try {
132 start = ((Integer)ses.getValue(START_YEAR)).intValue();
133 count = ((Integer)ses.getValue(SALARY_COUNT)).intValue();
134 }
135 catch (Exception e) {
136 }
137 page.heading(3, count + " values entered so far, starting with " + start);
138 String servlet = request.getServletPath();
139 page.form(servlet);
140 page.table(0);
141 page.row();
142 page.col();
143 page.out.println("Year");
144 page.col();
145 page.out.println("Salary (USD)");
146 Integer moreYear = (Integer)ses.getValue(MORE_YEAR);
147 int year = 1985;
148 if (moreYear != null) year = moreYear.intValue();
149 int n = year + 5;
150 while (year < n) {
151 page.row();
152 page.col();
153 page.out.println(year);
154 page.col();
155 page.field("sal" + year, "0", 10, 10);
156 ++year;
157 }
158 ses.putValue(MORE_YEAR, new Integer(year));
159 page.table();
160 page.submit(ACTION, MORE_DATA);
161 page.form();
162 page.form(servlet, CHART, "target=_blank");
163 page.submit(ACTION, CHART);
164 page.form();
165 page.form(servlet, LOGOUT);
166 page.submit(ACTION, LOGOUT);
167 page.form();
168 page.close();
169 }
170
171 private void appendData(HttpServletRequest request, HttpServletResponse response,
172 HttpSession ses) throws ServletException, IOException {
173 Enumeration parameterList = request.getParameterNames();
174 int count = 0;
175 try {
176 count = ((Integer)ses.getValue(SALARY_COUNT)).intValue();
177 }
178 catch (Exception e) {
179 }
180 while (parameterList.hasMoreElements()) {
181 String parm = (String)parameterList.nextElement();
182 Integer val = null;
183 try {
184 val = new Integer(request.getParameter(parm));
185 }
186 catch (NumberFormatException e) {
187 val = new Integer(0);
188 }
189 if (parm.startsWith("sal")) {
190 ++count;
191 ses.putValue(parm, val);
192 }
193 }
194 ses.putValue(SALARY_COUNT, new Integer(count));
195 }
196
197 private void makeChart(HttpServletRequest request, HttpServletResponse response,
198 HttpSession ses) throws ServletException, IOException {
199 response.setContentType("application/pdf");
200 PdfChart chart = new PdfChart(response.getOutputStream(),
201 (int)(11.0 * PdfChart.POINTS_PER_INCH), (int)(8.5 * PdfChart.POINTS_PER_INCH));
202 chart.setTextColor(0x0);
203 chart.setFont(chart.BOLD, 18);
204 chart.println((String)ses.getValue(USERNAME));
205 chart.setFont(chart.NORMAL, 12);
206 chart.println(new Date().toString());
207 chart.drawMain(1.0, 2.0, 9.0, 5.0, 5, 4, 0xdddddd, 1.5, 0x0);
208 chart.close();
209 }
210
211 }
|
app2.a.java |
|