표현 언어
EL: Expression Language
값(데이터)을 웹 페이지에 표시(표현)하는 데 사용되는 태그.
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="get" action="testLogin.jsp">
<label for="userid">아이디: </label>
<input type="text" name="id" id="userid"><br>
<label for="userpw">암 호: </label>
<input type="password" name="pw" id="userpw"><br>
<input type="submit" value="로그인">
</form>
</body>
</html>
pageScope | requestScope | sessionScope | applicationScope |
<%=add>
: add는 자바의 변수 이름${add}
: add는 속성 이름servlet
package unit07;
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("/AdditionServlet")
public class AdditionServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int num1 = 20;
int num2 = 10;
int add = num1 + num2;
request.setAttribute("num1", num1);
request.setAttribute("num2", num2);
request.setAttribute("add", add);
RequestDispatcher dispatcher = request.getRequestDispatcher("addition.jsp");
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
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>
고전적인 방식:
<%
int num1 = (Integer)request.getAttribute("num1");
int num2 = (Integer)request.getAttribute("num2");
%>
<%=num1 %> + <%=num2 %> = <%=num1 + num2 %><hr>
EL 방식:
${num1} + ${num2} = ${add}
</body>
</html>