본문 바로가기

Learning/JSP

JSTL

  • WebContent에 jstl이라는 폴더 만들고 adder.jsp 복사 붙여넣기. action에 addResult_jstl 로 변경

  • jstl 라이브러리 다운받기

https://mvnrepository.com/artifact/javax.servlet/jstl/1.2 에서 jar 파일 다운로드, WEB-INF에 넣기

 

 

 

 

 


 

  • adder.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="addResult_jstl.jsp">

첫번째 수 : <input type="text" name="num1"><br>

두번째 수 : <input type="text" name="num2"><br>

<input type="submit" value="더하기">

</form>

</body>

</html>

  • addResult_jstl.jsp

 

  • set: 변수 지정 키워드

  • choose & when: 조건문

 

 

 

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

    첫번째: ${param.num1}<br/>

    첫번째: ${param.num2}<br/>

    <hr/>

    결과: ${param.num1+param.num2}<br/>

    <c:set var="no" value="${param.num1}"/>

    no: ${no }

    <hr/>

    <c:choose>

    <c:when test="${no mod 2==0}">

        짝수

    </c:when>

    <c:otherwise>

        홀수

    </c:otherwise>

    </c:choose>

    <hr>

    

    <c:if test="${no%2==0 }">

        짝수

    </c:if>

    <c:if test="${no%2!=0 }">

        홀수

    </c:if>

</body>

</html>

 


 

  • color.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="colorResult.jsp">

ID:<input type="text" name="id"><br>

Color:

<select name="color">

    <option value="yellow">노랑</option>

    <option value="blue">blue</option>

    <option value="orange">orange</option>

    <option value="pink">pink</option>  

</select>

<input type="submit" value="보내기">

</form>

</body>

</html>

 


  • colorResult.jsp:출력값 (choose를 써야 when을 쓸 수 있음)

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

    <c:if test="${param.id!=null}">

        <c:set var="id" value="${param.id }"/>

    </c:if>

    <c:if test="${param.id==null || param.id==''}">

        <c:set var="id" value="GUEST"/>

    </c:if>

    color:${param.color} <br>

    <c:choose>

    <c:when test="${param.color=='yellow'}">

        <c:set var="c" value="노랑"/>

    </c:when>

    <c:when test="${param.color=='blue'}">

        <c:set var="c" value="파랑"/>

    </c:when>

    <c:when test="${param.color=='pink'}">

        <c:set var="c" value="분홍"/>

    </c:when>

    <c:when test="${param.color=='orange'}">

        <c:set var="c" value="주황"/>

    </c:when>

    </c:choose>

    ${id}님이 좋아하는 색깔은

    <span style="background-color: ${param.color}">${c}</span>입니다.

</body>

</html>

 


 

 

 

'Learning > JSP' 카테고리의 다른 글

서블릿: 출력경로 통일하기  (0) 2020.07.29
서블릿으로 주소록 만들기  (0) 2020.07.28
서블릿  (0) 2020.07.27
게시판 2  (0) 2020.07.23
게시판 1  (0) 2020.07.22