[JSP/Servlet] 초기파라미터를 제공하는 EL 예제

초기파라미터를 제공하는 EL(Expression Language) 내장객체 initparam 이용한 확인 예제


web.xml


<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

  <display-name>myjsp</display-name>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

  <!-- 초기파라미터 - context root경로 -->

  <context-param>

  <param-name>rootPath</param-name>

  <param-value>/myjsp</param-value>

  </context-param>

</web-app>



request.jsp


<%@page import="java.util.Date"%>

<%@page import="java.text.SimpleDateFormat"%>

<%@ page contentType="text/html;charset=UTF-8"%>

<%

Cookie c = new Cookie("currentTime",new SimpleDateFormat("HH:mm:ss").format(new Date()));

response.addCookie(c);

%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

<h2>Context Root 경로 명</h2>

<%=application.getContextPath() %><br>

<%=application.getInitParameter("rootPath") %><br>

${initParam.rootPath }<%--초기파라미터를 제공하는 EL 내장객체 initParam --%>


<form action = "${initParam.rootPath }/el/exam3.jsp">

나이 : <input type="number" name = "age"><br>

취미 : <br> 

<label>게임<input type="checkbox" name="hobby" value="게임"></label>

<label>독서<input type="checkbox" name="hobby" value="독서"></label>

<label>영화감상<input type="checkbox" name="hobby" value="영화감상"></label>

<input type="submit" value="전송">

<button>전송2</button>

</form>

</body>

</html>




exam3.jsp


<%@ page contentType="text/html;charset=UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

<h2>쿠키값 출력</h2>

${cookie.currentTime.value } <!-- 쿠키값 EL : cookie.쿠키이름.value -->


<h2>요청파라미터 출력</h2>

age :  ${param.age }<!-- request.getParameter("age")출력과 동일함 -->

<br>

취미 : ${paramValues.hobby }<!-- request.getParameterValues("hobby") : String[] -->

<br>

${paramValues.hobby[0]} - ${paramValues.hobby[1]} -${paramValues.hobby[2]}

</body>

</html>

'BACKEND > Servlet&JSP' 카테고리의 다른 글

[JSP/Servlet] Annotation  (0) 2017.05.30
[JSP/Servlet] Fileter  (0) 2017.05.19
[JSP/Servlet] JSTL  (0) 2017.05.17
[JSP/Servlet] Expression Language 정의 및 간단한 확인 예제  (0) 2017.05.16
[JSP/Servlet] JSP 동적 부분  (0) 2017.05.15

댓글

Designed by JB FACTORY