1
- package com .MyPackage ;
2
-
3
1
/*
4
2
* To change this license header, choose License Headers in Project Properties.
5
3
* To change this template file, choose Tools | Templates
6
4
* and open the template in the editor.
7
5
*/
6
+ package com .MyPackage ;
8
7
8
+ import static com .MyPackage .MainServlet .SQL_DRIVER ;
9
9
import java .io .IOException ;
10
10
import java .io .PrintWriter ;
11
11
import java .sql .Connection ;
12
12
import java .sql .DriverManager ;
13
- import java .sql .PreparedStatement ;
14
13
import java .sql .ResultSet ;
15
- import java .sql .SQLDataException ;
16
14
import java .sql .SQLException ;
17
15
import java .sql .Statement ;
18
- import java .util .logging .Level ;
19
- import java .util .logging .Logger ;
20
16
import javax .naming .InitialContext ;
21
17
import javax .naming .NamingException ;
22
18
import javax .servlet .RequestDispatcher ;
23
19
import javax .servlet .ServletConfig ;
24
20
import javax .servlet .ServletException ;
25
- import javax .servlet .annotation .WebServlet ;
26
21
import javax .servlet .http .HttpServlet ;
27
22
import javax .servlet .http .HttpServletRequest ;
28
23
import javax .servlet .http .HttpServletResponse ;
29
- import javax .servlet .jsp .HttpJspPage ;
30
24
import javax .sql .DataSource ;
31
25
32
26
/**
33
27
*
34
- * @author Jesper
28
+ * @author hyzor
35
29
*/
36
- @ WebServlet (urlPatterns = {"/NewServlet" })
37
- public abstract class NewServlet extends HttpServlet implements HttpJspPage {
38
-
30
+ public class MainServlet extends HttpServlet {
31
+
39
32
InitialContext ctx = null ;
40
33
DataSource ds = null ;
41
34
protected Connection conn = null ;
42
35
Statement statement = null ;
43
36
ResultSet rs = null ;
44
37
SqlBean sqlBean = null ;
45
-
46
- //static final String JDBC_DRIVER="org.hsqldb.jdbc.JDBCDriver";
47
- static final String DB_URL ="jdbc:derby://localhost:1527/ourDatabase" ;
48
38
49
- String sql = "SELECT * FROM APP.USERS" ;
39
+ static final String SQL_DB_URL ="jdbc:derby://localhost:1527/MyDatabase" ;
40
+ static final String SQL_DRIVER ="org.apache.derby.jdbc.ClientDriver" ;
41
+ static final String SQL_USERNAME = "test" ;
42
+ static final String SQL_PASSWORD = "test123" ;
43
+
44
+ @ Override
45
+ public final void init (ServletConfig config ) throws ServletException {
46
+ super .init (config );
47
+ }
50
48
51
- /*
49
+ /**
52
50
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
53
51
* methods.
54
52
*
@@ -57,38 +55,33 @@ public abstract class NewServlet extends HttpServlet implements HttpJspPage {
57
55
* @throws ServletException if a servlet-specific error occurs
58
56
* @throws IOException if an I/O error occurs
59
57
*/
60
- protected final void processRequest (HttpServletRequest request , HttpServletResponse response )
58
+ protected void processRequest (HttpServletRequest request , HttpServletResponse response )
61
59
throws ServletException , IOException {
62
60
response .setContentType ("text/html;charset=UTF-8" );
63
61
try (PrintWriter out = response .getWriter ()) {
64
62
/* TODO output your page here. You may use following sample code. */
65
63
out .println ("<!DOCTYPE html>" );
66
64
out .println ("<html>" );
67
65
out .println ("<head>" );
68
- out .println ("<title>Servlet NewServlet </title>" );
66
+ out .println ("<title>Servlet MainServlet2 </title>" );
69
67
out .println ("</head>" );
70
68
out .println ("<body>" );
71
- out .println ("<h1>Servlet NewServlet at " + request .getContextPath () + "</h1>" );
69
+ out .println ("<h1>Servlet MainServlet2 at " + request .getContextPath () + "</h1>" );
72
70
out .println ("</body>" );
73
71
out .println ("</html>" );
74
72
}
75
73
}
76
-
77
- @ Override
78
- public final void init (ServletConfig config ) throws ServletException {
79
- super .init (config );
74
+
75
+ protected void processRequest_Get (HttpServletRequest request , HttpServletResponse response )
76
+ throws ServletException , IOException {
77
+ response .setContentType ("text/html;charset=UTF-8" );
78
+
80
79
try {
81
80
System .out .println ("Connecting to database..." );
82
81
ctx = new InitialContext ();
83
- //ds = (DataSource) ctx.lookup("java:comp/env/jdbc/hsqldb");
84
- //conn = ds.getConnection();
85
- Class .forName ("org.apache.derby.jdbc.ClientDriver" ).newInstance ();
86
- conn = DriverManager .getConnection (DB_URL , "test" , "tgdas" );
87
-
82
+ Class .forName (SQL_DRIVER ).newInstance ();
83
+ conn = DriverManager .getConnection (SQL_DB_URL , SQL_USERNAME , SQL_PASSWORD );
88
84
System .out .println ("Connected!" );
89
- //ps = conn.cr(sql);
90
- //statement = conn.createStatement();
91
- //rs = statement.executeQuery(sql);
92
85
93
86
sqlBean = new SqlBean ();
94
87
sqlBean .setConnection (conn );
@@ -97,39 +90,27 @@ public final void init(ServletConfig config) throws ServletException {
97
90
} catch (NamingException ne ) {
98
91
System .out .println ("NamingException: " + ne .getMessage ());
99
92
} catch (ClassNotFoundException ex ) {
100
- Logger .getLogger (NewServlet .class .getName ()).log (Level .SEVERE , null , ex );
93
+ //Logger.getLogger(MainServlet.class.getName()).log(Level.SEVERE, null, ex);
94
+ System .out .println ("ClassNotFoundException: " + ex .getMessage ());
101
95
} catch (InstantiationException ex ) {
102
- Logger .getLogger (NewServlet .class .getName ()).log (Level .SEVERE , null , ex );
96
+ //Logger.getLogger(MainServlet.class.getName()).log(Level.SEVERE, null, ex);
97
+ System .out .println ("InstantiationException: " + ex .getMessage ());
103
98
} catch (IllegalAccessException ex ) {
104
- Logger .getLogger (NewServlet .class .getName ()).log (Level .SEVERE , null , ex );
99
+ //Logger.getLogger(MainServlet.class.getName()).log(Level.SEVERE, null, ex);
100
+ System .out .println ("IllegalAccessException: " + ex .getMessage ());
105
101
}
102
+
103
+ RequestDispatcher rd = null ;
106
104
107
- jspInit ();
108
- }
109
-
110
- @ Override
111
- public final void destroy () {
112
- try {
113
- if (rs != null ) {
114
- rs .close ();
115
- }
116
- if (statement != null ) {
117
- statement .close ();
118
- }
119
- if (conn != null ) {
120
- conn .close ();
121
- }
122
- if (ctx != null ) {
123
- ctx .close ();
124
- }
125
- } catch (SQLException se ) {
126
- System .out .println ("SQLException: " + se .getMessage ());
127
- } catch (NamingException ne ) {
128
- System .out .println ("NamingException: " + ne .getMessage ());
105
+ if (request .getAttribute ("username" ) == null ) {
106
+ rd = request .getRequestDispatcher ("loginPage.jsp" );
107
+ } else {
108
+ rd = request .getRequestDispatcher ("home.jsp" );
129
109
}
130
110
131
- jspDestroy ();
132
- super .destroy ();
111
+ request .setAttribute ("sqlBean" , sqlBean );
112
+ request .getSession ().setAttribute ("sqlBean" , sqlBean );
113
+ rd .forward (request , response );
133
114
}
134
115
135
116
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
@@ -142,18 +123,9 @@ public final void destroy() {
142
123
* @throws IOException if an I/O error occurs
143
124
*/
144
125
@ Override
145
- protected final void doGet (HttpServletRequest request , HttpServletResponse response )
126
+ protected void doGet (HttpServletRequest request , HttpServletResponse response )
146
127
throws ServletException , IOException {
147
- //SqlBean sqlBean = new SqlBean();
148
- //sqlBean.setConnection(conn);
149
-
150
-
151
-
152
- RequestDispatcher rd = request .getRequestDispatcher ("loginPage.jsp" );
153
- request .setAttribute ("sqlBean" , sqlBean );
154
- //request.setAttribute("conn", conn);
155
- rd .forward (request , response );
156
- //processRequest(request, response);
128
+ processRequest_Get (request , response );
157
129
}
158
130
159
131
/**
@@ -165,7 +137,7 @@ protected final void doGet(HttpServletRequest request, HttpServletResponse respo
165
137
* @throws IOException if an I/O error occurs
166
138
*/
167
139
@ Override
168
- protected final void doPost (HttpServletRequest request , HttpServletResponse response )
140
+ protected void doPost (HttpServletRequest request , HttpServletResponse response )
169
141
throws ServletException , IOException {
170
142
processRequest (request , response );
171
143
}
@@ -176,43 +148,32 @@ protected final void doPost(HttpServletRequest request, HttpServletResponse resp
176
148
* @return a String containing servlet description
177
149
*/
178
150
@ Override
179
- public final String getServletInfo () {
151
+ public String getServletInfo () {
180
152
return "Short description" ;
181
153
}// </editor-fold>
182
-
183
- /**
184
- * Called when the JSP is loaded.
185
- * By default does nothing.
186
- */
187
- @ Override
188
- public void jspInit () {}
189
-
190
- /**
191
- * Called when the JSP is unloaded.
192
- * By default does nothing.
193
- */
194
- @ Override
195
- public void jspDestroy () {}
196
-
197
- /**
198
- * Invokes the JSP's _jspService method.
199
- */
200
- @ Override
201
- public final void service (
202
- HttpServletRequest request ,
203
- HttpServletResponse response )
204
- throws ServletException , IOException
205
- {
206
- _jspService (request , response );
207
- }
208
154
209
- /**
210
- * Handles a service request.
211
- */
155
+
212
156
@ Override
213
- public abstract void _jspService (
214
- HttpServletRequest request ,
215
- HttpServletResponse response )
216
- throws ServletException , IOException ;
217
-
157
+ public final void destroy () {
158
+ try {
159
+ if (rs != null ) {
160
+ rs .close ();
161
+ }
162
+ if (statement != null ) {
163
+ statement .close ();
164
+ }
165
+ if (conn != null ) {
166
+ conn .close ();
167
+ }
168
+ if (ctx != null ) {
169
+ ctx .close ();
170
+ }
171
+ } catch (SQLException se ) {
172
+ System .out .println ("SQLException: " + se .getMessage ());
173
+ } catch (NamingException ne ) {
174
+ System .out .println ("NamingException: " + ne .getMessage ());
175
+ }
176
+
177
+ super .destroy ();
178
+ }
218
179
}
0 commit comments