Faculty of Information Technology 3124232549 Advanced Internet Programming
- Slides: 53
Faculty of Information Technology 31242/32549 Advanced Internet Programming Java Server Pages (JSP) V 5 © Copyright UTS Faculty of Information Technology 2008 – JSP 1
Topics Faculty of Information Technology • JSP architecture • JSP syntax • • • objects directives scripting elements standard actions expression language JSP and Java. Beans Tag libraries JSP deployment Model 1 and Model 2 JSP architectures © Copyright UTS Faculty of Information Technology 2008 – JSP 3
J 2 EE & JSP © Copyright UTS Faculty of Information Technology 2008 – JSP Faculty of Information Technology 4
JSP architecture HTTP Faculty of Information Technology Web Server JVM Compile + Execute JSP JDBC HTML + JSP file © Copyright UTS Faculty of Information Technology 2008 – JSP 5
JSP architecture Faculty of Information Technology • Advantages: – all the advantages of servlets • efficient, robust, secure, access to Java libraries – uses "page-centric" coding • HTML that contains embedded code • Disadvantages: – minor performance issue first time a JSP is loaded • has to be compiled on-the-fly • only affects very first request to a JSP • after first request, compiled code is cached for faster execution © Copyright UTS Faculty of Information Technology 2008 – JSP 6
Topics Faculty of Information Technology • JSP architecture • JSP syntax • • • objects directives scripting elements standard actions execution JSP and Java. Beans Tag libraries JSP deployment Model 1 and Model 2 JSP architectures © Copyright UTS Faculty of Information Technology 2008 – JSP 7
Not JSP syntax Faculty of Information Technology • JSP is an embedded language like ASP, PHP <P>This is my ASP Hello World program. So <% First. Name = Request. Form("FIRSTNAME") Response. Write "hi " & First. Name & vb. Cr. Lf %> </P> <P>This is my PHP Hello World program. So <? php $firstname = $HTTP_POST_VARS['FIRSTNAME']; echo "hi ". $firstname. "n"; ? > </P> © Copyright UTS Faculty of Information Technology 2008 – JSP 8
JSP syntax Faculty of Information Technology <P>This is my JSP Hello World program. So <% String first. Name = request. get. Parameter("FIRSTNAME"); out. println("hi " + first. Name); %> </P> • Java code is enclosed by <%. . . %> • Outside those tags, it is normal HTML • e. g. : http: //127. 0. 0. 1: 7001/My. App/hw. jsp? FIRSTNAME=Wayne This is my JSP Hello World program. So hi Wayne © Copyright UTS Faculty of Information Technology 2008 – JSP 9
JSP Objects Faculty of Information Technology • You can access Java objects from within JSP’s • Main types of objects – Implicit objects (provided by the container) – Application-specific objects • Local, instance & class variables • Java. Beans • EJBs • Web container provides a context to provide scope of object © Copyright UTS Faculty of Information Technology 2008 – JSP 10
JSP implicit objects © Copyright UTS Faculty of Information Technology 2008 – JSP Faculty of Information Technology 11
JSP implicit objects © Copyright UTS Faculty of Information Technology 2008 – JSP Faculty of Information Technology 12
Kinds of embedded JSP code Faculty of Information Technology 1. Directives 2. Scripting Elements • declarations • scriptlets • expressions 3. Actions • • include forward use. Bean get/set. Property 4. expression language (EL) © Copyright UTS Faculty of Information Technology 2008 – JSP 13
JSP Directives Faculty of Information Technology <%@ directivename attribute=“value” %> • Note: <%@directive %> tags • Directives are instructions to the JSP compiler – e. g. "include", includes at compile-time, NOT run-time • Main directives – page – include – taglibs (later) © Copyright UTS Faculty of Information Technology 2008 – JSP 14
JSP Directives Faculty of Information Technology • page Directive <%@ page import=“java. util. *, java. io. *” %> • include Directive [not at runtime!] <%@ include file=“copyright. html” %> <%@ page language="java" import="java. util. *" info="My wonderful JSP example" %> <%@ include file="other_file. html"> © Copyright UTS Faculty of Information Technology 2008 – JSP 15
Scripting Elements: Declarations Faculty of Information Technology ! <% Java variable and method declarations %> • Declarations executed when page is initialised – used to define class-wide variables and methods – declarations must produce no output <%! int i = 0; %> <%! int icubed = 0; %> <%! public int cubed (int j) { return (j * j); } %> © Copyright UTS Faculty of Information Technology 2008 – JSP 16
Scripting Elements: Scriptlets Faculty of Information Technology • Basically any Java code can go in a scriptlet • Executed during the request-processing time <% Valid Java code statements %> <% try { i = Integer. parse. Int(request. get. Parameter("mynum")); } catch (Number. Format. Exception nfe) { out. println("Go back and enter a valid number. "); i = 0; } icubed = cubed(i); %> © Copyright UTS Faculty of Information Technology 2008 – JSP 17
Scripting Elements: Expressions <% Faculty of Information Technology = Java expression to be evaluated %> • Expressions are primarily for inserting values of Java variables into HTML code without having to type in full as a scriptlet: – eg: <% out. print(i); %> vs <%= i %> No ; <BODY> <P>You entered the value <%= i %></P> <P>That number cubed is <%= icubed %></P> <P>I can say the same thing with <%= cubed(i) %></P> </BODY> © Copyright UTS Faculty of Information Technology 2008 – JSP 18
Standard Actions Faculty of Information Technology • Well-known tags that affect the runtime behaviour • Generates the Java code that corresponds to the required task - during conversion < jsp: include page=“myjsp. jsp” flush=“true” /> • Other standard actions – – <jsp: use. Bean> <jsp: set. Property> <jsp: get. Property> etc. © Copyright UTS Faculty of Information Technology 2008 – JSP 19
Standard Actions Faculty of Information Technology <jsp: forward page="My. Other. Servlet" /> <jsp: include page="My. Included. Servlet" /> <jsp: param name=“var” value=“something”/> <jsp: use. Bean id="myperson" class="au. edu. uts. it. Person. Bean" scope="session" /> • Uses XML syntax. • Actions are run-time behaviours – "jsp: include" includes a page at run-time – “jsp: param” adds parameters to the included page © Copyright UTS Faculty of Information Technology 2008 – JSP 20
Expression Language Faculty of Information Technology ${object} • Expression Language ( EL) introduced with JSP 2. 4 & J 2 EE 1. 4 • It complements JSP Expressions with simpler syntax. Each ${object } would be same as <%= object %> • Supports: – JSP implicit objects – Java. Beans – simple calculations © Copyright UTS Faculty of Information Technology 2008 – JSP 21
Expression Language Object param Faculty of Information Technology example ${param. newcol } header Description Request params & values HTTP headers cookie Cookie names & value ${cookie[“id”] } init. Param Init params in web. xml ${init. Param. name } ${header. Host } page. Context Use this to get ${page. Context. request, response, remote. Host } session • Use page. Context to get servlet parameter. eg: the example is same as <%= request. get. Remote. Host() %> © Copyright UTS Faculty of Information Technology 2008 – JSP 22
EL operators Faculty of Information Technology • EL also has some common programming operators ie: – – – Math: + - * / div % mod relational: == eq != ne < lt > gt <= le >= ge negative: logical: && and ||or ! not empty: empty (used to test for null or “”) • You can also access Java collections using array syntax eg: – ${myarray [1]} © Copyright UTS Faculty of Information Technology 2008 – JSP 23
JSP Execution • • In practice, JSPs are converted into servlets for execution Conversion involves: 1. 2. 3. 4. • Faculty of Information Technology Container generates Java source code Container invokes Java compiler Class file is executed as with a normal servlet A copy of the class file is kept (cached) to avoid unnecessary recompilation in future See for yourself! – weblogic specific: /home/chw/weblogic/Admin. Server/tmp/_WL_user/_app sdir_labs_war/fr 8116/jsp_servlet/__jspfilename. java) !!! © Copyright UTS Faculty of Information Technology 2008 – JSP 24
Topics Faculty of Information Technology • JSP architecture • JSP syntax • • • objects directives scripting elements standard actions execution JSP and Java. Beans Tag libraries JSP deployment Model 1 and Model 2 JSP architectures © Copyright UTS Faculty of Information Technology 2008 – JSP 25
Page View with Bean Faculty of Information Technology • Refinement Request JSP Response Java utility Bean class © Copyright UTS Faculty of Information Technology 2008 – JSP Business Processing 26
Using Java. Beans Faculty of Information Technology • Java. Beans are a client-side component model – not to be confused with Enterprise Java. Beans • Allows you to encapsulate reusable functionality in a "bean" – a Bean is a normal Java class that follows some rules – called "design patterns" • One basic design pattern is for "properties" – using a getter and/or a setter method – more complex patterns too – see a Java. Beans tutorial © Copyright UTS Faculty of Information Technology 2008 – JSP 27
Using Java. Beans Faculty of Information Technology public class Person { private String yourname; public String get. Name() { return yourname; } public void set. Name (String yourname) { this. yourname = yourname; } Person +name other. Method() public void other. Method () { //. . . } } • Note: method naming convention is important © Copyright UTS Faculty of Information Technology 2008 – JSP 28
Using Java. Beans Faculty of Information Technology • Using beans – <jsp: use. Bean id=“fred” class=“com. myapp. Person” scope=“page” /> • com. myapp. Person fred = new com. myapp. Person() • Setting properties – <jsp: set. Property name=“fred” property=“name” value=“Fred Jones” /> • fred. set. Name(“Fred Jones”) • Getting properties – <jsp: get. Property name=“fred” property=“name” /> • fred. get. Name() © Copyright UTS Faculty of Information Technology 2008 – JSP 29
JSP Scopes Faculty of Information Technology • JSP allows you to save data into a scope • Scopes can be one of: Scope Where saved page Only for the currentpage, destroyed when you leave the page. request kept while request is active. session saved in the current session. *** application saved for the whole application © Copyright UTS Faculty of Information Technology 2008 – JSP 30
EL and Java. Beans Faculty of Information Technology • Expression Language also can access Java. Beans • Instead of: <jsp: use. Bean id="fred" class="com. myapp. Person" /> <jsp: get. Property name=“fred” property=“name” /> • [note: this invokes fred. get. Name() ] • use: ${fred. name} instead © Copyright UTS Faculty of Information Technology 2008 – JSP 31
Topics Faculty of Information Technology • JSP architecture • JSP syntax • • • objects directives scripting elements standard actions execution JSP and Java. Beans Tag libraries JSP deployment Model 1 and Model 2 JSP architectures © Copyright UTS Faculty of Information Technology 2008 – JSP 32
Tag libraries Faculty of Information Technology • Create your own custom tags that can be used in documents – encapsulate reusable functionality – simplify page coding – Apache Jakarta project has large range of taglibs http: //jakarta. apache. org/taglibs – J 2 EE 1. 4 adopted some of these as the Java Standard Tag Library (JSTL) http: //java. sun. com/products/jsp/jstl <%@ taglib uri=“/hello” prefix=“examples” /> <html> <p>Hello message is: <examples: hello /> </html> © Copyright UTS Faculty of Information Technology 2008 – JSP 33
Tag libraries • Faculty of Information Technology Four things needed: 1. in JSP file: <%@ taglib uri=“/hello” prefix=“examples” /> <html> <p>Hello message is: <examples: hello /> </html> 2. Update web. xml with the taglib declaration <web-app> <taglib-uri>/hello</taglib-uri> <taglib-location>/WEB-INF/hellotags. tld</taglib-location> </taglib> </web-app> © Copyright UTS Faculty of Information Technology 2008 – JSP 34
Tag libraries • Faculty of Information Technology Four things needed (continued): 3. Create a Tag Library Descriptor (TLD) file in WEB-INF directory: (hellotags. tld) <taglib> <tag> <name>hello</name> <tagclass>Hello. Tag</tagclass> </taglib> 4. Create Java class that implements the tag handler public class Hello. Tag extends Tag. Support { public int do. Start. Tag() throws Jsp. Tag. Exception {. . . } } © Copyright UTS Faculty of Information Technology 2008 – JSP 35
Topics Faculty of Information Technology • JSP architecture • JSP syntax • • • objects directives scripting elements standard actions execution JSP and Java. Beans Tag libraries JSP deployment Model 1 and Model 2 JSP architectures © Copyright UTS Faculty of Information Technology 2008 – JSP 36
JSP deployment Faculty of Information Technology • For a simple JSP, just include it in a WAR file, no need to put anything in web. xml file • If you want to call your JSP by a different name, you can map it in web. xml, e. g. <servlet> <servlet-name>My. First. JSP</servlet-name> <jsp-file>foobar. jsp</jsp-file> </servlet> <servlet-mapping> <servlet-name>My. First. JSP</servlet-name> <url-pattern>/foobar/*</url-pattern> </servlet-mapping> © Copyright UTS Faculty of Information Technology 2008 – JSP 37
JSP deployment – tag libraries Faculty of Information Technology • For tag libraries, need to specify in web. xml: <web-app> <taglib-uri>/hello</taglib-uri> <taglib-location>/WEB-INF/tlds/hellotags. tld</tagliblocation> </taglib> </web-app> © Copyright UTS Faculty of Information Technology 2008 – JSP 38
WAR file directory structure Faculty of Information Technology Same as servlets: – Put JSP same place as html/gif etc, ie: top level directory or subdirectory – *NOT* in WEB-INF Not same as servlets: • WEB-INF/classes – Java classes required to implement tag libs or supporting classes • WEB-INF/lib – JAR files containing additional classes for tag libs • WEB-INF/tlds – contains TLDs (not mandatory though) - conventional – not tag handler classes © Copyright UTS Faculty of Information Technology 2008 – JSP 39
JSTL Faculty of Information Technology • J 2 EE 1. 4 provides Java Standard Tag Library (JSTL) – <%@ taglib prefix=“c” uri=“http: //java. sun. com/jsp/jstl/core” %> • Core tags: – – – <c: set> <c: remove> set/remove attributes from JSP <c: catch> catch Java errors! <c: for. Each> equivalent of Java for loop <c: for. Tokens> iterate over tokens in string <c: if> equivalent to Java if but no else ! <c: choose><c: when> <c: otherwise> equivalent to Java switch – <c: out value=“${x}”> like ${x} but escapes XML chars © Copyright UTS Faculty of Information Technology 2008 – JSP 40
JSTL – more libraries Faculty of Information Technology • More JSTL tag libraries: just use @taglib with the following parameters: Function prefix URL http: //java. sun. com/jsp/. . . XML processing x xml Internationalisation, fmt formatting fmt SQL access sql String, Collection functions fn functions © Copyright UTS Faculty of Information Technology 2008 – JSP 41
JSTL example • Scriptlet based <ul> <% for (int i=0; i< arr. length; i++) { String msg = arr[i]; %> <li><%= msg %></li> <% } %> </ul> Faculty of Information Technology • JSTL based <ul> <c: for. Each var=“msg” items=“${arr}”> <li>${msg}<li> </c: for. Each> • No java code needed!! • This also works if arr[] was a java collection! © Copyright UTS Faculty of Information Technology 2008 – JSP 42
Topics Faculty of Information Technology • JSP architecture • JSP syntax • • • objects directives scripting elements standard actions execution JSP and Java. Beans Tag libraries JSP deployment Model 1 and Model 2 JSP architectures © Copyright UTS Faculty of Information Technology 2008 – JSP 43
Web application architectures Faculty of Information Technology • In theory, you can use servlets and JSPs interchangeably • However current best practice is to design the web part of your application according to a particular architecture – Model 1 architecture – Model 2 architecture © Copyright UTS Faculty of Information Technology 2008 – JSP 44
JSP Model 1 Architecture Faculty of Information Technology 1 HTTP JSP 4 Browser 2 Java. Beans Client Web Server © Copyright UTS Faculty of Information Technology 2008 – JSP 3 Data Source(s) 45
JSP Model 1 Arch (cont'd) Faculty of Information Technology 1. Browser makes HTTP request to JSP performs control logic 2. JSP may make use of Java. Beans for accessing either state information or database data 3. Java. Bean may retrieve its data from an external data source (optional) 4. JSP finishes execution and returns result to browser © Copyright UTS Faculty of Information Technology 2008 – JSP 46
Limitations of Model 1 • Faculty of Information Technology Model 1 applications have no structured way of keeping track of "what to do next" – JSPs can call each other in random patterns • Model 1 applications also have no separation between "design" and "coding" functions – typically these are performed by different people • • Model 1 is okay for simple applications For larger applications it becomes hard to keep track of JSP interdependences © Copyright UTS Faculty of Information Technology 2008 – JSP 47
Model-View-Controller Faculty of Information Technology • Model-View-Controller (MVC) – a design pattern for building applications that interact with a user (not web-specific, not Java-specific) • Model – code for internal representation of system state (data) • View – code for displaying information to user (GUI) • Controller – code for changing system state (logic) © Copyright UTS Faculty of Information Technology 2008 – JSP 48
JSP Model 2 Architecture Faculty of Information Technology 1 n ti a 2 te 4 Java. Beans JSP 5 (View) Client ta Browser (Controller) s 6 Servlet in HTTP (Model) Web Server 3 Data Source(s) • Model 2 = MVC for web applications © Copyright UTS Faculty of Information Technology 2008 – JSP 49
JSP Model 2 Arch (cont'd) Faculty of Information Technology 1. Browser makes HTTP request to controller servlet 2. Controller servlet instantiates any Java. Beans that might be needed for data displayed to user 3. Java. Bean may retrieve external data (parallel to step 4) 4. Controller forwards request to appropriate JSP only contains presentation/view code 5. JSP accesses Java. Bean data set up by controller 6. Execution finishes and result is returned to browser © Copyright UTS Faculty of Information Technology 2008 – JSP 50
Advantages of Model 2 • • Faculty of Information Technology Introducing a "controller" provides more structure to the application Also provides clearer separation of "design" and "coding" – web designers create JSPs without much coding – programmers write the controller servlet without being concerned by design issues • A large application may contain many controllers – e. g. one for each use case / piece of functionality © Copyright UTS Faculty of Information Technology 2008 – JSP 51
Model 2 Architecture Summary Faculty of Information Technology • Building web applications using the Model 2 architecture is current best practice • We'll revisit Model 2 again when we consider EJBs – EJBs change the picture, but only slightly • If you design a three-tier application using Model 2, migrating it to an N-tier application (with EJBs) should involve minimal code changes – changes only to the Java. Beans, in theory © Copyright UTS Faculty of Information Technology 2008 – JSP 52
Model 2 Implementations • • • Faculty of Information Technology The Struts framework http: //struts. apache. org/ is a common implementation of MVC architecture. Consists of a controller servlet + config file + JSP taglib You write View(JSP), Action and Model classes © Copyright UTS Faculty of Information Technology 2008 – JSP 53
Summary Faculty of Information Technology • JSP has many similarities to servlets • Again a fair amount of information to know about – use the documentation as a reference • Many ways to modularise and simplify coding – include directive – forward and include actions – tag libraries, especially Apache Jakata & JSTL • Tag libraries complex, but useful for big projects • Model 2 is best practice application architecture © Copyright UTS Faculty of Information Technology 2008 – JSP 54
- Advanced internet programming
- Chapter 1 information technology the internet and you
- Chapter 1 information technology the internet and you
- Hijjawi faculty for engineering technology
- Unt faculty information system
- Advanced dynamic programming
- Forward reference table(frt) is arranged like
- Imperative statement in assembly language
- Advanced programming in java
- Internet tools for advanced nursing practice
- Search images on google
- Operators for advanced internet legal research
- Ah geography understanding standards
- Aesthetic advanced technology
- Advanced technology microwave sounder
- Advanced science and technology letters
- Technology operating modelsê
- Advanced refrigeration technology
- Advanced processor technology
- Advanced technology microwave sounder
- Advanced technology microwave sounder
- Autocaat
- Apex advanced technology
- Advanced science and technology letters
- Advanced processor technology
- Advanced performance technology
- Perbedaan linear programming dan integer programming
- Greedy vs dynamic programming
- Definition of system programming
- Linear vs integer programming
- Definisi integer
- What is internet
- Unit 4 crime and punishment
- Wjec unit 4 criminology
- Advanced digital information corporation
- Telecommunications, the internet, and wireless technology
- What are wireless devices and the wireless revolution
- Strategy and the internet
- Internet technology
- Internet technology wikipedia
- Which internet service is used for information retrieval
- Iis internet information services
- Php to asp.net
- Materials technology positive impacts
- Negative effects of technology on environment
- Volvo information technology ab
- User acceptance of information technology
- Ict conclusion
- Similarities of media literacy and technology literacy
- Northwestern mailroom
- 5 disadvantages of information technology
- Discuss about information technology and e-business
- Positive impacts of ict
- Information technology history