2016. 10. 26. 17:07ㆍProgramming/JSP
**배운 것**
1) 다른 jsp 페이지로 넘어가는 법
response.sendRedirect("./L01LoginForm.jsp");
2) 파라미터 넘기기
response.sendRedirect("./L01LoginForm.jsp?msg=login failed");
3) dispatcher 통한 페이지 넘기기(forward)
<index.jsp:아무것도 안 하고 바로 L01LoginForm.jsp으로 넘겨줌>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> <!-- index.jsp 호출시 redirect로 스쳐지나가게 할 예정 --> <% System.out.println("index.jsp 페이지를 지나쳤다."); response.sendRedirect("./L01LoginForm.jsp"); %> </body> </html> | cs |
//페이지 넘기는 다른 방식(RequestDispatcher)
RequestDispatcher dispatcher = request.getRequestDispatcher("./L01LoginForm.jsp");
dispatcher.forward(request, response);
//Dispatcher의 좋은 점
//1. parameter를 post 타입으로 전달하기 위해 -> Object 타입으로 전달 가능
//-> header 정보로 파라미터 넘길 수 있음
//2. request 객체와 response 객체가 유지됨.
//-> dispatcher.forward에 그대로 담아 넘기기 때문(요청 위임)
//페이지 넘기는 또 다른 방식(jsp:forward)
<L01LoginForm.jsp: 로그인 form 화면, 제출 버튼 통해 /loginCtrl 실행>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>servlet을 이용해 로그인 성공하자</title> </head> <body> <h1>로그인 form</h1> <!-- 제출한 파라미터와 DB에서 가져온 id/pw를 L01LoginController.java에서 비교 후, 같으면 true 파라미터를 L03LoginResult.jsp로 보낸다 => 성공 메시지 다르면 L01LoginForm 페이지로 와서 "login failed" 메시지 출력 --> <%String login = request.getParameter("msg"); if(login!=null){ %> <h3><%=login%></h3> <% } %> <form action="./loginCtrl" method="post"> <p> <label>아이디:</label> <input type="text" name="id" value="jsp"> </p> <p> <label>비밀번호:</label> <input type="text" name="pw" value="admin1234"> </p> <button type="submit">제출</button> </form> </body> </html> | cs |
<L02LoginController 서블릿: db와 입력 정보(=파라미터)를 비교해 로그인 판정을 result 페이지에 보내줌>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/loginCtrl") public class L02LoginController extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String db_id = "jsp"; String db_pw = "admin1234"; request.setCharacterEncoding("utf-8"); String id = request.getParameter("id"); String pw = request.getParameter("pw"); System.out.println("id:" + id + " //pw:" + pw); if (id.equals(db_id) && pw.equals(db_pw)) { response.sendRedirect("./L03LoginResult.jsp?login=1"); //login=1 -> true, 0 -> false } else { response.sendRedirect("./L01LoginForm.jsp?msg=login failed"); } } } | cs |
<L03LoginResult.jsp: 로그인 판정 결과 표시>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>login 성공 페이지</title> </head> <body> <!-- parameter login이 오지 않으면 이 페이지에 접근 불가능 이 때 login은 세션 or 쿠키 대신. 세션 or 쿠키는 브라우저가 유지하고 있는 파라미터. --> <% String login = request.getParameter("login"); boolean access = false; int int_login =0; if(login != null){ int_login = Integer.parseInt(login); //login은 아직 int로 선언 안 됐으므로 if(int_login == 1){ access = true; } } if(!access){ response.sendRedirect("./L01LoginForm.jsp?msg=access failed"); } %> <h1>로그인 성공 페이지</h1> </body> </html> | cs |
'Programming > JSP' 카테고리의 다른 글
L06 세션으로 값 전달(1/2) (0) | 2016.10.27 |
---|---|
L05 Template (0) | 2016.10.26 |
L03 서블릿 메소드(2/2) (0) | 2016.10.25 |
L03 서블릿 메소드(1/2) (0) | 2016.10.25 |
L02 JSP 작성 (0) | 2016.10.25 |