gwooden_코린이

이클립스 JSP 내장 객체 본문

JSP&Servlet

이클립스 JSP 내장 객체

gwooden22 2023. 1. 19. 16:47
728x90

1. 내장 객체

request : 요청

response : 응답

out : 출력

 

request 내장 객체 : 요청

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form method="get" action="request.jsp">
		<input type="text" name="teemo">
		<input type="submit" value="전송">
	</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	값 : <%= request.getParameter("teemo") %> <br>
	이름 : <%= request.getParameterNames() %> <br>
	컨텍스트 패스 : <%= request.getContextPath() %> <br>
	요청방식 :<%= request.getMethod() %> <br>
	요청 URL : <%= request.getRequestURL() %> <br>
	요청 URI : <%= request.getRequestURI() %> <br>
	서버이름 : <%= request.getServerName() %> <br>
	프로토콜 : <%= request.getProtocol() %>
</body>
</html>

 

<body>
	<form method="get" action="request.jsp">
		<input type="text" name="teemo">
		<input type="text" name="teemo1">
		<input type="text" name="teemo2">
<%
	Enumeration<String> e = request.getParameterNames();

	while(e.hasMoreElements()) {
		
		out.print(e.nextElement() + "<br>");
	}
%>


request(요청) 처리해서 클라이언트에게 되돌려줄때

 

respone.sendRedirect("이동할 페이지");

 

sendRedirect -> request, respone

 

 

간단한 로그인 페이지

로그인 창 -> 요청 -> 아이디와 비밀번호 체크

-> 아이디와 비밀번호가 일치하면 메인페이로 이동

-> 일치하지 않으면 다시 로그인 페이지로 이동

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>로그인 페이지</title>
</head>
<body>
	<form method="post" action="checkLogin.jsp">
		아이디 : <input type="text" name="id"> <br>
		비밀번호 : <input type="password" name="pw"> <br>
		<input type="submit" value="로그인"> 
	</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		String id = "teemo";
		String pw = "1234";
		
		String user_id = request.getParameter("id");
		String user_pw = request.getParameter("pw");
		
/* 		out.print(id.equals(user_id));
		out.print(user_pw == pw); */
		
 		if(id.equals(user_id) && pw.equals(user_pw)) {
			out.print("로그인성공");
		} else {
			response.sendRedirect("loginForm.jsp");
		} 
	%>
</body>
</html>

변수에 단순 문자열을 대입한것과 새로운 문자열을 변수에 선언을 하면서 서로 비교연산자로 == 물어볼 경우 false로 표시가 된다. 두 변수는 데이터 입력 값을 비교하는게 아닌 할당된 주소값을 비교하기 때문에 주소 값이 달라 같지않다고 표시된다.

 

그래서 주소값끼지 비교하기 위해서는 equals 메서드를 사용해 해결을 하면 된다.

 

String a = "aaa"

String b = "aaa"

String c = new String("aaa")

 

라고 했을 때 a == b로 하면 true가 나오지만 a == c 또는 b == c 라고 하면 false로 나온다.

 

변수를 대입하면 같은 주소값으로 설정되어 true가 나오지만 새로운 문자열을 선언한 경우 대입한 것과 다른 주소값이 설정 되다보니 위에서 언급한것  처럼 equals로 처리하면 해당 변수 안에 있는 데이터 값을 비교하게 해준다.

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		String id = "teemo";
		String pw = "1234";
		
		String user_id = request.getParameter("id");
		String user_pw = request.getParameter("pw");
		
/* 		out.print(id.equals(user_id));
		out.print(user_pw == pw); */
		
 		if(id.equals(user_id) && pw.equals(user_pw)) {
 			response.sendRedirect("loginSu.jsp");
		} else {
			response.sendRedirect("loginForm.jsp");
		} 
	%>
</body>
</html>


forward 요청방식

forward -> request, respone 유지

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form method="get" action="forwardChk.jsp">
		이름 : <input type="text" name="name"> <br>
		나이 : <input type="text" name="age"> <br>
		<input type="submit" value="접속"> 
	</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	int age = Integer.parseInt(request.getParameter("age"));

	//나이가 20살 이상이면 접속페이지로 이동, 아니면
	if(age < 20) {
%>

<script>
	alert("접속불가");
	history.go(-1);
</script>

<%
	} else {
		RequestDispatcher dispatcher = request.getRequestDispatcher("forwardResult.jsp");
		dispatcher.forward(request, response);
	}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		String name = request.getParameter("name");
		String age = request.getParameter("age");
	%>
	
	<h1><%=name %>(<%=age %>)님 환영합니다.</h1>
	
</body>
</html>


어플리케이션 내장 객체

하나의 어플리케이션(프로젝트) 내부에서 사용

정보 저장

request -> 다음 페이지에 정보를 전달해서 사용

 

ex) A페이지 (request 정보저장) -> B 페이지 (저장된 정보를 사용) -> C페이지 (request 사용 X)

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	String appPath = application.getContextPath();
	String filePath = application.getRealPath("application.jsp");
%>

	컨텍스트 패스 : <%=appPath %> <br>
	파일 경로 : <%=filePath %>
</body>
</html>

 

유효 범위

  • page : 하나의 JSP 페이지
  • request : 요청받은 페잊
  • session : 브라우저
  • application : 하나의 프로젝트
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	pageContext.setAttribute("name", "page");
	request.setAttribute("name", "request");
	session.setAttribute("name", "session");
	application.setAttribute("name", "application");
	
	System.out.println("첫번쨰 페이지");
	System.out.println("페이지 : " + pageContext.getAttribute("name"));
	System.out.println("요청 : " + request.getAttribute("name"));
	System.out.println("세션 : " + session.getAttribute("name"));
	System.out.println("어플리케이션 : " + application.getAttribute("name"));
	
	RequestDispatcher dispatcher = request.getRequestDispatcher("second.jsp");
	dispatcher.forward(request, response);
%>


</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	pageContext.setAttribute("name", "page");
	request.setAttribute("name", "request");
	session.setAttribute("name", "session");
	application.setAttribute("name", "application");
	
	System.out.println("첫번쨰 페이지");
	System.out.println("페이지 : " + pageContext.getAttribute("name"));
	System.out.println("요청 : " + request.getAttribute("name"));
	System.out.println("세션 : " + session.getAttribute("name"));
	System.out.println("어플리케이션 : " + application.getAttribute("name"));
	
	RequestDispatcher dispatcher = request.getRequestDispatcher("second.jsp");
	dispatcher.forward(request, response);
%>


</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>세 번째 페이지</h1>
	페이지 : <%=pageContext.getAttribute("name") %> <br>
	요청 : <%=request.getAttribute("name") %> <br>
	세션 : <%=session.getAttribute("name") %> <br>
	어플리케이션 : <%=application.getAttribute("name") %> <br>
</body>
</html>

url 링크만 복사하고 해당 브라우저를 종료하거나 타 브라우저로 이동해서 복사한 링크로 접속 할 경우 세션 사라짐
서버 종료 후 재시작 하고 복사한 링크로 다시 접속 시 어플리케이션 사라짐

728x90
Comments