서블릿 JSP

JSP jsp태그

짱코딩러 2022. 9. 13. 20:16

out.write : 문자열을 출력하는 메서드

out.print : 다양한 형태를 출력할 수 있는 메서드

 

<% 순수하게 java형태로 코드를 입력할 수 있다 %>

<%= out.print에 적는 것과 같은 결과를 줄 수 있다 %>

<%! 멤버변수, 멤버함수로 들어감 %>

(*일반적으로는 모두 _jspService라는 메서드 안에 들어감*)

<%@ 페이지 지시자 %>

 

redirect : 현재 작업과 상관 없이 전혀 새로운 요청

forward : 현재 작업하는 내용을 이어갈 수 있도록 공유(request사용)

 

*저장소*

request : forward관계일 때 공유할 수 있는 저장소

pagecontext : 페이지 내에서 혼자 사용하는 페이지 내의 저장소

session,page,cookie

 

spagForm

<%@ 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 action="spagTest.jsp">
		<input type="text" name="num"><br>
		<input type="submit" value="입장" >
	</form>
</body>
</html>

spagTest

<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ 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 num = 0;
	String num_ = request.getParameter("num");	//name이 num인 애한테 받은 값
	if(num_ != null && !num_.equals(""))
		num = Integer.parseInt(num_); 
	
	String result;
	
	if(num%2 !=0)
		result="홀수";
	else
		result="짝수";
	
	request.setAttribute("r",result);
	
	String[] names = {"newrec","dragon"};
	request.setAttribute("names",names);
	
	Map<String, Object> notice = new HashMap<String, Object>();
	notice.put("id",1);
	notice.put("title","EL은 좋아요");
	request.setAttribute("notice", notice);
	
	RequestDispatcher dispatcher = request.getRequestDispatcher("spagResult.jsp");
	dispatcher.forward(request, response);
	%>

</body>
</html>

spagResult

<%@ 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>
<%
	pageContext.setAttribute("aa", "hello");
	pageContext.setAttribute("r", "hi");
%>
</head>
<body>
	<%=request.getAttribute("r")%>입니다. ${ r }입니다.
	<br> ${names[0]}
	<br> ${notice.title}
	<br> ${aa}
	<br> ${requestScope.r}	<!-- 이름이 겹치는 경우 페이지 내의 변수를 표시해줄 수 있다.-->
	<br> ${r}
	<br>
	<b><a href='javascript:history.go(-1)'>다시</a></b>
</body>
</html>

 

*EL표기*

request.getAttribute("cnt") => ${cnt} 같은 뜻임!!

 

 

'서블릿 JSP' 카테고리의 다른 글

Servlet EL, JSTL태그  (0) 2022.09.22
Servlet 자바빈, 액션태그  (0) 2022.09.18
Servlet 저장소  (0) 2022.09.17
JSP 코드 블럭, 내장 객체  (0) 2022.09.12
Servlet 생명 주기  (0) 2022.09.08