Download presentation
Presentation is loading. Please wait.
Published byLucinda Farmer Modified over 8 years ago
1
Introduction to Server-Side Web Development 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 b.vrusias@surrey.ac.uk
2
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 20042 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:
3
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 20043 Session I History of JSP Fundamentals of server-side programming. Comparison to other server-side languages Fundamentals of JavaServer Pages / Servlets. More Servlets JSP Syntax JavaBeans
4
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 20044 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.
5
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 20045 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
6
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 20046 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
7
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 20047 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.
8
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 20048 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. 6. Browser processes HTML and displays page. 2. Client request web page 1. Author writes instructions 3. Web server locates instructions file 4. Web server processes instructions to create HTML 5. HTML stream returned to browser
9
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 20049 Requests and Responses
10
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200410 Other Server-Side programming languages CGI –Perl, C, C++ ColdFusion –Tags with encapsulated functionality ASP (Active Server Pages) and ASP.NET –VBasic, JavaScript –ADO PHP (Personal Home Pages) –Syntax similar to C and Perl –Open-source, cross-platform
11
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200411 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 ActiveX Controls Can be ported to different platforms using third party products, ActiveX must be present on the platform
12
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200412 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 JavaBeans. 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.
13
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200413 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
14
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200414 JSP Models Model 1 Model 2
15
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200415 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 JavaBeans.
16
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200416 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.
17
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200417 Servlet Code Sample import javax.servlet.*; import java.io.*; import java.util.*; public class SimpleServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); printWriter pw = response.getWriter(); pw.println(" "); for(int counter = 1; counter <= 10; counter++) { pw.println(counter + " Mississippi, "); } pw.println(" Ready or not, here I come! "); pw.println(" "); pw.close(); }
18
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200418 JSP Code Sample Mississippi, Ready or not, here I come!
19
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200419 JSP Syntax Directives Declarations Scriptlets Expressions Comments
20
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200420 Directives Here is what directives look like in a general form: There are three directives: specifies information that affects the page e.g. includes a file at the location of the include directive (parsed) e.g. allows the use of custom tags in the page e.g.
21
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200421 Declarations <%! // this integer can be used anywhere in this JSP page private int myVariable = -1; // this function can be called from anywhere in this JSP page public boolean isPositive() { return ( myVariable > 0 ); } %>
22
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200422 Scriptlets <% // Java Code %>
23
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200423 Expressions
24
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200424 Comments
25
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200425 Action Elements There are three different types of action elements: –Standard and –Custom Tags constructed by the developer. –JSTL (JavaServer Pages Standard Tag Library) It provides tag libraries in the subject of: –Internationalization and formatting, XML, SQL, and the core tags.
26
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200426 JavaBeans JavaBeans is a portable (platform-independent) component model written in Java and was developed in collaboration with industry leaders. JavaBeans 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 JavaBeans component. JavaBeans will minimize the code on the JSP.
27
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200427 JavaBeans Basics JavaServer Pages technology directly supports using JavaBeans 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 PropertyClass getProperty() {... } –For each writable property, the bean must have a method of the form setProperty(PropertyClass pc) {... } In addition to the property methods, a JavaBeans component must define a constructor that takes no parameters.
28
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200428 JavaBeans Syntax You declare that your JSP page will use a JavaBeans component using either one of the following formats: – The second format is used when you want to include jsp:setProperty statements, for initialising bean properties.
29
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200429 JavaBeans Syntax To retrieve a JavaBeans component property you can use either one of the following formats: – beanName must be the same as that specified for the id attribute in a useBean element, and there must be a getPropName method in the JavaBeans component.
30
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200430 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.
31
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200431 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) –Extended Path information and URL-rewriting ( privacy issues) Next –Cookies (data can be encrypted) Cookie uid = new Cookie(“uid”, “234ff543333c”); response.addCookie(uid); –Session (data can be encrypted) session.putValue("user_id", user_id); session.getValue("user_id")
32
Introduction to Server-Side Web Development 19 th February 2004Bogdan L. Vrusias © 200432 Session I: Closing Questions??? Remarks??? Comments!!! Evaluation!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.