|
app1.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 app1 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 public void doGet(HttpServletRequest request, HttpServletResponse response)
26 throws ServletException, IOException {
27 HttpSession ses = request.getSession(true);
28 if (ses == null) {
29 HtmlPage page = new HtmlPage(response);
30 String s = "Sorry...";
31 page.title(s);
32 page.body();
33 page.center(true);
34 page.heading(1, s);
35 page.heading(2, "There are too many users to let you sign in");
36 page.out.println("Come back later");
37 page.close();
38 return;
39 }
40 String username = (String)ses.getValue(USERNAME);
41 if (ses.isNew() || (username == null)) signIn(request, response);
42 else acceptValues(request, response, ses);
43 }
44
45 public void doPost(HttpServletRequest request, HttpServletResponse response)
46 throws ServletException, IOException {
47 String action = request.getParameter(ACTION);
48 HttpSession ses = request.getSession(true);
49 String username = (String)ses.getValue(USERNAME);
50 if (LOGIN.equals(action)) {
51 ses.putValue(USERNAME, request.getParameter(USERNAME));
52 try {
53 Integer year = new Integer(request.getParameter(START_YEAR));
54 ses.putValue(START_YEAR, year);
55 ses.putValue(MORE_YEAR, year);
56 }
57 catch (NumberFormatException e) {
58 ses.putValue(START_YEAR, new Integer(1985));
59 ses.putValue(MORE_YEAR, new Integer(1985));
60 }
61 acceptValues(request, response, ses);
62 return;
63 }
64 if (ses.isNew() || (username == null)) signIn(request, response);
65 else if (MORE_DATA.equals(action)) {
66 appendData(request, response, ses);
67 acceptValues(request, response, ses);
68 }
69 else if (CHART.equals(action)) {
70 makeChart(request, response, ses);
71 }
72 else {
73 ses.invalidate();
74 signIn(request, response);
75 }
76 }
77
78 // Send the user here if there isn't any session data or it has become invalid
79 private void signIn(HttpServletRequest request, HttpServletResponse response)
80 throws ServletException, IOException {
81 HtmlPage page = new HtmlPage(response);
82 String s = "Compare salary to cost of living";
83 page.title(s);
84 page.body();
85 page.center(true);
86 page.heading(1, s);
87 page.heading(2, "Please sign in");
88 page.form(request.getServletPath());
89 page.table(0);
90 page.tField("Name:", USERNAME, "to print on report", 20, 40);
91 page.tField("Starting year:", START_YEAR, "1985", 20, 40);
92 page.row();
93 page.col();
94 page.col();
95 page.submit(ACTION, LOGIN);
96 page.table();
97 page.form();
98 page.close();
99 }
100
101 private void acceptValues(HttpServletRequest request, HttpServletResponse response,
102 HttpSession ses) throws ServletException, IOException {
103 HtmlPage page = new HtmlPage(response);
104 String s = "Data entry - " + (String)ses.getValue(USERNAME);
105 page.title(s);
106 page.body();
107 page.center(true);
108 page.heading(1, s);
109 int count = 0;
110 int start = 0;
111 try {
112 start = ((Integer)ses.getValue(START_YEAR)).intValue();
113 count = ((Integer)ses.getValue(SALARY_COUNT)).intValue();
114 }
115 catch (Exception e) {
116 }
117 page.heading(3, count + " values entered so far, starting with " + start);
118 String servlet = request.getServletPath();
119 page.form(servlet);
120 page.table(0);
121 page.row();
122 page.col();
123 page.out.println("Year");
124 page.col();
125 page.out.println("Salary (USD)");
126 Integer moreYear = (Integer)ses.getValue(MORE_YEAR);
127 int year = 1985;
128 if (moreYear != null) year = moreYear.intValue();
129 int n = year + 5;
130 while (year < n) {
131 page.row();
132 page.col();
133 page.out.println(year);
134 page.col();
135 page.field("sal" + year, "0", 10, 10);
136 ++year;
137 }
138 ses.putValue(MORE_YEAR, new Integer(year));
139 page.table();
140 page.submit(ACTION, MORE_DATA);
141 page.form();
142 page.form(servlet, CHART, "target=_blank");
143 page.submit(ACTION, CHART);
144 page.form();
145 page.form(servlet, LOGOUT);
146 page.submit(ACTION, LOGOUT);
147 page.form();
148 page.close();
149 }
150
151 private void appendData(HttpServletRequest request, HttpServletResponse response,
152 HttpSession ses) throws ServletException, IOException {
153 Enumeration parameterList = request.getParameterNames();
154 int count = 0;
155 try {
156 count = ((Integer)ses.getValue(SALARY_COUNT)).intValue();
157 }
158 catch (Exception e) {
159 }
160 while (parameterList.hasMoreElements()) {
161 String parm = (String)parameterList.nextElement();
162 Integer val = null;
163 try {
164 val = new Integer(request.getParameter(parm));
165 }
166 catch (NumberFormatException e) {
167 val = new Integer(0);
168 }
169 if (parm.startsWith("sal")) {
170 ++count;
171 ses.putValue(parm, val);
172 }
173 }
174 ses.putValue(SALARY_COUNT, new Integer(count));
175 }
176
177 private void makeChart(HttpServletRequest request, HttpServletResponse response,
178 HttpSession ses) throws ServletException, IOException {
179 response.setContentType("application/pdf");
180 PdfChart chart = new PdfChart(response.getOutputStream(),
181 (int)(11.0 * PdfChart.POINTS_PER_INCH), (int)(8.5 * PdfChart.POINTS_PER_INCH));
182 chart.setFont(chart.BOLD, 18);
183 chart.println((String)ses.getValue(USERNAME));
184 chart.setFont(chart.NORMAL, 12);
185 chart.println(new Date().toString());
186 chart.close();
187 }
188
189 }
|
app1.java |
|