본문 바로가기
CLASS/JSP

#1-1 / 🌺 JSP 기초

by eungSe__ 2024. 6. 24.

JavaServer Pages

 

jsp는 보안상 뚫리기가 쉽기 때문에

do를 실행시켜서 jsp로 data를 출력시키는 경우가 많다!

 

view

1. webapp에서 jsp를 로드하는 방식 ( 외부에서 직접 해당 jsp를 실행 가능 )

2. META-INF에서 jsp를 로드하는 방식 (외부에서 직접 해당 jsp를 실행 불가)

     META-INF 에 파일을 넣을시 외부에서 절대 접근 금지  (보안 아주 good 👍) - 정통

     ∴ 중요한 jsp파일은 META-INF에 넣는다!! -> do로 실행 

response.setContentType("text/html;charset=utf-8");

//view(출력 역할)
RequestDispatcher rd = request.getRequestDispatcher("./META-INF/jsp2_1.jsp");
rd.forward(request, response);

절대 안들어가짐~ (requestdispatcher 로 해당 경로 지정한) .do 파일에서 확인 가능

 


 

⚡ html -> jsp로 .do 없이 class및 method를 이용한 결과값 출력(가능은 하나 정통방식 x )

단점 :

error시 모~든 뻑난 코드가 노출됨

Class 생성 불가(return만 가능,타 jsp파일에서 class 불러오기도 불가능)

method 별도로 생성 불가

 ( jsp의 void는 계산 수식 및 배열 값을 전역변수로 전달하는 method역할만 함 )

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	//단점 : error시 모~든 뻑난 코드가 노출됨 , Class 생성 불가 , method 별도로 생성 불가
	request.setCharacterEncoding("utf-8");
	String mname = request.getParameter("mname");
	out.print(mname);

	//static 선언 불가
	int sum = 0;
	//외부 class를 생성
	class abc{
		int sum2 = 0;
		int c = 0;
		int d = 0;
		//class 안에서 사용하는 변수는 전역변수이다. 
		  //단, 외부에서는 사용하지 못하고 class밖에있는 변수도 사용 불가
		public void bbb(){ //jsp의 void는 계산 수식 및 배열 값을 전역변수로 전달하는 method역할만 함
			int a = 10;
			int b = 20;
			this.c = 100;
			this.d = 200;
			//sum = a+b; 불가
		}
		public int ccc(){
			this.sum2 = this.c + this.d;
			return this.sum2;
		}
	}
	abc zz = new abc(); //class 호출
	zz.bbb(); //해당 메소드로 전역변수에 값을 이관
	out.print(zz.ccc()); //300출력
%>

~~ !doctype~ 생략

 


 

⚡ 조건문을 이용한 script 출력

- script 안에 jsp문법을 이용하여 출력하는 방식
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>조건문을 이용한 html 출력</title>
</head>
<script>
/* script 안에 jsp문법을 이용하여 출력하는 방법 */
<%
String mid = "";
if(mid == "hong"){
	%>
	alert("환영합니다. 로그인 되셨습니다");
	<%
} else {
	%>
	alert("아이디를 확인할 수 없습니다");
	<%
}
%>
</script>
<body>

</body>
</html>



- jsp 문법에 script 태그를 이용한 출력방식
   HTML 태그 중에서 이벤트 핸들링 함수처럼 JSP를 컨트롤 하는 방식은 없음

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
/*jsp 문법에 script 태그를 이용한 출력방식*/
String mname="홍길동";
if(mname == "홍길동"){
	out.print("<script>alert('확인')</script>");
}else{
	out.print("<script>alert('취소')</script>");
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>조건문을 이용한 html 출력</title>
</head>
<body>

</body>
</html>

 

<body>
<% 
boolean view = true; //조건문을 이용하여 html코드를 핸들링 할 수 있음
%>
<% if(view == true) {%>
<ul>
	<li>메뉴1</li>
	<li>메뉴2</li>
	<li>메뉴3</li>
	<li>메뉴4</li>
</ul>
<% } %>
</body>

 

⚡ 반복문을 이용한 html 출력
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String data[] = {"a","b","c","d"};
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<ol>
	<% 
	int w = 0;
	while(w< data.length){
	%>
	<li> <%=data[w]%> </li>
	<%
		w++;  };
	%>
</ol>
</body>
</html>

 

⚡ jsp => js+html+css... 다컨트롤 가능 

더 상위 언어
<%
	String wh[] = {"100px","200px"};
	String color[] = {"pink","skyblue"};
%>

<style>
.box {width:<%=wh[0]%>; height:<%=wh[1]%>; display:block; background-color:<%=color[0]%>;}
</style>

<body>
	<div class="box"></div>
</body>