JSP Basic By: Mr. PHUPHA PUNYAPOTASAKUL ( ภูผา ปัญญาโพธาสกุล )
Lab outstanding Style sheet Java Script Eclipse JSP Basic
Road map from now Week 6 – (8/12/2007) Lecture Week 7 – Lab home work Week 8 – Mid-term Week 9 – Lecture + Lab quiz
Mid-term exam คะแนนรวม 50 คะแนน หัวข้อที่สอบ HTTP, HTML 17 คะแนน Style sheet 13 คะแนน Java Script 13 คะแนน JSP Basic 7 คะแนน ลักษณะคำถาม Multiple choice รวม 24 คะแนน เติมคำ + แสดงวิธีทำ 26 คะแนน
JSP Basic JSP Script let <% .. %> JSP Expression <%= .. %> same as out.print(“..”); JSP Declaration <%! .. %> JSP Comment <%-- .. --%>
How does it work? JSP file (*.jsp) Servlet (*.java) Class file (*.class) Compile
Example JSP < % if ( Math . random () < 0.5 ) { % > Have a <B>nice< / B> day ! < % } else { % > Have a <B>lousy< / B> day ! < % } % > Convert to if ( Math . random () < 0.5 ) { out . println (&quot; Have a <B>nice< / B> day !&quot;) ; } else { out . println (&quot; Have a <B>lousy< / B> day !&quot;) ; }
View Converted Files Tomcat /{tomcat_dir}/work/.. Eclipse + Tomcat /{eclipse_workspace}/ . metadata / . plugins/org . eclipse . wst . server . core/tmp0/work
Servlet Object javax . servlet . Servlet javax . servlet . jsp . JspPage javax.servlet.jsp.HttpJspPage Server implementation Class e.g. org.apache.jasper.runtime.HttpJspBase Each page e.g. org . apache . jsp . userman. { jspfilename }_jsp
JSP Page Directive Syntax <%@ page att=“val” %> Attributes import =&quot; package.class &quot; contentType=&quot; MIME - Type &quot; isThreadSafe =&quot; true |false &quot; session=&quot; true |false&quot; buffer =&quot; size kb|none &quot; autoflush=&quot; true |false&quot; extends =&quot; package.class &quot; info=&quot; message &quot; errorPage =&quot; url &quot; isErrorPage=&quot;true| false &quot; language =&quot; java &quot;
JSP Include Directive Syntax < % @ include file =&quot; url &quot; % > Compile time including URL must be in local
JSP Action jsp : include - Include a file at the time the page is requested. jsp:useBean - Find or instantiate a JavaBean jsp : setProperty - Set the property of a JavaBean. jsp:getProperty - Insert the property of a JavaBean into the output . jsp : forward - Forward the requester to a new page.
JSP Include Include at run time in the way that execute pages separately and then combine output later Attributes page =&quot; { relativeURL | < %= expression % > } &quot; The relative URL that locates the resource to be included, or an expression that evaluates to a String equivalent to the relative URL. flush=&quot;true | false &quot; If the page output is buffered and the flush attribute is given a true value, the buffer is flushed prior to the inclusion, otherwise the buffer is not flushed. The default value for the flush attribute is false .
JSP Param Using with JSP Include, Forward <jsp : include page =&quot; scripts / login . jsp &quot; > <jsp : param name =&quot; username &quot; value =&quot; jsmith &quot; / > <jsp : param name =“ password &quot; value =“ xxxx &quot; / > < / jsp : include> Use to submit request parameters to included page
JSP Forward Redirect to anther URL using HTTP header Beware when using JSP Forward with page:cache=“none”, cause IllegalStateException Attributes page=&quot;{ relativeURL | <%= expression %>}&quot; A String or an expression representing the relative URL of the component to which you are forwarding the request . The component can be another JSP page, a servlet, or any other object that can respond to a request . May use together with JSP Param
JSP useBean locates or instantiates a JavaBeans component. F irst ly attempts to locate an instance of the bean. If the bean does not exist, just instantiates it Example <jsp : useBean id =“ userobj &quot; class =“ test.UserObj &quot; scope=“session” / >
Java Bean Example public class MyBean{ private String username=“”; private String password=“”; public String getUsername(){ return username; } public String getPassword(){ return password; } public void setUsername(String u){ username=u; } public void setPassword(String p){ password=p; } }
JSP useBean Attributes id =&quot; beanInstanceName &quot; A variable that identifies the bean in the scope you specify. You can use the variable name in expressions or scriptlets in the JSP page. scope=&quot; page |request|session|application“ class =&quot; package.class &quot; t ype=&quot; package . class “ What is different between class and type? TypeName bean=new ClassName();
JSP setProperty The <jsp : setProperty> element sets the value of one or more properties in a bean, using the bean's setter methods. You must declare the bean with < jsp : useBean > before you set a property value with <jsp : setProperty> . ( Copy value from request parameters ) Because <jsp : useBean> and <jsp : setProperty> work together, the bean instance names they use must match (that is, the value of name in <jsp : setProperty> and the value of id in <jsp : useBean> must be the same).
JSP setProperty Example <jsp : setProperty name =&quot; mybean &quot; property =&quot;*&quot; / > <jsp : setProperty name =&quot; mybean &quot; property =&quot; username &quot; / > <jsp : setProperty name =&quot; mybean &quot; property =&quot; username &quot; value =&quot; Steve &quot; / > Equals to mybean.setUsername(request.getParameter(“username”))
Predefined (Built-in) Variables request This is the HttpServletRequest associated with the request, and lets you look at the request parameters (via getParameter ), the request type ( GET , POST , HEAD , etc.), and the incoming HTTP headers (cookies, Referer , etc.). response This is the HttpServletResponse associated with the response to the client. Note that, since the output stream (see out below) is buffered, it is legal to set HTTP status codes and response headers, even though this is not permitted in regular servlets once any output has been sent to the client.
Predefined (Built-in) Variables out This is the PrintWriter used to send output to the client . However, in order to make the response object ( see the previous section ) useful, this is a buffered version of PrintWriter called JspWriter. Note that you can adjust the buffer size, or even turn buffering off, through use of the buffer attribute of the page directive . session This is the HttpSession object associated with the request . application This is the ServletContext as obtained via getServletConfig().getContext().
Predefined (Built-in) Variables config This is the ServletConfig object for this page. User’s defined configuration in web.xml can be retrieve using this object page This is simply a synonym for this , and is not very useful in Java. It was created as a placeholder for the time when the scripting language could be something other than Java.
Question & Answer

KMUTNB - Internet Programming 5/7

  • 1.
    JSP Basic By: Mr. PHUPHA PUNYAPOTASAKUL ( ภูผา ปัญญาโพธาสกุล )
  • 2.
    Lab outstanding Stylesheet Java Script Eclipse JSP Basic
  • 3.
    Road map fromnow Week 6 – (8/12/2007) Lecture Week 7 – Lab home work Week 8 – Mid-term Week 9 – Lecture + Lab quiz
  • 4.
    Mid-term exam คะแนนรวม 50 คะแนน หัวข้อที่สอบ HTTP, HTML 17 คะแนน Style sheet 13 คะแนน Java Script 13 คะแนน JSP Basic 7 คะแนน ลักษณะคำถาม Multiple choice รวม 24 คะแนน เติมคำ + แสดงวิธีทำ 26 คะแนน
  • 5.
    JSP Basic JSPScript let <% .. %> JSP Expression <%= .. %> same as out.print(“..”); JSP Declaration <%! .. %> JSP Comment <%-- .. --%>
  • 6.
    How does itwork? JSP file (*.jsp) Servlet (*.java) Class file (*.class) Compile
  • 7.
    Example JSP <% if ( Math . random () < 0.5 ) { % > Have a <B>nice< / B> day ! < % } else { % > Have a <B>lousy< / B> day ! < % } % > Convert to if ( Math . random () < 0.5 ) { out . println (&quot; Have a <B>nice< / B> day !&quot;) ; } else { out . println (&quot; Have a <B>lousy< / B> day !&quot;) ; }
  • 8.
    View Converted FilesTomcat /{tomcat_dir}/work/.. Eclipse + Tomcat /{eclipse_workspace}/ . metadata / . plugins/org . eclipse . wst . server . core/tmp0/work
  • 9.
    Servlet Object javax. servlet . Servlet javax . servlet . jsp . JspPage javax.servlet.jsp.HttpJspPage Server implementation Class e.g. org.apache.jasper.runtime.HttpJspBase Each page e.g. org . apache . jsp . userman. { jspfilename }_jsp
  • 10.
    JSP Page DirectiveSyntax <%@ page att=“val” %> Attributes import =&quot; package.class &quot; contentType=&quot; MIME - Type &quot; isThreadSafe =&quot; true |false &quot; session=&quot; true |false&quot; buffer =&quot; size kb|none &quot; autoflush=&quot; true |false&quot; extends =&quot; package.class &quot; info=&quot; message &quot; errorPage =&quot; url &quot; isErrorPage=&quot;true| false &quot; language =&quot; java &quot;
  • 11.
    JSP Include DirectiveSyntax < % @ include file =&quot; url &quot; % > Compile time including URL must be in local
  • 12.
    JSP Action jsp: include - Include a file at the time the page is requested. jsp:useBean - Find or instantiate a JavaBean jsp : setProperty - Set the property of a JavaBean. jsp:getProperty - Insert the property of a JavaBean into the output . jsp : forward - Forward the requester to a new page.
  • 13.
    JSP Include Includeat run time in the way that execute pages separately and then combine output later Attributes page =&quot; { relativeURL | < %= expression % > } &quot; The relative URL that locates the resource to be included, or an expression that evaluates to a String equivalent to the relative URL. flush=&quot;true | false &quot; If the page output is buffered and the flush attribute is given a true value, the buffer is flushed prior to the inclusion, otherwise the buffer is not flushed. The default value for the flush attribute is false .
  • 14.
    JSP Param Usingwith JSP Include, Forward <jsp : include page =&quot; scripts / login . jsp &quot; > <jsp : param name =&quot; username &quot; value =&quot; jsmith &quot; / > <jsp : param name =“ password &quot; value =“ xxxx &quot; / > < / jsp : include> Use to submit request parameters to included page
  • 15.
    JSP Forward Redirectto anther URL using HTTP header Beware when using JSP Forward with page:cache=“none”, cause IllegalStateException Attributes page=&quot;{ relativeURL | <%= expression %>}&quot; A String or an expression representing the relative URL of the component to which you are forwarding the request . The component can be another JSP page, a servlet, or any other object that can respond to a request . May use together with JSP Param
  • 16.
    JSP useBean locatesor instantiates a JavaBeans component. F irst ly attempts to locate an instance of the bean. If the bean does not exist, just instantiates it Example <jsp : useBean id =“ userobj &quot; class =“ test.UserObj &quot; scope=“session” / >
  • 17.
    Java Bean Example public class MyBean{ private String username=“”; private String password=“”; public String getUsername(){ return username; } public String getPassword(){ return password; } public void setUsername(String u){ username=u; } public void setPassword(String p){ password=p; } }
  • 18.
    JSP useBean Attributes id =&quot; beanInstanceName &quot; A variable that identifies the bean in the scope you specify. You can use the variable name in expressions or scriptlets in the JSP page. scope=&quot; page |request|session|application“ class =&quot; package.class &quot; t ype=&quot; package . class “ What is different between class and type? TypeName bean=new ClassName();
  • 19.
    JSP setProperty The <jsp : setProperty> element sets the value of one or more properties in a bean, using the bean's setter methods. You must declare the bean with < jsp : useBean > before you set a property value with <jsp : setProperty> . ( Copy value from request parameters ) Because <jsp : useBean> and <jsp : setProperty> work together, the bean instance names they use must match (that is, the value of name in <jsp : setProperty> and the value of id in <jsp : useBean> must be the same).
  • 20.
    JSP setProperty Example<jsp : setProperty name =&quot; mybean &quot; property =&quot;*&quot; / > <jsp : setProperty name =&quot; mybean &quot; property =&quot; username &quot; / > <jsp : setProperty name =&quot; mybean &quot; property =&quot; username &quot; value =&quot; Steve &quot; / > Equals to mybean.setUsername(request.getParameter(“username”))
  • 21.
    Predefined (Built-in) Variablesrequest This is the HttpServletRequest associated with the request, and lets you look at the request parameters (via getParameter ), the request type ( GET , POST , HEAD , etc.), and the incoming HTTP headers (cookies, Referer , etc.). response This is the HttpServletResponse associated with the response to the client. Note that, since the output stream (see out below) is buffered, it is legal to set HTTP status codes and response headers, even though this is not permitted in regular servlets once any output has been sent to the client.
  • 22.
    Predefined (Built-in) Variablesout This is the PrintWriter used to send output to the client . However, in order to make the response object ( see the previous section ) useful, this is a buffered version of PrintWriter called JspWriter. Note that you can adjust the buffer size, or even turn buffering off, through use of the buffer attribute of the page directive . session This is the HttpSession object associated with the request . application This is the ServletContext as obtained via getServletConfig().getContext().
  • 23.
    Predefined (Built-in) Variablesconfig This is the ServletConfig object for this page. User’s defined configuration in web.xml can be retrieve using this object page This is simply a synonym for this , and is not very useful in Java. It was created as a placeholder for the time when the scripting language could be something other than Java.
  • 24.