Download presentation
Presentation is loading. Please wait.
Published byBaldwin Daniel Modified over 8 years ago
1
Java Server Pages (JSP) Provide a cross-platform framework for creating dynamic web contents JSP Components: Static HTML/XML parts Special JSP Tags Snippets of Java code called “scriptlets”
2
Client JSP file component JSP Container & Web Server request response
3
JSP Syntax HTML Comment: HTML Comment: Hidden Comment Hidden Comment <%-- documents a JSP file...comments are not sent to the client --%> --%>
4
JSP Syntax Declaration Declaration <%! int num;double salary[20]; String name; %> Expression Expression
5
JSP Syntax Scriptlet Scriptlet <% for (int k=0; k<20; k++) {salary[k] = k *10.0; if (k % 2) salary[k] *= 2; else salary[k] *= 3; }%>
6
Temperature Conversion Temperature Conversion Program Enter a temperature to convert: Select Conversion Type: <input type="radio" checked name="ttype" value="0">F-to-C C-to-F
7
Conversion Results <%!//variable declarations String TempStr, ConversionFlag;int InputVal, OutputVal; %> Conversion Result Degrees Farenheit Centigrade <% TempStr = request.getParameter("temp"); InputVal = Integer.parseInt(TempStr); ConversionFlag= request.getParameter("ttype"); if (ConversionFlag.equals("0")){ //F-to-C Conversion OutputVal = (int) (((((InputVal-32)*100.0)/9.0)*5)/100.0); %> <% } else { OutputVal = (int)((((InputVal *100.0)/5)* 9)/100.0) + 32; %> <% } %> Go back to home page
8
JSP Syntax Include Directive JSP TEST THE CURRENT DATE and TIME: OUTPUT: THE CURRENT DATE and TIME: February 2, 2001 8:45:20
9
JSP Syntax Page Directive defines attributes that apply to an entire JSP page. <%@ page [language=“java”][extends=“package.class”] [import=“package.class”, …] [session=“true|false”][buffer=“none|8kb|sizekb”][autoFlush=“true|false”] [contentType=“mimeType [;text/html”]] %>
10
JSP Standard Actions <jsp:useBean> Associates a JavaBean with the JSP. <jsp:useBean id=“name” scope=“page|request|session|app” {class=“package.class” | type=“package.class” | beanName=“beanName” type=“typeName”}</jsp:useBean>
11
JSP Standard Actions <jsp:setProperty> Sets a property value or values in a bean. Used in conjunction with the action. < jsp:setProperty name=“beanName” {property=“*” | property=“propertyName” | property=“propertyName” param=“paramName” | property=“propertyName”value=“propertyValue”}/>
12
UseBean Sample Enter String: Reverse All Caps All Lower SpellCheck The word is The word has been processed into: <%int selOption=Integer.parseInt(request.getParameter(“proctype”); String processStr=“”; switch (selOption){ case 1: processStr = procword.reverse( ); break; case 2: processStr = procword.upper( ); break; case 3: processStr = procword.lower( ); break; case 4: if ( procword.spellchk( )) { %>RIGHT WRONG<%} ; break; } %>
13
/** * Title: ProcWord bean * Description: A java bean used to process a character string * @version 1.0 */ package myClass; public class ProcWord { private String word; public void setWord(String wordparam){ word = wordparam; } public String reverse(){ return((new StringBuffer(word).reverse()).toString()); } public String upper() { return word.toUpperCase(); } public String lower() { return word.toLowerCase(); } public boolean spellchk(){ if (word.equals("HELLO")) return true; else return false; }
14
JSP Standard Actions <jsp:getProperty> Use to access the properties of a bean. < jsp:getProperty name=“beanName” property=“propertyName” /> The word is The word has been processed into: <%int selOption=Integer.parseInt(request.getParameter(“proctype”); String processStr=“”; switch (selOption){ case 1: processStr = procword.reverse( ); break; case 2: processStr = procword.upper( ); break; case 3: processStr = procword.lower( ); break; case 4: if ( procword.spellchk( )) { %>RIGHT WRONG<%} ; break; } %>
15
JSP Standard Actions <jsp:forward> Forwards a client request to an HTML file, JSP file, or servlet for processing. The lines after in the JSP file are not processed. OR </jsp:forward>
16
JSP Standard Actions <jsp:include> Includes a static file or sends a request to a dynamic file. OR </jsp:include> EXAMPLES:
17
Notes on Using JavaBeans Make sure that you save your class file in the folder that you named in your package line. For example, if the package line is package myClass; The file must be saved in C:\Tomcat\webapps\TESTFILES\Web-inf\Classes\myClasswhere: TESTFILES is the folder where the index.html and the JSP files are stored.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.