Servlet & JSP – 21 – jsp:useBean – 02

Merhaba Arkadaslar,
Bu bolumde <jsp:useBean> ‘de kullanilan param attribute ozelligini incelecegiz.

Ornek Uygulama

WebContent altinda yeni bir klasor olusturalim;
05_Param

personform1.html

<html>
<body>
	<form id="form1" action="param1.jsp" method="post">
		<div></div>
		<table id="table1" cellspacing="5px" cellpadding="5%">
			<tr>
				<td align="right">First Name:</td>
				<td><input type="text" name="firstName" /></td>
			</tr>
			<tr>
				<td align="right">Last Name:</td>
				<td><input type="text" name="lastName" /></td>
			</tr>
			<tr>
				<td align="right">Year of Birth :</td>
				<td><input type="text" name="yearOfBirth" /></td>
			</tr>

			<tr>
				<td><input type="submit" value="Submit!" /></td>
			</tr>
		</table>
	</form>
</body>
</html>

Basit bir html dosyasi olusturduk. input alanlarimiza yazdigimiz degerleri HttpServletRequest objesi uzerinden getParameter() metodu ile ulasabiliyorduk. HTML dosyamizdan aldigimiz degerleri JSP dosyamizda <jsp:useBean> yapisinda kullanabiliriz;

Burada dikkat edecek olursak year property alani int tipinde oldugu icin getParameter ile input’tan aldigimiz degeri String’ten int’e donusturmemiz gereklidir.

param1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>param1.jsp</title>
</head>
<body>

	<jsp:useBean id="person1" class="_10_UseBean.model.Person">
		<jsp:setProperty name="person1" property="name"
			value='<%=request.getParameter("firstName")%>' />
		<jsp:setProperty name="person1" property="surname"
			value='<%=request.getParameter("lastName")%>' />
		<jsp:setProperty name="person1" property="year"
			value='<%=Integer.parseInt(request.getParameter("yearOfBirth"))%>' />
	</jsp:useBean>

	<jsp:getProperty property="name" name="person1" />
	<jsp:getProperty property="surname" name="person1" />
	<jsp:getProperty property="year" name="person1" />

</body>
</html>

Ornek Uygulama – 2

<jsp:setProperty> ‘de request.getParemeter() metodunu kullanmak yerine param alanini kullanabiliriz.

personform2.html

<html>
<body>
	<form id="form1" action="param2.jsp" method="post">
		<div></div>
		<table id="table1" cellspacing="5px" cellpadding="5%">
			<tr>
				<td align="right">First Name:</td>
				<td><input type="text" name="firstName" /></td>
			</tr>
			<tr>
				<td align="right">Last Name:</td>
				<td><input type="text" name="lastName" /></td>
			</tr>
			<tr>
				<td align="right">Year of Birth :</td>
				<td><input type="text" name="yearOfBirth" /></td>
			</tr>

			<tr>
				<td><input type="submit" value="Submit!" /></td>
			</tr>
		</table>
	</form>
</body>
</html>

param2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>param1.jsp</title>
</head>
<body>

	<jsp:useBean id="person1" class="_10_UseBean.model.Person">
		<jsp:setProperty name="person1" property="name" param="firstName" />
		<jsp:setProperty name="person1" property="surname" param="lastName" />
		<jsp:setProperty name="person1" property="year" param="yearOfBirth" />
	</jsp:useBean>

	<jsp:getProperty property="name" name="person1" />
	<jsp:getProperty property="surname" name="person1" />
	<jsp:getProperty property="year" name="person1" />

</body>
</html>

param=”firstName”  , param=”lastName” , param=”yearOfBirth” kisimlarina dikkat edelim firstName , lastName ve yearOfBirth degerleri HTML dosyamizdaki text alanlarinin ismidir. Yani getParameter() metodunu kullanarak degerlerini alabildigimiz gibi param yapisini kullanarak da bu degerlere ulasabiliriz. <jsp:setProperty> de  dikkat edecek olursak “year” property degeri icin String degerden int degerine donus otomatik olarak yapilmaktadir. Eger expression kullanirsak yukarida yaptigimiz gibi parseInt metodunu kullanmamiz gerekir.

<jsp:setProperty name="person1" property="year" value='<%=Integer.parseInt(request.getParameter("yearOfBirth")) %>' />

Ornek Uygulama – 3

param yapisindan daha kisa bir yol daha var , oncelikle yeni bir html dosyasi olusturalim ;

HTML dosyamizda text alanlarinin isimlerine dikkat edelim , bu isimleri name, surname ve year olarak degistirdik. People sinifinin instance variable’lari name,surname ve year.
Yani HTML deki input name alanlari ile People instance degiskeleri ayni isimde olacak sekilde tanimladik.

personform3.html

<html>
<body>
	<form id="form1" action="param3.jsp" method="post">
		<div></div>
		<table id="table1" cellspacing="5px" cellpadding="5%">
			<tr>
				<td align="right">First Name:</td>
				<td><input type="text" name="name" /></td>
			</tr>
			<tr>
				<td align="right">Last Name:</td>
				<td><input type="text" name="surname" /></td>
			</tr>
			<tr>
				<td align="right">Year of Birth :</td>
				<td><input type="text" name="year" /></td>
			</tr>

			<tr>
				<td><input type="submit" value="Submit!" /></td>
			</tr>
		</table>
	</form>
</body>
</html>

param3.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

	<jsp:useBean id="person1" class="_10_UseBean.model.Person">
		<jsp:setProperty name="person1" property="name" />
		<jsp:setProperty name="person1" property="surname" />
		<jsp:setProperty name="person1" property="year" />
	</jsp:useBean>

	<jsp:getProperty property="name" name="person1" />
	<jsp:getProperty property="surname" name="person1" />
	<jsp:getProperty property="year" name="person1" />

</body>
</html>

<jsp:setProperty> de dikkat edecek olursak value veya param alanini kullanmadik property alanini kullandik. request parametreleri bean’in  property isimleri/instance variable ile eslesirse <jsp:setProperty> de eslesme islemi otomatik olarak yapilacaktir.

Ornek Uygulama – 4

Daha kisa bir yol olarak <jsp:setProperty> de  property alani icin * karakterini kullanabiliriz.

personform4.html

<html>
<body>
	<form id="form1" action="param4.jsp" method="post">
		<div></div>
		<table id="table1" cellspacing="5px" cellpadding="5%">
			<tr>
				<td align="right">First Name:</td>
				<td><input type="text" name="name" /></td>
			</tr>
			<tr>
				<td align="right">Last Name:</td>
				<td><input type="text" name="surname" /></td>
			</tr>
			<tr>
				<td align="right">Year of Birth :</td>
				<td><input type="text" name="year" /></td>
			</tr>

			<tr>
				<td><input type="submit" value="Submit!" /></td>
			</tr>
		</table>
	</form>
</body>
</html>

param4.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

	<jsp:useBean id="person1" class="_10_UseBean.model.Person">
		<jsp:setProperty name="person1" property="*" />
	</jsp:useBean>

	<jsp:getProperty property="name" name="person1" />
	<jsp:getProperty property="surname" name="person1" />
	<jsp:getProperty property="year" name="person1" />

</body>
</html>

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 *