이미지도 넣어보자
LoginProc.java
package net.control;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginProc extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//method=post 방식이면 service() 함수가 doPost() 함수를 호출함
try {
req.setCharacterEncoding("utf-8");
String uid = req.getParameter("uid").trim();
String upw = req.getParameter("upw").trim();
if(uid.equals("itwill") && upw.equals("1234")) {
//로그인 성공했을 때
req.setAttribute("r_uid", uid);
req.setAttribute("r_upw", upw);
req.setAttribute("msg", "로그인 성공~~");
req.setAttribute("img", "<img src='control/smile.png'>");
} else {
//로그인 실패했을 때
req.setAttribute("r_uid", "guest");
req.setAttribute("r_upw", "guest");
req.setAttribute("msg", "로그인 실패!!");
req.setAttribute("img", "<img src='control/crying.png'>");
}//if end
//뷰페이지 이동
String view = "control/loginResult.jsp";
RequestDispatcher rd = req.getRequestDispatcher(view);
rd.forward(req, resp);
} catch (Exception e) {
System.out.println("요청실패 : " + e);
}//end
}//doPost() end
}//class end
loginResult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>loginResult.jsp</title>
</head>
<body>
<h3>* 회원 로그인 결과 *</h3>
1)JSP<br>
<%=request.getAttribute("msg") %> <br>
<%=request.getAttribute("img") %> <br>
아이디 : <%=request.getAttribute("r_uid")%><br>
비번 : <%=request.getAttribute("r_upw")%><br>
<hr>
2)EL(표현언어)<br>
${requestScope.msg} <br>
${requestScope.img} <br>
아이디 : ${requestScope.r_uid}<br>
비번 : ${requestScope.r_upw}<br>
<!-- Scope는 생략 가능하다 -->
${msg} <br>
${img} <br>
아이디 : ${r_uid}<br>
비번 : ${r_upw}<br>
</body>
</html>


MVC에서는 주로 아래와 같이 코딩을 한다.

LoginProc.java
package net.control;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginProc extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//method=post 방식이면 service() 함수가 doPost() 함수를 호출함
try {
req.setCharacterEncoding("utf-8");
String uid = req.getParameter("uid").trim();
String upw = req.getParameter("upw").trim();
HttpSession session = req.getSession(); //요청한 사용자의 session 객체 선언
ServletContext application = req.getServletContext(); //요청한 사용자의 application 객체 선언
if(uid.equals("itwill") && upw.equals("1234")) {
//로그인 성공했을 때
req.setAttribute("r_uid", uid);
req.setAttribute("r_upw", upw);
req.setAttribute("msg", "로그인 성공~~");
req.setAttribute("img", "<img src='control/smile.png'>");
session.setAttribute("s_uid", uid);
session.setAttribute("s_upw", upw);
application.setAttribute("a_uid", uid);
application.setAttribute("a_upw", upw);
} else {
//로그인 실패했을 때
req.setAttribute("r_uid", "guest");
req.setAttribute("r_upw", "guest");
req.setAttribute("msg", "로그인 실패!!");
req.setAttribute("img", "<img src='control/crying.png'>");
session.setAttribute("s_uid", uid);
session.setAttribute("s_upw", upw);
application.setAttribute("a_uid", uid);
application.setAttribute("a_upw", upw);
}//if end
//뷰페이지 이동
String view = "control/loginResult.jsp";
RequestDispatcher rd = req.getRequestDispatcher(view);
rd.forward(req, resp);
} catch (Exception e) {
System.out.println("요청실패 : " + e);
}//end
}//doPost() end
}//class end
jsp에서는 session이나 application이 내부 객체로 존재했지만, mvc에서는 직접 객체를 생성하고 사용해야 한다.
위 코드는 .jsp가 아닌, .java에서 작성한 코드이다.
loginResult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>loginResult.jsp</title>
</head>
<body>
<h3>* 회원 로그인 결과 *</h3>
1)JSP<br>
<%=request.getAttribute("msg") %> <br>
<%=request.getAttribute("img") %> <br>
아이디 : <%=request.getAttribute("r_uid")%><br>
비번 : <%=request.getAttribute("r_upw")%><br>
아이디 : <%=session.getAttribute("s_uid")%><br>
비번 : <%=session.getAttribute("s_upw")%><br>
아이디 : <%=application.getAttribute("a_uid")%><br>
비번 : <%=application.getAttribute("a_upw")%><br>
<hr>
2)EL(표현언어)<br>
${requestScope.msg} <br>
${requestScope.img} <br>
아이디 : ${requestScope.r_uid}<br>
비번 : ${requestScope.r_upw}<br>
아이디 : ${sessionScope.s_uid}<br>
비번 : ${sessionScope.s_upw}<br>
아이디 : ${applicationScope.a_uid}<br>
비번 : ${applicationScope.a_upw}<br>
<!-- Scope는 생략 가능하다 -->
<!-- 웹의 ServletContext 순환 순서 : pageContext -> request -> session -> application -->
${msg} <br>
${img} <br>
아이디 : ${r_uid}<br> 비번 : ${r_upw}<br>
아이디 : ${s_uid}<br> 비번 : ${s_upw}<br>
아이디 : ${a_uid}<br> 비번 : ${a_upw}<br>
</body>
</html>


ServletContext는 pageContext -> request -> session -> application의 네 공간으로 나눠져 있다.
'웹개발 교육 > jsp' 카테고리의 다른 글
[64일] jsp (46) - MVC 패턴 (페이지 이동) (0) | 2022.10.27 |
---|---|
[64일] jsp (45) - HttpServlet (0) | 2022.10.27 |
[63일] jsp (44) - JSTL (0) | 2022.10.26 |
[63일] jsp (43) - EL (0) | 2022.10.26 |
[63일] jsp (42) - error (0) | 2022.10.26 |
이미지도 넣어보자
LoginProc.java
package net.control;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginProc extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//method=post 방식이면 service() 함수가 doPost() 함수를 호출함
try {
req.setCharacterEncoding("utf-8");
String uid = req.getParameter("uid").trim();
String upw = req.getParameter("upw").trim();
if(uid.equals("itwill") && upw.equals("1234")) {
//로그인 성공했을 때
req.setAttribute("r_uid", uid);
req.setAttribute("r_upw", upw);
req.setAttribute("msg", "로그인 성공~~");
req.setAttribute("img", "<img src='control/smile.png'>");
} else {
//로그인 실패했을 때
req.setAttribute("r_uid", "guest");
req.setAttribute("r_upw", "guest");
req.setAttribute("msg", "로그인 실패!!");
req.setAttribute("img", "<img src='control/crying.png'>");
}//if end
//뷰페이지 이동
String view = "control/loginResult.jsp";
RequestDispatcher rd = req.getRequestDispatcher(view);
rd.forward(req, resp);
} catch (Exception e) {
System.out.println("요청실패 : " + e);
}//end
}//doPost() end
}//class end
loginResult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>loginResult.jsp</title>
</head>
<body>
<h3>* 회원 로그인 결과 *</h3>
1)JSP<br>
<%=request.getAttribute("msg") %> <br>
<%=request.getAttribute("img") %> <br>
아이디 : <%=request.getAttribute("r_uid")%><br>
비번 : <%=request.getAttribute("r_upw")%><br>
<hr>
2)EL(표현언어)<br>
${requestScope.msg} <br>
${requestScope.img} <br>
아이디 : ${requestScope.r_uid}<br>
비번 : ${requestScope.r_upw}<br>
<!-- Scope는 생략 가능하다 -->
${msg} <br>
${img} <br>
아이디 : ${r_uid}<br>
비번 : ${r_upw}<br>
</body>
</html>


MVC에서는 주로 아래와 같이 코딩을 한다.

LoginProc.java
package net.control;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginProc extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//method=post 방식이면 service() 함수가 doPost() 함수를 호출함
try {
req.setCharacterEncoding("utf-8");
String uid = req.getParameter("uid").trim();
String upw = req.getParameter("upw").trim();
HttpSession session = req.getSession(); //요청한 사용자의 session 객체 선언
ServletContext application = req.getServletContext(); //요청한 사용자의 application 객체 선언
if(uid.equals("itwill") && upw.equals("1234")) {
//로그인 성공했을 때
req.setAttribute("r_uid", uid);
req.setAttribute("r_upw", upw);
req.setAttribute("msg", "로그인 성공~~");
req.setAttribute("img", "<img src='control/smile.png'>");
session.setAttribute("s_uid", uid);
session.setAttribute("s_upw", upw);
application.setAttribute("a_uid", uid);
application.setAttribute("a_upw", upw);
} else {
//로그인 실패했을 때
req.setAttribute("r_uid", "guest");
req.setAttribute("r_upw", "guest");
req.setAttribute("msg", "로그인 실패!!");
req.setAttribute("img", "<img src='control/crying.png'>");
session.setAttribute("s_uid", uid);
session.setAttribute("s_upw", upw);
application.setAttribute("a_uid", uid);
application.setAttribute("a_upw", upw);
}//if end
//뷰페이지 이동
String view = "control/loginResult.jsp";
RequestDispatcher rd = req.getRequestDispatcher(view);
rd.forward(req, resp);
} catch (Exception e) {
System.out.println("요청실패 : " + e);
}//end
}//doPost() end
}//class end
jsp에서는 session이나 application이 내부 객체로 존재했지만, mvc에서는 직접 객체를 생성하고 사용해야 한다.
위 코드는 .jsp가 아닌, .java에서 작성한 코드이다.
loginResult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>loginResult.jsp</title>
</head>
<body>
<h3>* 회원 로그인 결과 *</h3>
1)JSP<br>
<%=request.getAttribute("msg") %> <br>
<%=request.getAttribute("img") %> <br>
아이디 : <%=request.getAttribute("r_uid")%><br>
비번 : <%=request.getAttribute("r_upw")%><br>
아이디 : <%=session.getAttribute("s_uid")%><br>
비번 : <%=session.getAttribute("s_upw")%><br>
아이디 : <%=application.getAttribute("a_uid")%><br>
비번 : <%=application.getAttribute("a_upw")%><br>
<hr>
2)EL(표현언어)<br>
${requestScope.msg} <br>
${requestScope.img} <br>
아이디 : ${requestScope.r_uid}<br>
비번 : ${requestScope.r_upw}<br>
아이디 : ${sessionScope.s_uid}<br>
비번 : ${sessionScope.s_upw}<br>
아이디 : ${applicationScope.a_uid}<br>
비번 : ${applicationScope.a_upw}<br>
<!-- Scope는 생략 가능하다 -->
<!-- 웹의 ServletContext 순환 순서 : pageContext -> request -> session -> application -->
${msg} <br>
${img} <br>
아이디 : ${r_uid}<br> 비번 : ${r_upw}<br>
아이디 : ${s_uid}<br> 비번 : ${s_upw}<br>
아이디 : ${a_uid}<br> 비번 : ${a_upw}<br>
</body>
</html>


ServletContext는 pageContext -> request -> session -> application의 네 공간으로 나눠져 있다.
'웹개발 교육 > jsp' 카테고리의 다른 글
[64일] jsp (46) - MVC 패턴 (페이지 이동) (0) | 2022.10.27 |
---|---|
[64일] jsp (45) - HttpServlet (0) | 2022.10.27 |
[63일] jsp (44) - JSTL (0) | 2022.10.26 |
[63일] jsp (43) - EL (0) | 2022.10.26 |
[63일] jsp (42) - error (0) | 2022.10.26 |