2014년 4월 14일 월요일

[Java]로그인 화면(model2)

1. login_form.jsp
  1. <%@ page language="java" contentType="text/html; charset=EUC-KR"
  2.     pageEncoding="EUC-KR"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
  7. <title>Insert title here</title>
  8. <style type="text/css">
  9.     table, tr, td{
  10.         border: 1px solid #000;
  11.         text-align:center;
  12.     }
  13.     table{
  14.         margin:0 auto;
  15.     }
  16.    
  17. </style>
  18. </head>
  19. <body>
  20. <%
  21.     Cookie[] cookies = request.getCookies();
  22.     String id="";
  23.     String pw="";
  24.     if(cookies != null){
  25.         for(Cookie c : cookies){
  26.             if(c.getName().equals("id")){
  27.                 id = c.getValue();
  28.             }else if(c.getName().equals("pw")){
  29.                 pw = c.getValue();
  30.             }
  31.         }
  32.     }
  33. %>
  34.     <form method="post" action="/LoginAction">
  35.     <div>
  36.         <table class="table" border="2">
  37.             <tr>
  38.                 <td>ID:</td>
  39.                 <td>
  40.                     <input type="text" name="id" value="<%=id%>">
  41.                 </td>
  42.                 <td rowspan="2">
  43.                     <input type="submit" value="Login" >
  44.                 </td>
  45.             </tr>
  46.             <tr>
  47.                 <td>PW:</td>
  48.                 <td>
  49.                     <input type="password" name="pw" value="<%=pw%>">
  50.                 </td>
  51.             </tr>
  52.             <tr>
  53.                 <td colspan="3">
  54.                     <input name="save" type="checkbox">ID/PW 저장
  55.                 </td>
  56.             </tr>
  57.         </table>
  58.     </div> 
  59.     </form>
  60. </body>
  61. </html>
2. LoginAction.java
  1. package com.example;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6. import javax.servlet.RequestDispatcher;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.annotation.WebServlet;
  9. import javax.servlet.http.Cookie;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13.  
  14.  
  15. @WebServlet("/LoginAction")
  16. public class LoginAction extends HttpServlet {
  17.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  18.         /*String log = request.getParameter("login");
  19.         Hw hw = new Hw();
  20.         String str = hw.logPrint(log);*/
  21.         request.setCharacterEncoding("UTF-8");
  22.         response.setContentType("text/html");
  23.         response.setCharacterEncoding("UTF-8");
  24.        
  25.         PrintWriter out = response.getWriter();
  26.         //요청시 매개변수 "id"를 가져와 id 넣는다
  27.         String id = request.getParameter("id");
  28.         //요청시 매개변수 "pw"를 가져와 pw 넣는다
  29.         String pw = request.getParameter("pw");
  30.        
  31.         boolean save = false;
  32.         if(request.getParameter("save") != null){
  33.             save = true;
  34.         }
  35.        
  36.         //비지니스 레이어 호출(model layer)
  37.         Dao dao = new Dao();
  38.         if(dao.login(id, pw)){
  39.             if(save){
  40.                 Cookie idCookie = new Cookie("id", id);
  41.                 idCookie.setMaxAge(60*60*24);
  42.                 response.addCookie(idCookie);
  43.                
  44.                 Cookie pwCookie = new Cookie("pw", pw);
  45.                 pwCookie.setMaxAge(60*60*24);
  46.                 response.addCookie(pwCookie);
  47.             }
  48.             //id,pw가 맞다면 메세지로 success 출력
  49.             request.setAttribute("msg""success");
  50.         }else{
  51.             //id,pw가 아니다면 메세지로 fail 출력
  52.             request.setAttribute("msg""fail");
  53.         }
  54.         // request + alpha
  55.         /*request.setAttribute("answer", str);
  56.         */
  57.         //"/login_view.jsp"로 요청을 다른 서블릿으로 보내줌
  58.         RequestDispatcher rd = request.getRequestDispatcher("/login_view.jsp");
  59.         rd.forward(request, response);
  60.     }
  61. }
3. Dao.java
  1. package com.example;
  2.  
  3. public class Dao {
  4.     public boolean login(String id, String pw){
  5.         // db에서 가져온 값
  6.         String dbId = "admin";
  7.         String dbPw = "1234";
  8.         if(id.equals(dbId) && pw.equals(dbPw)){
  9.             //id&pw가 일치할 경우 값을 남긴다
  10.             return true;
  11.         }
  12.         //일치하지 않는 경우 남기지 않는다
  13.         return false;
  14.     }
  15.    
  16. }
4. login_view.jsp
  1. <%@ page language="java" contentType="text/html; charset=EUC-KR"
  2.     pageEncoding="EUC-KR"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10.     <%=request.getAttribute("msg")%>
  11. </body>
  12. </html>

댓글 없음:

댓글 쓰기