데일리로그C:
article thumbnail

[ EL ]

el_test1.jsp

<%@ 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="el_test2.jsp" method="post">
<table>
	<tr>
		<td>이름 : </td>
		<td><input type="text" name="name" value="홍길동"></td>
	</tr>
	<tr>
		<td colspan=2 align=center><input type="submit" value="입력"></td>
	</tr>
</table>
</form>

</body>
</html>

el_test2.jsp

<%@ 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.setCharacterEncoding("utf-8");

String name = request.getParameter("name"); // el_test1에서 넘어온 변수명
%>

name : <%=name %><br>

name : ${param.name}<br> <!-- EL param 객체 이용함 -->

name : ${param['name']}<br> <!-- EL param 객체 이용함 -->

name : ${requestScope.name}<br>  <!-- EL에서는 param으로 받아온건 무조건 param으로(request 못받아옴)  -->

<hr>

<%
request.setAttribute("id", "1111"); // 개발자가 변수명과 값을 넣고싶다면 setAttribute() 메소드 이용
pageContext.setAttribute("id","2222");
%>

id : <%=request.getAttribute("id") %><br> <!-- 출력 -->

id : ${requestScope.id} <br> <!-- request.setAttribute로 받아온건 requestScope로 받기 -->

id : ${pageScope.id} <br> <!-- 현재페이지에서만 출력가능 -->
 
</body>
</html>


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>EL 연산자 예제</title>
</head>
<body>

${5+7} <br> <!-- 12 출력 -->

\${5+7} = ${5+7} <br>  <!-- 앞에 역슬러시 붙이면 문자열로 인식 ${5+7} = 12 출력-->

${10%2} <br> <!-- 나머지 값 0 출력 -->

\${10%2} = ${10%2} <br> <!-- ${10%2} = 0 출력 -->

\${7<8} = ${7<8} <br>

${5+3 == 8?8:10} <br> <!-- 삼항연산자 : 8이라면(참) 8로 보여주고 아니라면(else) 10 보여주자-->

${5+3 == 8?"참":"거짓"} <br> 




</body>
</html>


[JSTL & EL ]

jstl_core_ex1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!-- c태그로 쓰겠다 -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<!-- c태그에서 변수  var 변수명-->
<c:set var="test" value="hi! 안녕" />

<c:out value="${test }" /><br>  <!-- c:out으로 안해도 됨 -->
out : ${test } <br>

<c:remove var="test"/>
remove : ${test }<br>

<c:catch var="err">
	<%=10/0 %> <!-- 오류 잡음(java.lang.ArithmeticException: / by zero) -->
	${10/0 }  <!-- 오류 안잡음  -->
</c:catch>

err : ${err } <br>

<hr>
<!--test="조건" 츌력:참이네 -->
<c:if test="${5>0 }">
	참이네
</c:if> 

<!--test="조건" false라서 출력안함 -->
<c:if test="${5<0 }">
	거짓이네
</c:if> 

<hr>

<c:set var="a" value="5" />
<!-- when = if --> <!-- when = else if -->  <!-- otherwise = else --> 
<c:choose>
	<c:when test="${a>0 }">양수</c:when>  
	<c:when test="${a==0 }">0</c:when>  
	<c:otherwise>음수</c:otherwise> 
</c:choose>

<hr>

// 반복문<br>
<c:forEach var="a" begin="1" end="10" step="1">
	${a}, 
</c:forEach>

<hr>

// var = 변수명 delims = 구분 요소 <br>
<c:forTokens var="bb" items="a,b,c,d,e" delims=",">
	${bb }
</c:forTokens>

<hr>

<c:set var="cc" value="홍길동, 유관순, 강감찬" />
<c:forTokens var="dd" items="${cc }" delims=",">
	${dd }
</c:forTokens>

<hr>

// script와 c:redirect 동일함<br>
<script>
	//location.href = "index.jsp";
</script>

<% 
//response.sendRedirect("index.jsp");

String id ="a123";
%>
<c:redirect url="index.jsp" />
<c:set var="pass" value="b123" />

<a href="index.jsp"?id=<%=id%>&pass=${pass }">이동</a>

</body>
</html>


test01.jsp

<%@ 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="EL_cul_proc.jsp" method="post"> <!--변수 3개 전달(ex1, ex2, ex3)  -->
	<input type="text" name="ex1"> 
	<select name="ex2">
		<option value="+"> + </option>
		<option value="-"> - </option>
		<option value="*"> * </option>
		<option value="/"> / </option>
	</select> 
	<input type="text" name="ex3"> 
	<input type="submit" value="확인"> 
</form>

</body>
</html>

EL_cul_proc.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<%
int x = Integer.parseInt(request.getParameter("ex1"));
String giho = request.getParameter("ex2");
int y = Integer.parseInt(request.getParameter("ex3"));
%>


<c:if test="${param.ex2 == '+'}">${param.ex1}+${param.ex3} = ${param.ex1 + param.ex3 }</c:if>
<c:if test="${param.ex2 == '-'}">${param.ex1}-${param.ex3} = ${param.ex1 - param.ex3 }</c:if>
<c:if test="${param.ex2 == '*'}">${param.ex1}*${param.ex3} = ${param.ex1 * param.ex3 }</c:if>
<c:if test="${param.ex2 == '/'}">
	<c:choose>
		<c:when test="${param.ex3 == 0}">0으로 나눌 수 없습니다.</c:when>
		<c:otherwise>${param.ex1}/${param.ex3} = ${param.ex1 / param.ex3 }</c:otherwise>
	</c:choose>
</c:if>

<hr>
<c:choose>
	<c:when test="${param.ex2 == '+'}">${param.ex1}+${param.ex3} = ${param.ex1 + param.ex3 }</c:when>
	<c:when test="${param.ex2 == '-'}">${param.ex1}-${param.ex3} = ${param.ex1 - param.ex3 }</c:when>
	<c:when test="${param.ex2 == '*'}">${param.ex1}*${param.ex3} = ${param.ex1 * param.ex3 }</c:when>
	<c:otherwise test="${param.ex2 == '/'}">
		<c:choose>
			<c:when test="${param.ex3 == 0}">0으로 나눌 수 없습니다.</c:when>
			<c:otherwise>${param.ex1}/${param.ex3} = ${param.ex1 / param.ex3 }</c:otherwise>
		</c:choose>
	</c:otherwise>
</c:choose>






</body>
</html>

 


test02.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

문자열 : ${"test"}<br>
문자열 : ${'test' }<br>
정수 : ${123}<br>
소수점 : ${12.34}<br>
Boolean : ${true}<br>
null : ${null }<br>

<hr>

<%
int ss[] = new int[] {10,20,30};

pageContext.setAttribute("s", new int[] {1,2,3});
request.setAttribute("r", new int[] {4,5,6});
%>

<%=ss[0]%> <%=ss[1]%> <%=ss[2]%> <br>  <!-- 10 20 30 출력 -->
${pageScope.s[0]} ${pageScope.s[1]} ${pageScope.s[2]} <br> <!-- 1 2 3 출력 -->
${requestScope.r[0]} ${requestScope.r[1]} ${requestScope.r[2]}<br> <!-- 4 5 6 출력 -->

</body>
</html>

<%
int ss[] = new int[] {10,20,30};

pageContext.setAttribute("s", new int[] {1,2,3});
request.setAttribute("s", new int[] {4,5,6});
%>

<%=ss[0]%> <%=ss[1]%> <%=ss[2]%> <br>  <!-- 10 20 30 출력 -->
${pageScope.s[0]} ${pageScope.s[1]} ${pageScope.s[2]} <br> <!-- 1 2 3 출력 -->
${requestScope.s[0]} ${requestScope.s[1]} ${requestScope.s[2]}<br> <!-- 4 5 6 출력 -->

같은 이름이 존재한다면 pageScope가 주체를 가지게 된다

<%
int ss[] = new int[] {10,20,30};

pageContext.setAttribute("s", new int[] {1,2,3});
request.setAttribute("s", new int[] {4,5,6});
session.setAttribute("s", new int[] {7,8,9});
%>

<%=ss[0]%> <%=ss[1]%> <%=ss[2]%> <br>  <!-- 10 20 30 출력 -->
${s[0]} ${s[1]} ${pageScope.s[2]} <br> <!-- 1 2 3 출력 -->
${requestScope.s[0]} ${requestScope.s[1]} ${requestScope.s[2]}<br> <!-- 4 5 6 출력 -->
${sessionScope.s[0]} ${sessionScope.s[1]} ${sessionScope.s[2]}<br> <!-- 7 8 9 출력 -->


test03,.jsp

<%@ 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="post"> <!--action 없으므로 자신페이지로 돌아와서 출력  -->
id : <input name="id"><br>
pass : <input name="pass"> <br>
<button>login</button>
<hr>

<h3>스크립틀릿</h3>
id : <%=request.getParameter("id") %> <br>
pass : <%=request.getParameter("pass") %> <br>

<h3>EL</h3>
id : ${param.id } <br>
pass : ${param.pass } <br>
</form>

</body>
</html>


test04.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<%
int sum =0; // 전역변수
for(int i=1; i<=10; i++) {
	sum+=i;
}
out.print("총합 : "+sum+"<br>");
%>

<c:set var="sum" value="0" /> <!-- 전역변수  -->
<c:forEach var="i" begin="1" end="10">
	<c:set var="sum" value="${sum=sum+i}" /> <!-- 지역변수 -->
</c:forEach>

총합 : <c:out value="${sum}" />

</body>
</html>


test05.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

피라미드 만들기<br>

*<br>
**<br>
***<br>
****<br>
<br>
<c:forEach var="i" begin="1" end="4">
	<c:forEach var="j" begin="1" end="${i}">*</c:forEach>
	<br>
</c:forEach>

<hr>
역순 피라미드<br>
****<br>
***<br>
**<br>
*<br>
<br>

<c:set var="a" value="4"/>
<c:forEach var="x" begin="1" end="${a}">
	<c:forEach var="y" begin="1" end="${a}">*</c:forEach>
	<c:set var="a" value="${a-1 }"/>
	<br>
</c:forEach>

</body>
</html>

 


Hello.java (servlet)

package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/hello")  // (/member/hello)로 할 때
public class Hello extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
 
    public Hello() {
        super();
       
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String str = "hi~ 안녕";
		
		request.setAttribute("str2", str); // request객체에 str2를 추가(test.jsp에 전달할 변수)
		
		// java에서 forward 할 때 이용하는 두줄임
		RequestDispatcher dis = request.getRequestDispatcher("/test.jsp"); //(/member/test.jsp or test.jsp로 설정)
		dis.forward(request, response); // 주체는 hello.java 일은 jsp가 
	}

	
	

}

test.jsp

<%@ 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>

<%-- ${requestScope.str2 } --%>
${str2 }

</body>
</html>

 

'JAVA > model2' 카테고리의 다른 글

230228_web2(4)-회원목록  (0) 2023.02.28
230227_web2(3) admin  (0) 2023.02.27
230223 _ Scope, parameter vs attribute, 쿠키, pstmt  (0) 2023.02.23
23.02.22_EL,JSTL,servlet 등  (0) 2023.02.22
bb6  (0) 2023.01.30
profile

데일리로그C:

@망밍

포스팅이 도움됐다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

profile on loading

Loading...