JSP Ders03 - Örtülü Nesneler


     JSP’de 9 örtülü nesne vardır.Bu nesneleri Servlette olduğu gibi tanımlamamıza gerek yoktur.Bize hazır olarak web container tarafından sunulmuştur.
Nesne
Tip
out
JspWriter
request
HttpServletRequest
response
HttpServletResponse
config
ServletConfig
application
ServletContext
session
HttpSession
pageContext
PageContext
page
Object
exception
Throwable


index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>index.html</title>
</head>
<body>

 <form action="1.jsp">
  ad<br /> <input type="text" name="ad" /><br />
  <br /> <input type="submit" value="gonder" />
 </form>


</body>
</html>

1.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>1.jsp</title>
</head>
<body>


<% 

   String ad=request.getParameter("ad"); 
   session.setAttribute("ad",ad);
   response.sendRedirect("2.jsp");
%>



</body>
</html>

2.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>2.jsp</title>
</head>
<body>

<%
 out.println("merhaba " +session.getAttribute("ad") +",<br/>");

%>


<%= config.getInitParameter("sayfa")+".sayfadasin" %>

<%="toplamda "+ application.getInitParameter("sayfaSayisi")+" sayfa var." %>


<%this.log("-----test-------"); %>
</body>
</html>

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" id="WebApp_ID" version="3.1">
  <display-name>JSP03</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  
  <servlet>
        <servlet-name>bir</servlet-name>
        <jsp-file>/1.jsp</jsp-file>
        <init-param>  
            <param-name>sayfa</param-name>  
 <param-value>1</param-value>  
        </init-param>
  </servlet>
  
  <servlet-mapping>  
        <servlet-name>bir</servlet-name>  
         <url-pattern>/1.jsp</url-pattern>  
  </servlet-mapping> 
 
  <context-param>
       <param-name>sayfaSayisi</param-name>  
       <param-value>2</param-value> 
  </context-param>
 
  <servlet>
        <servlet-name>iki</servlet-name>
       <jsp-file>/2.jsp</jsp-file>
       <init-param>  
 <param-name>sayfa</param-name>  
 <param-value>2</param-value>  
       </init-param>
  </servlet>

   <servlet-mapping>  
        <servlet-name>iki</servlet-name>  
        <url-pattern>/2.jsp</url-pattern>  
   </servlet-mapping> 
</web-app>

JSP implicit objects

JSP implicit objects

JSP implicit objects

Geri kalan örtülü nesnelerle yeni bir örnek yapalım.

index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>index.html</title>
</head>
<body>

 <form action="1.jsp">
  ad <input type="text" name="ad" /><br /><br />
  <input type="text" name="sayi1" /> / <input type="text" name="sayi2" /> = ?
  <br /><br />  <input type="submit" value="gonder" />
 </form>


</body>
</html>

1.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>1.jsp</title>
</head>
<body>


<%@ page errorPage="error.jsp" %>
<%

String ad=request.getParameter("ad"); 

int sayi1=Integer.valueOf(request.getParameter("sayi1")); 
int sayi2=Integer.valueOf(request.getParameter("sayi2")); 

pageContext.setAttribute("ad",ad,pageContext.SESSION_SCOPE);


out.println(sayi1/sayi2);

%>



</body>
</html>

error.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>2.jsp</title>
</head>
<body>

<%@page isErrorPage="true" %>

<%String ad=(String)pageContext.getAttribute("ad",pageContext.SESSION_SCOPE); %>
<%="Merhaba "+ad +" ,<br/><br/>"%>

<%="hata adi : "+exception %>
</body>
</html>

JSP implicit objects

JSP implicit objects

JSP implicit objects

JSP implicit objects

Hata işleme jsp sayfaları yerine web.xml sayfalarında yapılabilir.Böylece hatanın işleneceği sayfalarda aşağıdaki tagı kullanmaya gerek kalmaz.

<%@ page errorPage="error.jsp" %>

web.xml sayfasında hata işleme örneği
    <web-app>  
        <error-page>  
            <exception-type>java.lang.Exception</exception-type>  
            <location>/error.jsp</location>  
        </error-page>  
    </web-app>  

veya
    <web-app>  
         <error-page>  
             <error-code>500</error-code>  
            <location>/error.jsp</location>  
        </error-page>  
    </web-app>  


Yorumlar

Bu blogdaki popüler yayınlar

Java SE Ders24 - Composition (Kompozisyon)

Spring Ders20 - Aspect Oriented Programming - AspectJ Annotation Style

JSF Ders30 - Page Template (Sayfa Şablonu)