<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>session</title>
</head>
<body>
<form method="post" action="7_session_ok.jsp">
<p>아이디: <input type="text" name="userid"></p>
<p>비밀번호: <input type="password" name="userpw"></p>
<p><button type="submit">로그인</button></p>
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>session</title>
</head>
<body>
<h2>로그인 실패!</h2>
<p>아이디 또는 비밀번호를 확인해주세요!</p>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
session.invalidate();
response.sendRedirect("7_session.jsp");
%>
<html>
<head>
<title>Title</title>
</head>
<body>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String userid = request.getParameter("userid");
String userpw = request.getParameter("userpw");
if(userid.equals("admin") && userpw.equals("1234")){
session.setAttribute("userid", userid);
response.sendRedirect("7_session_success.jsp");
}else{
response.sendRedirect("7_session_fail.jsp");
}
%>
<html>
<head>
<title>session</title>
</head>
<body>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
if(session.getAttribute("userid") == null){
response.sendRedirect("7_session.jsp");
}
String userid = (String)session.getAttribute("userid");
%>
<html>
<head>
<title>session</title>
</head>
<body>
<h2>환영합니다.</h2>
<p><%=userid%>님 로그인되었습니다.</p>
<p>[<a href="7_session_logout.jsp">로그아웃</a>]</p>
</body>
</html>
<%@ page import="java.util.Arrays" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
int total = 0;
if(application.getAttribute("total_count") != null){
total = (int)application.getAttribute("total_count");
}
application.setAttribute("menu", Arrays.asList("홈", "등록", "구매", "마이페이지"));
application.setAttribute("total_count", ++total);
%>
<html>
<head>
<title>application</title>
</head>
<body>
<p><a href="8_application_home.jsp">다음 페이지로</a></p>
<p>누적된 총계: <%=total%></p>
</body>
</html>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
List<String> list = (List)application.getAttribute("menu");
int total = (int)application.getAttribute("total_count");
%>
<html>
<head>
<title>application</title>
</head>
<body>
<ul>
<% for(String menu:list){ %>
<li><%=menu%></li>
<%}%>
</ul>
<p><%=total%></p>
</body>
</html>
try{} catch() {}<%@ page errorPage="에러가 발생했을 때 보여줄 페이지"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page errorPage="9_errorpage_error.jsp" %>
<html>
<head>
<title>error page</title>
</head>
<body>
<h2>errorpage</h2>
<p>의도치 않게 에러가 발생했습니다.</p>
<%
String abc = (String)session.getAttribute("ABC");
abc.charAt(3); // 에러 발생!
%>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isErrorPage="true"%> <!-- 에러 페이지로 사용함 -->
<html>
<head>
<title>error</title>
</head>
<body>
<h2>에러 발생!!</h2>
<p>에러가 발생했습니다. 이동해주세요.</p>
<p><a href="./7_session.jsp">이동</a></p>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>forward</title>
</head>
<body>
<form method="post" action="10_forward_ok.jsp">
<p>아이디: <input type="text" name="userid"></p>
<p><button type="submit">확인</button></p>
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:forward page="10_forward_result.jsp"/>
<html>
<head>
<title>Title</title>
</head>
<body>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String userid = request.getParameter("userid");
%>
<html>
<head>
<title>forward</title>
</head>
<body>
<p>전달받은 userid: <%=userid%></p>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>redirect</title>
</head>
<body>
<form method="post" action="10_redirect_ok.jsp">
<p>아이디: <input type="text" name="userid"></p>
<p><button type="submit">확인</button></p>
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String userid = request.getParameter("userid");
// request.setAttribute("userid", userid); // 적용 안됨!
// response.sendRedirect("10_redirect_result.jsp");
%>
<html>
<head>
<title>Title</title>
<script>
window.onload=function (){
document.getElementById("hiddenValue").value = "<%=userid%>"
document.getElementById("redirectForm").submit();
}
//location.href = '10_redirect_result.jsp?userid=' + "userid" // get
</script>
</head>
<body>
<form id="redirectForm" method="post" action="10_redirect_result.jsp">
<input type="hidden" id="hiddenValue" name="userid" value="<%=userid%>">
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String userid = request.getParameter("userid");
%>
<html>
<head>
<title>redirect</title>
</head>
<body>
<p>전달받은 userid: <%=userid%></p>
</body>
</html>
<!-- Servlet API -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<dependencies>이 사이에 추가했다.</dependencies>
그리고 동기화 한다. 오른쪽 상단 조그만 아이콘 클릭!
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>dispatcher</title>
</head>
<body>
<p><a href="11_dispatcher_ok.jsp?userid=apple">이동</a></p>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String userid = request.getParameter("userid");
request.setAttribute("userid", userid);
request.setAttribute("name", "김사과");
request.setAttribute("age", 20);
request.setAttribute("gender", "여자");
RequestDispatcher rd = request.getRequestDispatcher("11_dispatcher_result.jsp");
rd.forward(request, response);
%>
<html>
<head>
<title>dispatcher</title>
</head>
<body>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String userid = (String)request.getAttribute("userid");
String name = (String)request.getAttribute("name");
String gender = (String)request.getAttribute("gender");
int age = (Integer) request.getAttribute("age");
%>
<html>
<head>
<title>dispatcher</title>
</head>
<body>
<h2>포워드로 넘어온 값</h2>
<p>userid: <%=userid%></p>
<p>name: <%=name%></p>
<p>gender: <%=gender%></p>
<p>age: <%=age%></p>
</body>
</html>