Skip to content

Commit 57cc221

Browse files
committed
Fixed initial commit (TODO: Registration functionality)
1 parent a4387c0 commit 57cc221

File tree

6 files changed

+127
-132
lines changed

6 files changed

+127
-132
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ nbdist/
2222
nbactions.xml
2323
nb-configuration.xml
2424
.nb-gradle/
25+
/EnterpriseApplication1-war/nbproject/private/
26+
/EnterpriseApplication1-ejb/nbproject/private/
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,52 @@
1-
package com.MyPackage;
2-
31
/*
42
* To change this license header, choose License Headers in Project Properties.
53
* To change this template file, choose Tools | Templates
64
* and open the template in the editor.
75
*/
6+
package com.MyPackage;
87

8+
import static com.MyPackage.MainServlet.SQL_DRIVER;
99
import java.io.IOException;
1010
import java.io.PrintWriter;
1111
import java.sql.Connection;
1212
import java.sql.DriverManager;
13-
import java.sql.PreparedStatement;
1413
import java.sql.ResultSet;
15-
import java.sql.SQLDataException;
1614
import java.sql.SQLException;
1715
import java.sql.Statement;
18-
import java.util.logging.Level;
19-
import java.util.logging.Logger;
2016
import javax.naming.InitialContext;
2117
import javax.naming.NamingException;
2218
import javax.servlet.RequestDispatcher;
2319
import javax.servlet.ServletConfig;
2420
import javax.servlet.ServletException;
25-
import javax.servlet.annotation.WebServlet;
2621
import javax.servlet.http.HttpServlet;
2722
import javax.servlet.http.HttpServletRequest;
2823
import javax.servlet.http.HttpServletResponse;
29-
import javax.servlet.jsp.HttpJspPage;
3024
import javax.sql.DataSource;
3125

3226
/**
3327
*
34-
* @author Jesper
28+
* @author hyzor
3529
*/
36-
@WebServlet(urlPatterns = {"/NewServlet"})
37-
public abstract class NewServlet extends HttpServlet implements HttpJspPage {
38-
30+
public class MainServlet extends HttpServlet {
31+
3932
InitialContext ctx = null;
4033
DataSource ds = null;
4134
protected Connection conn = null;
4235
Statement statement = null;
4336
ResultSet rs = null;
4437
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";
4838

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+
}
5048

51-
/*
49+
/**
5250
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
5351
* methods.
5452
*
@@ -57,38 +55,33 @@ public abstract class NewServlet extends HttpServlet implements HttpJspPage {
5755
* @throws ServletException if a servlet-specific error occurs
5856
* @throws IOException if an I/O error occurs
5957
*/
60-
protected final void processRequest(HttpServletRequest request, HttpServletResponse response)
58+
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
6159
throws ServletException, IOException {
6260
response.setContentType("text/html;charset=UTF-8");
6361
try (PrintWriter out = response.getWriter()) {
6462
/* TODO output your page here. You may use following sample code. */
6563
out.println("<!DOCTYPE html>");
6664
out.println("<html>");
6765
out.println("<head>");
68-
out.println("<title>Servlet NewServlet</title>");
66+
out.println("<title>Servlet MainServlet2</title>");
6967
out.println("</head>");
7068
out.println("<body>");
71-
out.println("<h1>Servlet NewServlet at " + request.getContextPath() + "</h1>");
69+
out.println("<h1>Servlet MainServlet2 at " + request.getContextPath() + "</h1>");
7270
out.println("</body>");
7371
out.println("</html>");
7472
}
7573
}
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+
8079
try {
8180
System.out.println("Connecting to database...");
8281
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);
8884
System.out.println("Connected!");
89-
//ps = conn.cr(sql);
90-
//statement = conn.createStatement();
91-
//rs = statement.executeQuery(sql);
9285

9386
sqlBean = new SqlBean();
9487
sqlBean.setConnection(conn);
@@ -97,39 +90,27 @@ public final void init(ServletConfig config) throws ServletException {
9790
} catch (NamingException ne) {
9891
System.out.println("NamingException: " + ne.getMessage());
9992
} 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());
10195
} 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());
10398
} 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());
105101
}
102+
103+
RequestDispatcher rd = null;
106104

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");
129109
}
130110

131-
jspDestroy();
132-
super.destroy();
111+
request.setAttribute("sqlBean", sqlBean);
112+
request.getSession().setAttribute("sqlBean", sqlBean);
113+
rd.forward(request, response);
133114
}
134115

135116
// <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() {
142123
* @throws IOException if an I/O error occurs
143124
*/
144125
@Override
145-
protected final void doGet(HttpServletRequest request, HttpServletResponse response)
126+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
146127
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);
157129
}
158130

159131
/**
@@ -165,7 +137,7 @@ protected final void doGet(HttpServletRequest request, HttpServletResponse respo
165137
* @throws IOException if an I/O error occurs
166138
*/
167139
@Override
168-
protected final void doPost(HttpServletRequest request, HttpServletResponse response)
140+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
169141
throws ServletException, IOException {
170142
processRequest(request, response);
171143
}
@@ -176,43 +148,32 @@ protected final void doPost(HttpServletRequest request, HttpServletResponse resp
176148
* @return a String containing servlet description
177149
*/
178150
@Override
179-
public final String getServletInfo() {
151+
public String getServletInfo() {
180152
return "Short description";
181153
}// </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-
}
208154

209-
/**
210-
* Handles a service request.
211-
*/
155+
212156
@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+
}
218179
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
3+
<servlet>
4+
<servlet-name>MainServlet2</servlet-name>
5+
<servlet-class>com.MyPackage.MainServlet</servlet-class>
6+
</servlet>
7+
<servlet-mapping>
8+
<servlet-name>MainServlet2</servlet-name>
9+
<url-pattern>/MainServlet2</url-pattern>
10+
</servlet-mapping>
11+
<session-config>
12+
<session-timeout>
13+
30
14+
</session-timeout>
15+
</session-config>
16+
</web-app>

EnterpriseApplication1-war/web/index.html

-16
This file was deleted.
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<%--
2+
Document : index
3+
Created on : Apr 20, 2015, 8:03:41 PM
4+
Author : hyzor
5+
--%>
6+
<jsp:forward page="/MainServlet2" />
7+
8+
<%@page contentType="text/html" pageEncoding="UTF-8"%>
9+
<!DOCTYPE html>
10+
<html>
11+
<head>
12+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
13+
<title>JSP Page</title>
14+
</head>
15+
<body>
16+
<%
17+
RequestDispatcher rd = null;
18+
19+
if (session.getAttribute("username") == null) {
20+
rd = request.getRequestDispatcher("loginPage.jsp");
21+
} else {
22+
rd = request.getRequestDispatcher("home.jsp");
23+
}
24+
25+
rd.forward(request, response);
26+
%>
27+
</body>
28+
</html>

EnterpriseApplication1-war/web/loginCheck.jsp

+14-10
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
<%@page import="java.sql.Connection"%>
1212
<%@page contentType="text/html" pageEncoding="UTF-8"%>
1313

14-
<jsp:useBean id="sqlSessionBean" class="com.MyPackage.SqlBean" />
15-
1614
<!DOCTYPE html>
1715
<html>
1816
<head>
@@ -23,19 +21,25 @@
2321
<%
2422
String username = request.getParameter("username");
2523
String password = request.getParameter("password");
26-
27-
Statement statement = sqlSessionBean.getConnection().createStatement();
28-
String sql = "SELECT Password FROM APP.USERS WHERE Username="+username;
24+
25+
SqlBean sqlBean = (SqlBean)session.getAttribute("sqlBean");
26+
27+
Statement statement = sqlBean.getConnection().createStatement();
28+
29+
String sql = "SELECT Password FROM TEST.USERS WHERE Username="+"'"+username+"'";
2930
ResultSet rs = statement.executeQuery(sql);
3031
31-
if (rs.next() == true) {
32-
if (password == rs.getString("password")) {
32+
boolean userFound = false;
33+
34+
while (rs.next()) {
35+
if (password.equals(rs.getString("password"))) {
3336
session.setAttribute("username", username);
37+
userFound = true;
3438
response.sendRedirect("home.jsp");
35-
} else {
36-
response.sendRedirect("error.jsp");
3739
}
38-
} else {
40+
}
41+
42+
if (!userFound) {
3943
response.sendRedirect("error.jsp");
4044
}
4145
%>

0 commit comments

Comments
 (0)