KMUTNB - Internet Programming 5/7


Published on

Lecture for King Mongkut's University of Technology North Bangkok (KMUTNB) / Computer Science / Internet Programming Course by PHUPHA

Published in: Education
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

KMUTNB - Internet Programming 5/7

  1. 1. JSP Basic By: Mr. PHUPHA PUNYAPOTASAKUL ( ภูผา ปัญญาโพธาสกุล )
  2. 2. Lab outstanding <ul><li>Style sheet </li></ul><ul><li>Java Script </li></ul><ul><li>Eclipse </li></ul><ul><li>JSP Basic </li></ul>
  3. 3. Road map from now <ul><li>Week 6 – (8/12/2007) Lecture </li></ul><ul><li>Week 7 – Lab home work </li></ul><ul><li>Week 8 – Mid-term </li></ul><ul><li>Week 9 – Lecture + Lab quiz </li></ul>
  4. 4. Mid-term exam <ul><li>คะแนนรวม 50 คะแนน </li></ul><ul><li>หัวข้อที่สอบ </li></ul><ul><ul><li>HTTP, HTML 17 คะแนน </li></ul></ul><ul><ul><li>Style sheet 13 คะแนน </li></ul></ul><ul><ul><li>Java Script 13 คะแนน </li></ul></ul><ul><ul><li>JSP Basic 7 คะแนน </li></ul></ul><ul><li>ลักษณะคำถาม </li></ul><ul><ul><li>Multiple choice รวม 24 คะแนน </li></ul></ul><ul><ul><li>เติมคำ + แสดงวิธีทำ 26 คะแนน </li></ul></ul>
  5. 5. JSP Basic <ul><li>JSP Script let <% .. %> </li></ul><ul><li>JSP Expression <%= .. %> same as out.print(“..”); </li></ul><ul><li>JSP Declaration <%! .. %> </li></ul><ul><li>JSP Comment <%-- .. --%> </li></ul>
  6. 6. How does it work? JSP file (*.jsp) Servlet (*.java) Class file (*.class) Compile
  7. 7. Example <ul><li>JSP < % if ( Math . random () < 0.5 ) { % > Have a <B>nice< / B> day ! < % } else { % > Have a <B>lousy< / B> day ! < % } % > </li></ul><ul><li>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;) ; } </li></ul>
  8. 8. View Converted Files <ul><li>Tomcat /{tomcat_dir}/work/.. </li></ul><ul><li>Eclipse + Tomcat /{eclipse_workspace}/ . metadata / . plugins/org . eclipse . wst . server . core/tmp0/work </li></ul>
  9. 9. Servlet Object <ul><li>javax . servlet . Servlet </li></ul><ul><ul><li>javax . servlet . jsp . JspPage </li></ul></ul><ul><ul><ul><li>javax.servlet.jsp.HttpJspPage </li></ul></ul></ul><ul><ul><ul><ul><li>Server implementation Class e.g. org.apache.jasper.runtime.HttpJspBase </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Each page e.g. org . apache . jsp . userman. { jspfilename }_jsp </li></ul></ul></ul></ul></ul>
  10. 10. JSP Page Directive <ul><li>Syntax <%@ page att=“val” %> </li></ul><ul><li>Attributes </li></ul><ul><ul><li>import =&quot; package.class &quot; </li></ul></ul><ul><ul><li>contentType=&quot; MIME - Type &quot; </li></ul></ul><ul><ul><li>isThreadSafe =&quot; true |false &quot; </li></ul></ul><ul><ul><li>session=&quot; true |false&quot; </li></ul></ul><ul><ul><li>buffer =&quot; size kb|none &quot; </li></ul></ul><ul><ul><li>autoflush=&quot; true |false&quot; </li></ul></ul><ul><ul><li>extends =&quot; package.class &quot; </li></ul></ul><ul><ul><li>info=&quot; message &quot; </li></ul></ul><ul><ul><li>errorPage =&quot; url &quot; </li></ul></ul><ul><ul><li>isErrorPage=&quot;true| false &quot; </li></ul></ul><ul><ul><li>language =&quot; java &quot; </li></ul></ul>
  11. 11. JSP Include Directive <ul><li>Syntax < % @ include file =&quot; url &quot; % > </li></ul><ul><li>Compile time including </li></ul><ul><li>URL must be in local </li></ul>
  12. 12. JSP Action <ul><li>jsp : include - Include a file at the time the page is requested. </li></ul><ul><li>jsp:useBean - Find or instantiate a JavaBean </li></ul><ul><li>jsp : setProperty - Set the property of a JavaBean. </li></ul><ul><li>jsp:getProperty - Insert the property of a JavaBean into the output . </li></ul><ul><li>jsp : forward - Forward the requester to a new page. </li></ul>
  13. 13. JSP Include <ul><li>Include at run time in the way that execute pages separately and then combine output later </li></ul><ul><li>Attributes </li></ul><ul><li>page =&quot; { relativeURL | < %= expression % > } &quot; </li></ul><ul><ul><li>The relative URL that locates the resource to be included, or an expression that evaluates to a String equivalent to the relative URL. </li></ul></ul><ul><li>flush=&quot;true | false &quot; </li></ul><ul><ul><li>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 . </li></ul></ul>
  14. 14. JSP Param <ul><li>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> </li></ul><ul><li>Use to submit request parameters to included page </li></ul>
  15. 15. JSP Forward <ul><li>Redirect to anther URL using HTTP header </li></ul><ul><li>Beware when using JSP Forward with page:cache=“none”, cause IllegalStateException </li></ul><ul><li>Attributes </li></ul><ul><li>page=&quot;{ relativeURL | <%= expression %>}&quot; </li></ul><ul><ul><li>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 . </li></ul></ul><ul><li>May use together with JSP Param </li></ul>
  16. 16. JSP useBean <ul><li>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 </li></ul><ul><li>Example <jsp : useBean id =“ userobj &quot; class =“ test.UserObj &quot; scope=“session” / > </li></ul>
  17. 17. Java Bean Example <ul><li>public class MyBean{ </li></ul><ul><li>private String username=“”; </li></ul><ul><li>private String password=“”; </li></ul><ul><li>public String getUsername(){ </li></ul><ul><li>return username; </li></ul><ul><li>} </li></ul><ul><li>public String getPassword(){ </li></ul><ul><li>return password; </li></ul><ul><li>} </li></ul><ul><li>public void setUsername(String u){ </li></ul><ul><li>username=u; </li></ul><ul><li>} </li></ul><ul><li>public void setPassword(String p){ </li></ul><ul><li>password=p; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  18. 18. JSP useBean Attributes <ul><li>id =&quot; beanInstanceName &quot; </li></ul><ul><ul><li>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. </li></ul></ul><ul><li>scope=&quot; page |request|session|application“ </li></ul><ul><li>class =&quot; package.class &quot; </li></ul><ul><li>t ype=&quot; package . class “ </li></ul><ul><li>What is different between class and type? TypeName bean=new ClassName(); </li></ul>
  19. 19. JSP setProperty <ul><li>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 ) </li></ul><ul><li>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). </li></ul>
  20. 20. JSP setProperty <ul><li>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; / > </li></ul><ul><li>Equals to mybean.setUsername(request.getParameter(“username”)) </li></ul>
  21. 21. Predefined (Built-in) Variables <ul><li>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.). </li></ul><ul><li>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. </li></ul>
  22. 22. Predefined (Built-in) Variables <ul><li>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 . </li></ul><ul><li>session This is the HttpSession object associated with the request . </li></ul><ul><li>application This is the ServletContext as obtained via getServletConfig().getContext(). </li></ul>
  23. 23. Predefined (Built-in) Variables <ul><li>config This is the ServletConfig object for this page. User’s defined configuration in web.xml can be retrieve using this object </li></ul><ul><li>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. </li></ul>
  24. 24. Question & Answer