Servlet & JSP – 23 – Expression Language – 02

Merhaba Arkadaslar,
Bu bolumde Expression Language implicit objects kullanimi inceleyecegiz.

Expression Language implicit objects

Expression Language asagidaki implicit objelere sahiptir. Burada dikkat etmemiz gereken nokta bu Expression Language implicit objeleri JSP implicit objeleri ile ayni degildir ! Sadece pageContext implicit objesi hem EL hem JSP de aynidir !

pageScope
requestScope
sessionScope
applicationScope

param
paramValues

header
headerValues

cookie

initParam

pageContext

Ornek Uygulama

Yeni bir klasor olusturalim;
06_EL_ImplicitObject

formGet.html

<html>
<body>
	<form id="form1" action="requestParameterInEL.jsp" method="get">
		<div></div>
		<table id="table1" cellspacing="5px" cellpadding="5%";>
			<tr>
				<td align="right">Isim:</td>
				<td><input type="text" name="firstName" /></td>
			</tr>
			<tr>
				<td align="right">Soy Isim:</td>
				<td><input type="text" name="lastName" /></td>
			</tr>
			<tr>
				<td align="right">Cinsiyet</td>
				<td>
					<select name="gender">
						<option value="Bay" selected="selected">Bay</option>
						<option value="Bayan">Bayan</option>
					</select>
				</td>
			</tr>

			<tr>
				<td align="right">Medeni Hal</td>
				<td>
				    <input type="radio" name="maritalstatus" value="Bekar" />Bekar
					<input type="radio" name="maritalstatus" value="Evli" /> Evli 
					<input type="radio" name="maritalstatus" value="Dul" />Dul 
				    <input type="radio" name="maritalstatus" value="Javaci" />Javaci :)
				 </td>
			</tr>

			<tr>
				<td align="right">Diller</td>
				<td>
				    <input type="checkbox" name="lang" value="Java" /> Java 
				    <input type="checkbox" name="lang" value="C" />C 
				    <input type="checkbox" name="lang" value="C++" />C++
				</td>
			</tr>
			<tr>
				<td><input type="submit" value="Onayla" /></td>
			</tr>
		</table>
	</form>
</body>
</html>

requestParameterInEL.jsp

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

${param.firstName} <br>
${param.lastName }<br>
${param.gender }<br>
${param.maritalstatus}<br>
${param.lang }<br>
${paramValues.lang[0]}
${paramValues.lang[1]}

</body>
</html>

param , EL implicit objesini kullanarak , formumuzdaki parametrelere ulasabiliriz.
paramValues , El implicit objesini kullanarak checkboxtaki birden fazla parametre degerine ulasabiliriz.

requestScope , sessionScope , applicationScope EL implicit objelerini daha iyi anlamak icin ornek bir uygulama yapalim.

Ornek Uygulama – 2

ELImplicit.java

package _11_ExpressionLanguage.implicitobject;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import _11_ExpressionLanguage.model.Employee;

public class ELImplicit extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		Employee employee = new Employee();
		employee.setName("request scope employee name");
		
		Employee employee2 = new Employee();
		employee2.setName("session scope employee name");
		
		req.setAttribute("employee", employee);
		req.setAttribute("test.attribute", employee);
		
		req.getSession().setAttribute("employee", employee2);
		
		String str1="request scope str";
		String str2="session scope str";
		String str3="context scope str";
		
		req.setAttribute("str", str1);
		req.getSession().setAttribute("str", str2);
		getServletContext().setAttribute("str", str3);
		
		
		Cookie userNameCookie = new Cookie("userName", "levent");
		resp.addCookie(userNameCookie);
		
					
		RequestDispatcher view = req.getRequestDispatcher("06_EL_ImplicitObject/implicitobject.jsp");
		view.forward(req, resp);
	}
}
	<servlet>
		<servlet-name>ExpressionLanguageImplicitObject</servlet-name>
		<servlet-class>_11_ExpressionLanguage.implicitobject.ELImplicit</servlet-class>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>ExpressionLanguageImplicitObject</servlet-name>
		<url-pattern>/elimplicit</url-pattern>
	</servlet-mapping>
  • employee ve employee2 isimli Employee turundeki objelerimizi attribute olarak ekleyelim.
  • Benzer sekilde str isimli attribute objemizi request, scope ve context scope’larina ekledik.
  • Sonrasinda JSP dosyamiza dispatch ettik.

JSP dosyamizda ilgili scopelardan ayni isimli attribute objelerimize ulasmak icin requestScope , sessionScope , applicationScope implicit objelerini kullanabiliriz.

	${employee.name } <br>
	${requestScope.employee.name } <br>
	${sessionScope.employee.name } <br>
	${requestScope["test.attribute"].name } <br>

	${str} <br> 
	${requestScope.str} <br>
	${sessionScope.str} <br>
	${applicationScope.str } <br>
        ${cookie.userName.value }

Eger ilgili implicit obje page scope’ta yoksa bu durumda sirasiyla request , session , context scope’a bakilacaktir.

${employee.name }

JSP dosyamizda , cookie EL implicit objemiz araciligiyla ulasabiliriz. Cookie ismi “userName” , degerine ulasmak icin value property degerini kullanacagiz.

${cookie.userName.value }

 

Ornek Uygulama – 3

header , Expression Language implicit objesini kullanarak header bilgilerine ulasabiliriz.

	<%= request.getHeader("host") %>
	${header["host"] }
	${header.host }

JSP implicit objesi olan request araciligiyla host bilgisine ulastik. Benzer sekilde header Expression Language objesi ile host bilgisine ulasabiliriz.

	<%=request.getMethod()%> <br>
	<%--${request.method } 
	    ${requestScope.method } 
	 --%>
  • request adinda Expression Language’e ait bir implicit object yoktur ! request adinda JSP implicit object vardir.
  • JSP implicit object ile Expression Language implicit object’ler pageContext haric ayni degildir.
  • requestScope adinda Expression Language’e ait bir implicit object vardir.
  • requestScope , request scope attribute’leri tutan Map’tir . requestScope objesi request objesinin kendisi degildir. requestScope request attribute objelerine ulasabiliriz , request properties’lere ulasamayiz.
  • pageContext EL implicit objesi araciligiyla request property degerine sonrasinda ise method property degerine ulasabiliriz.
${pageContext.request.method}

Ornek Uygulama – 4

initParam , context init paremetrelerini tutan Map’tir.
initParam
,implicit objemiz aracigiyla projemizdeki context init parametrelere ulasabiliriz. Unutmayalim initParam , implicit objemiz servlet init parametreleri icin degil context init parametreleri icindir.

Projemizde email isminde context init parametremiz vardi , JSP dosyamizda bu context init parametremize ulasabiliriz.

${initParam.email }

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6, Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *