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 HttpSession.
Signed-off-by: Dongho Sim <dhsim86@gmail.com>
- Loading branch information
Showing
5 changed files
with
160 additions
and
0 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,82 @@ | ||
package Lesson05; | ||
|
||
import javax.servlet.RequestDispatcher; | ||
import javax.servlet.ServletContext; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpSession; | ||
import java.io.IOException; | ||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
|
||
/** | ||
* Created by Dongho on 2017. 3. 11.. | ||
*/ | ||
@WebServlet("/auth/login") | ||
public class LoginServlet extends HttpServlet { | ||
|
||
@Override | ||
protected void doGet( | ||
HttpServletRequest request, HttpServletResponse response | ||
) throws ServletException, IOException { | ||
RequestDispatcher rd = request.getRequestDispatcher( | ||
"/Lesson05/LogInForm.jsp" | ||
); | ||
rd.forward(request, response); | ||
} | ||
|
||
@Override | ||
protected void doPost( | ||
HttpServletRequest request, HttpServletResponse response | ||
) throws ServletException, IOException { | ||
|
||
Connection conn = null; | ||
PreparedStatement stmt = null; | ||
ResultSet rs = null; | ||
|
||
try { | ||
|
||
ServletContext sc = this.getServletContext(); | ||
conn = (Connection)sc.getAttribute("conn"); | ||
|
||
stmt = conn.prepareStatement( | ||
"select mname, email from members" + | ||
" where email = ? and pwd = ?" | ||
); | ||
|
||
stmt.setString(1, request.getParameter("email")); | ||
stmt.setString(2, request.getParameter("password")); | ||
|
||
rs = stmt.executeQuery(); | ||
|
||
if (rs.next()) { | ||
Member member = new Member() | ||
.setEmail(rs.getString("email")) | ||
.setName(rs.getString("mname")); | ||
|
||
HttpSession session = request.getSession(); | ||
session.setAttribute("member", member); | ||
|
||
response.sendRedirect("../member/list"); | ||
} | ||
else { | ||
RequestDispatcher rd = request.getRequestDispatcher( | ||
"/Lesson05/LogInFail.jsp" | ||
); | ||
rd.forward(request, response); | ||
} | ||
} | ||
catch (Exception e) { | ||
|
||
throw new ServletException(e); | ||
} | ||
finally { | ||
try { if (rs != null) rs.close(); } catch (Exception e) {} | ||
try { if (stmt != null) stmt.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package Lesson05; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpSession; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Created by Dongho on 2017. 3. 11.. | ||
*/ | ||
@WebServlet("/auth/logout") | ||
public class LogoutServlet extends HttpServlet { | ||
|
||
@Override | ||
protected void doGet( | ||
HttpServletRequest request, HttpServletResponse response | ||
) throws ServletException, IOException { | ||
|
||
HttpSession session = request.getSession(); | ||
session.invalidate(); | ||
|
||
response.sendRedirect("login"); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<%-- | ||
Created by IntelliJ IDEA. | ||
User: Dongho | ||
Date: 2017. 3. 11. | ||
Time: PM 4:59 | ||
To change this template use File | Settings | File Templates. | ||
--%> | ||
<%@ page contentType="text/html;charset=UTF-8" language="java" %> | ||
<!DOCTYPE html> | ||
<html> | ||
<meta http-equiv="refresh" content="1;url=login"> | ||
<head> | ||
<title>Login Failure</title> | ||
</head> | ||
<body> | ||
<p> Login Failure, Email or password is incorrect. Retry after a while.</p> | ||
</body> | ||
</html> |
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,23 @@ | ||
<%-- | ||
Created by IntelliJ IDEA. | ||
User: Dongho | ||
Date: 2017. 3. 11. | ||
Time: PM 4:56 | ||
To change this template use File | Settings | File Templates. | ||
--%> | ||
<%@ page contentType="text/html;charset=UTF-8" language="java" %> | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Login</title> | ||
</head> | ||
<body> | ||
<h2>User Login</h2> | ||
<form action="login" method="post"> | ||
email: <input type="text" name="email"><br> | ||
password: <input type="password" name="password"><br> | ||
|
||
<input type="submit" value="Login"> | ||
</form> | ||
</body> | ||
</html> |