Introduction to ServerSide Web Development Session I Introduction
Introduction to Server-Side Web Development Session I: Introduction to Server-Side Web Development with Servlets and JSP; basic concepts and syntax 19 th February 2004 Bogdan L. Vrusias 19 th February 2004 b. vrusias@surrey. ac. uk Bogdan L. Vrusias © 2004
Introduction to Server-Side Web Development Introduction • Most web sites want to display dynamic content based on user’s requests and expectations. • Most contents, such as images, text, and banner ads, are easiest to build with HTML editors. • So we need to mix the "static" content of HTML files with "directives" for accessing or generating dynamic content. • Introducing: 19 th February 2004 Bogdan L. Vrusias © 2004 2
Introduction to Server-Side Web Development Session I • • History of JSP Fundamentals of server-side programming. Comparison to other server-side languages Fundamentals of Java. Server Pages / Servlets. More Servlets JSP Syntax Java. Beans 19 th February 2004 Bogdan L. Vrusias © 2004 3
Introduction to Server-Side Web Development Definition • JSP provide server-side scripting support for generating web pages with combined static and dynamic content. • JSP is a fast way to serve web pages that display dynamically generated content. 19 th February 2004 Bogdan L. Vrusias © 2004 4
Introduction to Server-Side Web Development History: The Beginning • 1991… beginning of WWW… beginning of HTML • No dynamic pages… the Webmaster • Too many static HTML pages had to be created for each website. • Need for a change 19 th February 2004 Bogdan L. Vrusias © 2004 5
Introduction to Server-Side Web Development History: First approach - CGI • Solution… Common Gateway Interface technology created. • Dynamic pages…using Perl and C • Problem… these languages are not the simplest • Need for a change 19 th February 2004 Bogdan L. Vrusias © 2004 6
Introduction to Server-Side Web Development History: Servlets and JSP • Java had the answer… Servlets • Servlets are pure Java objects that generate HTML by writing it to a stream. • The files do not contain any HTML tags; they simply contain Java code which write out HTML (as strings) to produce the page that is sent to the user's browser. • Historically, this used to be the only way to generate dynamic content in Java, but it is quite painful to develop any large scale application with a bunch of servlets running around doing println() statements. • This is why Sun invented JSPs in the first place: to compete with the ease-of-development of Microsoft's ASP files, which - you guessed it could combine HTML tags and code in a single file. 19 th February 2004 Bogdan L. Vrusias © 2004 7
Introduction to Server-Side Web Development A dynamic Web Page • Static web page is a page whose content consists of some HTML that was predetermined and was typed directly into a text editor and saved as. htm or. html file. • Dynamic web page is a page whose content is generated at runtime dynamically. 1. Author writes instructions 5. HTML stream returned to browser 6. Browser processes HTML and displays page. 3. Web server locates instructions file 4. Web server processes instructions to create HTML 19 th February 2004 2. Client request web page Bogdan L. Vrusias © 2004 8
Introduction to Server-Side Web Development Requests and Responses 19 th February 2004 Bogdan L. Vrusias © 2004 9
Introduction to Server-Side Web Development Other Server-Side programming languages • CGI – Perl, C, C++ • Cold. Fusion – Tags with encapsulated functionality • ASP (Active Server Pages) and ASP. NET – VBasic, Java. Script – ADO • PHP (Personal Home Pages) – Syntax similar to C and Perl – Open-source, cross-platform 19 th February 2004 Bogdan L. Vrusias © 2004 10
Introduction to Server-Side Web Development JSP Vs ASP • Similarities – Create interactive pages with web-based applications – Separates programming logic from page design through the use of component technology – Faster and Easier alternative to older, more cryptic technologies such as CGI • Differences – JSP: Platform and Server Independent • JSP can run on any web server • Supported by large number of tools – ASP: Relies on Microsoft Platforms and Servers • Restricted to MS Windows based platforms because of Active. X Controls • Can be ported to different platforms using third party products, Active. X must be present on the platform 19 th February 2004 Bogdan L. Vrusias © 2004 11
Introduction to Server-Side Web Development Why Use JSPs? • Write once, run anywhere. – JSPs are entirely platform independent, both in dynamic Web pages and underlying server components. • Emphasize components. – JSPs encourage the use of reusable, cross-platform server components called Java. Beans. Therefore saves development time, while giving you the power and flexibility of the Java programming language. • Provide a front door to the Enterprise’s Java platform. – JSPs are an integral part of the Java Platform for the Enterprise, which brings Java technology to enterprise computing. You can develop powerful enterprise-wide or middle-tier server applications, using a JSP web site as a front end. 19 th February 2004 Bogdan L. Vrusias © 2004 12
Introduction to Server-Side Web Development Advantages of Java • More mature, powerful, and scalable than Basic-based scripting languages • Java helps developers protect against system crashes, ASP and NT systems are susceptible to crashing • Java helps with memory management by providing protection against memory leaks 19 th February 2004 Bogdan L. Vrusias © 2004 13
Introduction to Server-Side Web Development JSP Models Model 1 19 th February 2004 Model 2 Bogdan L. Vrusias © 2004 14
Introduction to Server-Side Web Development Servlets Characteristics • Security – Servlets are called within the server context so they have all the security of the server itself. – They’re hidden from view and transmission. • Robustness – Rich application programming interfaces, APIs, are available to Java programs making it easier to build sophisticated applications in a minimal amount of time (JDBC, EJB, etc. ). • Performance – Servlets run in the same context as the application server and can be preloaded or loaded on demand. – They’re multi-threaded to scale with multiprocessors and heterogeneous systems • Portability – Write-once, run-anywhere capability – They are able to exploit re-useable Java components called Java. Beans. 19 th February 2004 Bogdan L. Vrusias © 2004 15
Introduction to Server-Side Web Development How Servlets relate to JSPs • JSPs and servlets are two different ways to accomplish the same goal: generating dynamic HTML pages using Java code. One puts Java code in your HTML, and one puts HTML in your Java code. • Functionally, they are equivalent. In fact, under the covers, the web server takes a JSP and converts it to the corresponding servlet and dynamically compiles it. 19 th February 2004 Bogdan L. Vrusias © 2004 16
Introduction to Server-Side Web Development Servlet Code Sample import javax. servlet. *; import java. io. *; import java. util. *; public class Simple. Servlet extends Generic. Servlet { public void service(Servlet. Request request, Servlet. Response response) throws Servlet. Exception, IOException { response. set. Content. Type("text/html"); print. Writer pw = response. get. Writer(); pw. println("<HTML>"); pw. println("<BODY>"); for(int counter = 1; counter <= 10; counter++) { pw. println(counter + " Mississippi, <BR>"); } pw. println("<B>Ready or not, here I come!</B>"); pw. println("</BODY>"); pw. println("</HTML>"); pw. close(); } } 19 th February 2004 Bogdan L. Vrusias © 2004 17
Introduction to Server-Side Web Development JSP Code Sample <%@ page language="java" %> <HTML> <BODY> <% for (int counter = 1; counter <= 10; counter++) {%> <%= counter %> Mississippi, <BR> <% }%> <B>Ready or not, here I come!</B> </BODY> </HTML> 19 th February 2004 Bogdan L. Vrusias © 2004 18
Introduction to Server-Side Web Development JSP Syntax • • • Directives Declarations Scriptlets Expressions Comments 19 th February 2004 Bogdan L. Vrusias © 2004 19
Introduction to Server-Side Web Development Directives • Here is what directives look like in a general form: <%@ directive attribute="some. Value" attribute="another. Value". . . %> • There are three directives: <%@ page. . . %> specifies information that affects the page e. g. <%@ page import="java. util. Date, java. text. *" %> <%@ include. . . %> includes a file at the location of the include directive (parsed) e. g. <%@ include file="/copyright. html" %> <%@ taglib. . . %> allows the use of custom tags in the page e. g. <%@ taglib uri="/WEB-INF/tlds/my. Tag. Lib. tld" prefix="my. Prefix" %> <my. Prefix: display. Image file="logo. jpg" /> 19 th February 2004 Bogdan L. Vrusias © 2004 20
Introduction to Server-Side Web Development Declarations <%! // this integer can be used anywhere in this JSP page private int my. Variable = -1; // this function can be called from anywhere in this JSP page public boolean is. Positive() { return ( my. Variable > 0 ); } %> 19 th February 2004 Bogdan L. Vrusias © 2004 21
Introduction to Server-Side Web Development Scriptlets <% // Java Code %> 19 th February 2004 Bogdan L. Vrusias © 2004 22
Introduction to Server-Side Web Development Expressions <%= counter %> 19 th February 2004 Bogdan L. Vrusias © 2004 23
Introduction to Server-Side Web Development Comments <%-- comment for server side only --%> 19 th February 2004 Bogdan L. Vrusias © 2004 24
Introduction to Server-Side Web Development Action Elements • There are three different types of action elements: – Standard • • <jsp: include> <jsp: use. Bean> <jsp: get. Property> and <jsp: set. Property> <jsp: forward> – Custom • Tags constructed by the developer. – JSTL (Java. Server Pages Standard Tag Library) • It provides tag libraries in the subject of: – Internationalization and formatting, XML, SQL, and the core tags. 19 th February 2004 Bogdan L. Vrusias © 2004 25
Introduction to Server-Side Web Development Java. Beans • Java. Beans is a portable (platform-independent) component model written in Java and was developed in collaboration with industry leaders. • Java. Beans components are Java classes that can be easily reused and composed together into applications. • Any Java class that follows certain design conventions can be a Java. Beans component. • Java. Beans will minimize the code on the JSP. 19 th February 2004 Bogdan L. Vrusias © 2004 26
Introduction to Server-Side Web Development Java. Beans Basics • Java. Server Pages technology directly supports using Java. Beans components with JSP language elements. You can easily create and initialise beans and get and set the values of their properties. • There is no requirement such that a property must be implemented by an instance variable; the property must simply be accessible using public methods that conform to certain conventions: – For each readable property, the bean must have a method of the form Property. Class get. Property() {. . . } – For each writable property, the bean must have a method of the form set. Property(Property. Class pc) {. . . } • In addition to the property methods, a Java. Beans component must define a constructor that takes no parameters. 19 th February 2004 Bogdan L. Vrusias © 2004 27
Introduction to Server-Side Web Development Java. Beans Syntax • You declare that your JSP page will use a Java. Beans component using either one of the following formats: – <jsp: use. Bean id="bean. Name" class="classname" scope="scope"/> – <jsp: use. Bean id="bean. Name" class="classname" scope="scope"> <jsp: set. Property. . . /> </jsp: use. Bean> • The second format is used when you want to include jsp: set. Property statements, for initialising bean properties. 19 th February 2004 Bogdan L. Vrusias © 2004 28
Introduction to Server-Side Web Development Java. Beans Syntax • To retrieve a Java. Beans component property you can use either one of the following formats: – <jsp: get. Property name="bean. Name" property="prop. Name"/> – <%= bean. Name. get. Prop. Name() %> • bean. Name must be the same as that specified for the id attribute in a use. Bean element, and there must be a get. Prop. Name method in the Java. Beans component. 19 th February 2004 Bogdan L. Vrusias © 2004 29
Introduction to Server-Side Web Development JSP Sessions I • A session can be defined as a series of related interactions between a single client and the server, which take place over a period of time. • A session object can be used for storing and retrieving information. • Every time the client accesses the resources on the server, the client provides the session ID that was assigned by the server. • A session has a one-to-one association between a client and the server. 19 th February 2004 Bogdan L. Vrusias © 2004 30
Introduction to Server-Side Web Development JSP Sessions II • Session tracking is a technique for maintaining user information across pages: – HTTP information (not used much… privacy issues) – Hidden fields (very popular… but again privacy issues) <input type=“hidden” name=“my. Key” value=“my. Value” > – Extended Path information and URL-rewriting ( privacy issues) <a href=“/my. Page. jsp? login=james&item=book_1”> Next</a> – Cookies (data can be encrypted) Cookie uid = new Cookie(“uid”, “ 234 ff 543333 c”); response. add. Cookie(uid); – Session (data can be encrypted) session. put. Value("user_id", user_id); session. get. Value("user_id") 19 th February 2004 Bogdan L. Vrusias © 2004 31
Introduction to Server-Side Web Development Session I: Closing • • Questions? ? ? Remarks? ? ? Comments!!! Evaluation! 19 th February 2004 Bogdan L. Vrusias © 2004 32
- Slides: 32