everyday com-eat
작성일
2021. 12. 12. 22:38
작성자
갱수터
728x90

jsp 문법

<%        %> : 자바코드 : scriptlet(스크립트릿)
<%@     %> : 하나의 jsp 설정 : Directive(디렉티브)
<%=      %> : 변수에 담겨있는 값 또는 리턴값을 출력 : 표현식
<%!       %> : 메서드 선언부 -> 향후에는 자바코드로 빠지면 안쓴다
<%--   --%> : 주석 ( Ctrl + Shift + / )

 

 

include 응용 - index 파일 layout 구성하기

<%@ include file="경로" %>

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%@ include file="/module/top.jsp" %>
	<%@ include file="/module/left.jsp" %>		 
	index.jsp 메인 화면 <br/>
	<%@ include file="/module/foot.jsp" %>		 
</body>
</html>

 

 

 

session 응용 - 로그인 후 권한 관리

request.getParameter()와 request.setParameter()는 문자열 밖에 받지 못하기 때문에

List를 받기 위해 session.setAttribute()와 session.getAttribute()를 쓴다.

 

void setAttribute(String name, Object value) {
	//이름이 name인 속성의 값을 value로 지정
}

Object getAttribute(String name) {
	//이름이 name인 속성의 값을 구함
	//이름의 속성이 존재하지 않을 경우 null을 리턴
}

Object 타입을 받기 때문에 형 변환이 필요하다

 if(uid.equals(dbid)) {
	 System.out.println("1-1 아이디 일치");
	 if(upw.equals(dbpw)) {
		 System.out.println("2-1 비밀번호 일치"); 
		 session.setAttribute("S_ID", dbid);
		 session.setAttribute("S_NAME", dbname);
		 session.setAttribute("S_LEVEL", dblevel);
		 response.sendRedirect(request.getContextPath()+"/index.jsp"); //로그인 성공 시 다시 index페이지로 돌아감
	 }else {
		 System.out.println("2-2 비밀번호 불일치");
	 }
 } else {
	 System.out.println("1-2 아이디 불일치");
 }

 

 

session.invalidate(); //세션종료

 

<%@ 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">
<%
	String S_ID = (String)session.getAttribute("S_ID");
	String S_NAME = (String)session.getAttribute("S_NAME");
	String S_LEVEL = (String)session.getAttribute("S_LEVEL");
	
	System.out.println(S_ID + "<-- S_ID  top.jsp");
	System.out.println(S_NAME + "<-- S_NAME  top.jsp");
	System.out.println(S_LEVEL + "<-- S_LEVEL  top.jsp");
%>
   <!-- Begin Wrapper -->
   <div id="wrapper">
         <!-- Begin Header -->
         <div id="header">
/module/top.jsp <br/>       
		       상단 메뉴	<br/>

<%
	if(S_LEVEL == null) {
%>
<!-- 1-1 로그인 전 화면 시작 -->  
<a href="<%= request.getContextPath() %>/user/user_insert_form.jsp">01회원가입</a><br/><br/>	 
<form action="<%= request.getContextPath() %>/login/login_action.jsp" method="post">
	아이디 : <input type="text" name="uid">	
	비번 : <input type="password" name="upw">
		<input type="submit" value="로그인">	
</form>
<!-- 1-1 로그인 전 화면 End -->
<%
	}else{
%>
<!-- 1-2 로그인 후 화면 시작 -->
<%
	if(S_LEVEL == "구매자") {
%>	
<a href="<%= request.getContextPath() %>/user/user_insert_form.jsp">01회원가입</a>
<a href="#">04상품검색</a><br/><br/>
<%
	}else if(S_LEVEL == "판매자") {
%>
<a href="<%= request.getContextPath() %>/user/user_insert_form.jsp">01회원가입</a>
<a href="#">03상품등록</a>
<a href="#">04상품검색</a><br/><br/>
<%
	}else if(S_LEVEL == "관리자") {
%>   
<a href="<%= request.getContextPath() %>/user/user_insert_form.jsp">01회원가입</a>
<a href="<%= request.getContextPath() %>/user/user_list.jsp">02전체회원조회</a>
<a href="#">03상품등록</a>
<a href="#">04상품검색</a>
<a href="<%= request.getContextPath() %>/superadmin/superadmin_index.jsp">05최고관리자메뉴</a><br/><br/>
<%
	}else if(S_LEVEL == "최고관리자") {
%> 
<a href="<%= request.getContextPath() %>/user/user_insert_form.jsp">01회원가입</a>
<a href="<%= request.getContextPath() %>/user/user_list.jsp">02전체회원조회</a>
<a href="#">03상품등록</a>
<a href="#">04상품검색</a>
<a href="<%= request.getContextPath() %>/superadmin/superadmin_index.jsp?S_LEVEL=<%=S_LEVEL %>">05최고관리자메뉴</a><br/><br/>
<%
	}
%>
	<%=S_ID %> 아이디/ <%=S_LEVEL %> 권한 로그인 중
	<a href="<%= request.getContextPath() %>/login/logout.jsp?S_LEVEL=null" method="post">로그아웃</a>
<!-- 1-2 로그인 후 화면 End  -->
<%
	}
%>
		 </div>
		 <!-- End Header -->
<%@ 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");		//post 방식 한글처리
	String S_LEVEL2 = (String)session.getAttribute("S_LEVEL");
	
	if(S_LEVEL2.equals("최고관리자")){
		System.out.print("<--S_LEVEL2   superadmin_index.jsp");
		%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>

<!-- 1. main.css 절대 경로 연결  하세요! -->
<link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/css/main2.css" /> 

</head>

<body>
<%@ include file="/module/top.jsp" %>
<%@ include file="/module/left.jsp" %>		 

		       superadmin_index.jsp 메인 화면 <br/>

<%@ include file="/module/hadan.jsp" %>		 
</body>
</html>
		
		<%
	}else{
		System.out.print("<--S_LEVEL   superadmin_index.jsp");
		%>
<script type="text/javascript">
	alert('접근 할 수 없습니다.');
	location.href='<%=request.getContextPath() %>/index.jsp';
</script>
<%
	}
%>

 

<script> 코드는 <%%> 안에 쓸 수 없다

만약 if문 마다 쓰고 싶으면 String으로 alert 변수를 주고 alert값을 if문 중괄호에 대입하면 된다.

728x90

'{ "Hello World!" }; > JSP/Servlet' 카테고리의 다른 글

(JDBC) 프로그램 순서 7단계  (0) 2021.12.17
(JSP) jsp와 java 연결하기  (0) 2021.12.13
(JSP) GET / POST  (0) 2021.12.12