This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ch05] 5.6, Used the data of ServletContext.
Signed-off-by: Dongho Sim <dhsim86@gmail.com>
- Loading branch information
Showing
7 changed files
with
68 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters