JSP

JSP - 장바구니(물건추가, 로그아웃)Session

yes or yes 2017. 8. 14. 14:54
반응형

login.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>


<form name ="form1" action="setProduct.jsp" method="post"> 

<h1>로그인</h1> 

<input type="text" name="name" > 
<input type="submit" name="login" value = "로그인"> 


</body> 
</html>

///////////////////////////////////////////////

setProduct.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"> 
<% 
request.setCharacterEncoding("euc-kr"); 
%> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"> 
<title>Insert title here</title> 
</head> 
<body> 

<% String name = request.getParameter("name");  
//앞에서 입력한 name값을 받아서 뿌려준다. 
%> 

<%if(name == null){ 
out.println("<script>alert('로그인 하세요'); history.go(-1); </script>"); 
}  
//name이  비었을 때 다음페이지로 넘어갈려고할때의 예외처리 
%> 

   <%= name%>님이 로그인 한 상태 입니다.<br><br><br> 

<form name="form2" action="add.jsp" method="post"> 
   <select name="fruit3"> 
   <option value="사과">사과</option> 
   <option value="배">배</option> 
   <option value="오렌지">오렌지</option> 
   <option value="키위">키위</option> 
    </select>  
    
    
   <input type="submit" name="add" value="추가버튼"> 
   </form> 
   <br><br> 
   <a href="checkOut.jsp">계산<>  

</body> 
</html>

/////////////////////////////////////////////

add.jsp

<%@page import="java.util.ArrayList"%> 
<%@ 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>add.jsp</title> 
</head> 
<body> 
 <% 
  request.setCharacterEncoding("EUC-KR"); 
  
  String productname = request.getParameter("fruit3"); 
   
   
   
  ArrayList list = (ArrayList)session.getAttribute("productlist"); 
  if(list == null){ 
   list = new ArrayList(); 
   session.setAttribute("productlist", list); 
  } 
   
  list.add(productname); 
 %> 
  
 <%if(productname == null){ 
out.println("<script>alert('과일을 고르세요'); history.go(-1); </script>"); 
} %> 
  
 <script> 
 alert("<%=productname %>이(가) 추가되었습니다!!"); 
 history.go(-1); 
  
 </script> 
</body> 
  
</html>

//////////////////////////////////

checkOut.jsp

<%@page import="java.util.ArrayList"%> 
<%@ 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>checkOut.jsp</title> 
</head> 
<body> 
 <div align="center"> 
  <h2>계산</h2> 
  선택한 상품 목록 
  <hr> 
   
  <% 
   ArrayList list = (ArrayList)session.getAttribute("productlist"); 
   if(list == null){ 
    out.println("선택한 상품이 없습니다.!!"); 
   } else { 
    for(Object productname:list){ 
     out.println(productname+"<br>"); 
    } 
   } 
   session.invalidate(); 
  %> 
  
 </div> 
  
 <form action="logout.jsp"> 
 <input type="submit" value="로그아웃"/> 
 </form> 
</body> 
  
</html>

/////////////////////////////////////////////

로그아웃시 session을 죽여주는 클래스
logout.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"> 

<%session.invalidate(); 
response.sendRedirect("login.jsp"); 
%> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"> 
<title>Insert title here</title> 
</head> 
<body> 

</body> 
</html>


반응형