Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch05] 5.6, Used the data of ServletContext.
Browse files Browse the repository at this point in the history
Signed-off-by: Dongho Sim <dhsim86@gmail.com>
  • Loading branch information
dhsim86 committed Mar 11, 2017
1 parent 75a5632 commit 7e0cbaf
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 38 deletions.
52 changes: 52 additions & 0 deletions src/main/java/AppInitServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import java.sql.Connection;
import java.sql.DriverManager;

/**
* Created by Dongho on 2017. 3. 11..
*/
public class AppInitServlet extends HttpServlet {

@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("AppInitServlet.init called");
super.init(servletConfig);

try {
ServletContext sc = this.getServletContext();
Class.forName(sc.getInitParameter("driver"));

Connection conn = DriverManager.getConnection(
sc.getInitParameter("url"),
sc.getInitParameter("username"),
sc.getInitParameter("password")
);

sc.setAttribute("conn", conn);
}
catch (Exception e) {
throw new ServletException(e);
}
}

@Override
public void destroy() {
System.out.println("AppInitServlet.destroy called");
super.destroy();

Connection conn =
(Connection)this.getServletContext().getAttribute("conn");

try {
if (conn != null && conn.isClosed() == false) {
conn.close();
}
}
catch (Exception e) {

}
}
}
8 changes: 1 addition & 7 deletions src/main/java/Lesson04/MemberAddServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
try {
ServletContext sc = this.getServletContext();

Class.forName(sc.getInitParameter("driver"));
conn = DriverManager.getConnection(
sc.getInitParameter("url"),
sc.getInitParameter("username"),
sc.getInitParameter("password")
);
conn = (Connection) sc.getAttribute("conn");

stmt = conn.prepareStatement(
"insert into members(email, pwd, mname, cre_date, mod_date)" +
Expand Down Expand Up @@ -81,7 +76,6 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
}
finally {
try { if (stmt != null) stmt.close(); } catch (Exception e) {}
try { if (conn != null) conn.close(); } catch (Exception e) {}
}
}
}
9 changes: 1 addition & 8 deletions src/main/java/Lesson04/MemberDeleteServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)

ServletContext sc = this.getServletContext();

Class.forName(sc.getInitParameter("driver"));

conn = DriverManager.getConnection(
sc.getInitParameter("url"),
sc.getInitParameter("username"),
sc.getInitParameter("password")
);
conn = (Connection) sc.getAttribute("conn");
stmt = conn.prepareStatement(
"delete from members" +
" where mno = ?"
Expand All @@ -49,7 +43,6 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
}
finally {
try { if (stmt != null) stmt.close(); } catch (Exception e) {}
try { if (conn != null) conn.close(); } catch (Exception e) {}
}
}
}
9 changes: 1 addition & 8 deletions src/main/java/Lesson04/MemberListServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)

ServletContext sc = this.getServletContext();

Class.forName(sc.getInitParameter("driver"));

conn = DriverManager.getConnection(
sc.getInitParameter("url"),
sc.getInitParameter("username"),
sc.getInitParameter("password")
);
conn = (Connection) sc.getAttribute("conn");
stmt = conn.createStatement();
rs = stmt.executeQuery(
"select mno, mname, email, cre_date" +
Expand Down Expand Up @@ -71,7 +65,6 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
finally {
try { if (rs != null) rs.close(); } catch (Exception e) {}
try { if (stmt != null) stmt.close(); } catch (Exception e) {}
try { if (conn != null) conn.close(); } catch (Exception e) {}
}
}
}
8 changes: 1 addition & 7 deletions src/main/java/Lesson04/MemberUpdateServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,7 @@ protected void doPost(
try {
ServletContext sc = this.getServletContext();

Class.forName(sc.getInitParameter("driver"));
conn = DriverManager.getConnection(
sc.getInitParameter("url"),
sc.getInitParameter("username"),
sc.getInitParameter("password")
);
conn = (Connection) sc.getAttribute("conn");
stmt = conn.prepareStatement(
"update members set email = ?, mname = ?, mod_date = now()" +
" where mno = ?"
Expand All @@ -106,7 +101,6 @@ protected void doPost(
}
finally {
try { if (stmt != null) stmt.close(); } catch (Exception e) {}
try { if (conn != null) conn.close(); } catch (Exception e) {}
}
}
}
9 changes: 1 addition & 8 deletions src/main/java/Lesson05/MemberListServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)

ServletContext sc = this.getServletContext();

Class.forName(sc.getInitParameter("driver"));

conn = DriverManager.getConnection(
sc.getInitParameter("url"),
sc.getInitParameter("username"),
sc.getInitParameter("password")
);
conn = (Connection) sc.getAttribute("conn");
stmt = conn.createStatement();
rs = stmt.executeQuery(
"select mno, mname, email, cre_date" +
Expand Down Expand Up @@ -78,7 +72,6 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
finally {
try { if (rs != null) rs.close(); } catch (Exception e) {}
try { if (stmt != null) stmt.close(); } catch (Exception e) {}
try { if (conn != null) conn.close(); } catch (Exception e) {}
}
}
}
11 changes: 11 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
</filter-mapping>

<!-- servlets -->
<servlet>
<servlet-name>AppInitServlet</servlet-name>
<servlet-class>AppInitServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>Lesson03.servlets.HelloWorld</servlet-class>
Expand Down Expand Up @@ -74,6 +80,11 @@
</servlet> -->

<!-- servlet mappings -->
<servlet-mapping>
<servlet-name>AppInitServlet</servlet-name>
<url-pattern>/AppInit</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
Expand Down

0 comments on commit 7e0cbaf

Please sign in to comment.