kmutnb - internet programming 5/7
DESCRIPTION
Lecture for King Mongkut's University of Technology North Bangkok (KMUTNB) / Computer Science / Internet Programming Course by PHUPHATRANSCRIPT
JSP Basic
By: Mr. PHUPHA PUNYAPOTASAKUL
(ภู�ผา ปั�ญญาโพธาสกุ�ล)
04/10/23 Free template from www.brainybetty.com 2
Lab outstanding
• Style sheet
• Java Script
• Eclipse
• JSP Basic
04/10/23 Free template from www.brainybetty.com 3
Road map from now
• Week 6 – (8/12/2007) Lecture
• Week 7 – Lab home work
• Week 8 – Mid-term
• Week 9 – Lecture + Lab quiz
04/10/23 Free template from www.brainybetty.com 4
Mid-term exam
• คะแนนรวม 50 คะแนน• หั�วข้�อที่��สอบ
– HTTP, HTML 17 คะแนน– Style sheet 13 คะแนน– Java Script 13 คะแนน– JSP Basic 7 คะแนน
• ล�กุษณะค"าถาม– Multiple choice รวม 24 คะแนน– เติ&มค"า + แสดงว&ธ�ที่"า 26 คะแนน
04/10/23 Free template from www.brainybetty.com 5
JSP Basic
• JSP Script let<% .. %>
• JSP Expression<%= .. %> same as out.print(“..”);
• JSP Declaration<%! .. %>
• JSP Comment<%-- .. --%>
04/10/23 Free template from www.brainybetty.com 6
How does it work?
JSP file(*.jsp)
JSP file(*.jsp)
Servlet(*.java)
Servlet(*.java)
Class file(*.class)
Class file(*.class)
Compile
04/10/23 Free template from www.brainybetty.com 7
Example
• JSP<% if (Math.random() < 0.5) { %>Have a <B>nice</B> day! <% } else { %>Have a <B>lousy</B> day!<% } %>
• Convert toif (Math.random() < 0.5) {out.println("Have a <B>nice</B> day!");} else {out.println("Have a <B>lousy</B> day!");}
04/10/23 Free template from www.brainybetty.com 8
View Converted Files
• Tomcat/{tomcat_dir}/work/..
• Eclipse + Tomcat/{eclipse_workspace}/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/work
04/10/23 Free template from www.brainybetty.com 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 pagee.g. org.apache.jsp.userman. {jspfilename}_jsp
04/10/23 Free template from www.brainybetty.com 10
JSP Page Directive
• Syntax<%@ page att=“val” %>
• Attributes– import="package.class" – contentType="MIME-Type" – isThreadSafe="true|false" – session="true|false" – buffer="sizekb|none" – autoflush="true|false" – extends="package.class" – info="message" – errorPage="url" – isErrorPage="true|false" – language="java"
04/10/23 Free template from www.brainybetty.com 11
JSP Include Directive
• Syntax<%@ include file="url" %>
• Compile time including
• URL must be in local
04/10/23 Free template from www.brainybetty.com 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.
04/10/23 Free template from www.brainybetty.com 13
JSP Include
• Include at run time in the way that execute pages separately and then combine output later
• Attributes• page="{ relativeURL | <%= expression %> }"
– The relative URL that locates the resource to be included, or an expression that evaluates to a String equivalent to the relative URL.
• flush="true | false" – If the page output is buffered and the flush attribute is gi
ven 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.
04/10/23 Free template from www.brainybetty.com 14
JSP Param
• Using with JSP Include, Forward<jsp:include page="scripts/login.jsp"> <jsp:param name="username" value="jsmith" /> <jsp:param name=“password" value=“xxxx" />
</jsp:include>
• Use to submit request parameters to included page
04/10/23 Free template from www.brainybetty.com 15
JSP Forward
• Redirect to anther URL using HTTP header• Beware when using JSP Forward with
page:cache=“none”, cause IllegalStateException• Attributes• page="{relativeURL | <%= expression %>}"
– 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
04/10/23 Free template from www.brainybetty.com 16
JSP useBean
• locates or instantiates a JavaBeans component. Firstly attempts to locate an instance of the bean. If the bean does not exist, just instantiates it
• Example<jsp:useBean id=“userobj" class=“test.UserObj" scope=“session”/>
04/10/23 Free template from www.brainybetty.com 17
Java Bean Examplepublic 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;}
}
04/10/23 Free template from www.brainybetty.com 18
JSP useBean Attributes
• id="beanInstanceName" – 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="page|request|session|application“• class="package.class" • type="package.class“
• What is different between class and type?TypeName bean=new ClassName();
04/10/23 Free template from www.brainybetty.com 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).
04/10/23 Free template from www.brainybetty.com 20
JSP setProperty
• Example<jsp:setProperty name="mybean" property="*" /> <jsp:setProperty name="mybean" property="username" /> <jsp:setProperty name="mybean" property="username"
value="Steve" />
• Equals tomybean.setUsername(request.getParameter(“username”))
04/10/23 Free template from www.brainybetty.com 21
Predefined (Built-in) Variables
• requestThis 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.).
• responseThis 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.
04/10/23 Free template from www.brainybetty.com 22
Predefined (Built-in) Variables
• outThis 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.
• sessionThis is the HttpSession object associated with the request.
• applicationThis is the ServletContext as obtained via getServletConfig().getContext().
04/10/23 Free template from www.brainybetty.com 23
Predefined (Built-in) Variables
• configThis is the ServletConfig object for this page. User’s defined configuration in web.xml can be retrieve using this object
• pageThis 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.
04/10/23 Free template from www.brainybetty.com 24
Question & Answer