- <%@ page language="java" contentType="text/html; charset=EUC-KR"
- pageEncoding="EUC-KR"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
- <title>Insert title here</title>
- <style type="text/css">
- table, tr, td{
- border: 1px solid #000;
- text-align:center;
- }
- table{
- margin:0 auto;
- }
- </style>
- </head>
- <body>
- <%
- Cookie[] cookies = request.getCookies();
- String id="";
- String pw="";
- if(cookies != null){
- for(Cookie c : cookies){
- if(c.getName().equals("id")){
- id = c.getValue();
- }else if(c.getName().equals("pw")){
- pw = c.getValue();
- }
- }
- }
- %>
- <form method="post" action="/LoginAction">
- <div>
- <table class="table" border="2">
- <tr>
- <td>ID:</td>
- <td>
- <input type="text" name="id" value="<%=id%>">
- </td>
- <td rowspan="2">
- <input type="submit" value="Login" >
- </td>
- </tr>
- <tr>
- <td>PW:</td>
- <td>
- <input type="password" name="pw" value="<%=pw%>">
- </td>
- </tr>
- <tr>
- <td colspan="3">
- <input name="save" type="checkbox">ID/PW 저장
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
2. LoginAction.java
- package com.example;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.Cookie;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- @WebServlet("/LoginAction")
- public class LoginAction extends HttpServlet {
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- /*String log = request.getParameter("login");
- Hw hw = new Hw();
- String str = hw.logPrint(log);*/
- request.setCharacterEncoding("UTF-8");
- response.setContentType("text/html");
- response.setCharacterEncoding("UTF-8");
- //요청시 매개변수 "id"를 가져와 id 넣는다
- //요청시 매개변수 "pw"를 가져와 pw 넣는다
- boolean save = false;
- if(request.getParameter("save") != null){
- save = true;
- }
- //비지니스 레이어 호출(model layer)
- Dao dao = new Dao();
- if(dao.login(id, pw)){
- if(save){
- Cookie idCookie = new Cookie("id", id);
- idCookie.setMaxAge(60*60*24);
- response.addCookie(idCookie);
- Cookie pwCookie = new Cookie("pw", pw);
- pwCookie.setMaxAge(60*60*24);
- response.addCookie(pwCookie);
- }
- //id,pw가 맞다면 메세지로 success 출력
- request.setAttribute("msg", "success");
- }else{
- //id,pw가 아니다면 메세지로 fail 출력
- request.setAttribute("msg", "fail");
- }
- // request + alpha
- /*request.setAttribute("answer", str);
- */
- //"/login_view.jsp"로 요청을 다른 서블릿으로 보내줌
- RequestDispatcher rd = request.getRequestDispatcher("/login_view.jsp");
- rd.forward(request, response);
- }
- }
3. Dao.java
- package com.example;
- public class Dao {
- // db에서 가져온 값
- String dbId = "admin";
- String dbPw = "1234";
- if(id.equals(dbId) && pw.equals(dbPw)){
- //id&pw가 일치할 경우 값을 남긴다
- return true;
- }
- //일치하지 않는 경우 남기지 않는다
- return false;
- }
- }
4. login_view.jsp
- <%@ page language="java" contentType="text/html; charset=EUC-KR"
- pageEncoding="EUC-KR"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
- <title>Insert title here</title>
- </head>
- <body>
- <%=request.getAttribute("msg")%>
- </body>
- </html>
댓글 없음:
댓글 쓰기