J2EE Servlets and JSP Advanced topics Presented by Bartosz Sakowicz.

Slides:



Advertisements
Similar presentations
Java Server Pages (JSP)
Advertisements

JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
DT211/3 Internet Application Development
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
DT228/3 Web Development JSP: Directives and Scripting elements.
Java Server Pages Russell Beale. What are Java Server Pages? Separates content from presentation Good to use when lots of HTML to be presented to user,
MVC Architecture1 Usage Rules PowerPoint slides for use only in for-credit courses at degree-granting institutions Slides can be modified.
Integrating Servlets and JavaServer Pages Vijayan Sugumaran School of Business Administration Oakland University Parts of this presentation provided by.
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
Java Servlets. What Are Servlets? Basically, a java program that runs on the server Basically, a java program that runs on the server Creates dynamic.
Struts 2.0 an Overview ( )
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
Netbeans – jsp.zip Introduction to JSP Netbeans – jsp.zip.
Servlets Pranav Maydeo. What is a Servlet ? Servlets are modules of Java code that run in a server application to answer client requests. Servlets are.
Introduction to JSP Based on: Marty Hall, Larry Brown, Core Servlets and JavaServer Pages.
What Are Beans? beans are simply Java classes that are written in a standard format. A bean class must have a zero-argument (default) constructor. A bean.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Li Tak Sing COMPS311F. Static attributes in Servlets Since Servlets are also Java classes, you can also use static attributes to store values that can.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
JSP Architecture Outline  Model 1 Architecture  Model 2 Architecture.
CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Chapter 2.
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Slides © Marty Hall, book © Sun Microsystems Press 1 JSP Scripting Elements Core Servlets & JSP book:
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
CSC 2720 Building Web Applications Frameworks for Building Web Applications.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
 Obtaining a RequestDispatcher Forwarding requests from servlets to dynamic resources  Forwarding requests from servlets to static resources  Using.
Chapter 11 Invoking Java Code with JSP Scripting Elements.
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
JSP. Types of JSP Scripting Elements Expressions of the form, which are evaluated and inserted into the servlet's output. Scriptlets of the form, which.
Web Technologies Java Beans & JSP By Praveen Kumar G.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
©SoftMoore ConsultingSlide 1 Filters. Filters can be used in a web application to intercept, examine, and possibly transform requests or responses associated.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
® IBM Software Group © 2007 IBM Corporation Servlet API (Part II)
Chapter 3 JSP Overview. The Problem with Servlets processing the request and generating the response are both handled by a single servlet class Java programming.
Servlets 4 Lec 30 Web Design and Development. Looking Back… Response Redirection  Sending a standard redirect  Sending a redirect to an error page Request.
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
Java Servlets and Java Server Pages
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
INVOKING JAVA CODE WITH JSP SCRIPTING ELEMENTS. Creating Template Text A large percentage of your JSP document consists of static text (usually HTML),
JAVA SERVER PAGES -by Rubeena Memon Deepti Jain Jaya Thakar Jisha Vettuventra.
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
1 Servlets – Part 2 Representation and Management of Data on the Web.
Java Server Pages. 2 Servlets The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Chapter 14 Using JavaBeans Components in JSP Documents.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
JSP Java Server Pages. Hello, !
Enterprise Java v050228MVC1 Model, View, Controller Web Architecture.
Handling Errors in Web Applications
Developing JavaServer Pages
Java Servlets.
Scripted Page Web App Development (Java Server Pages)
Engineering the Java Web Application (MVC)
Pre-assessment Questions
Introduction to JSP Dept. of B.Voc Software Development and System Administration St. Joseph’s College(Autonomous) Trichy-02 By Dr. J. Ronald Martin Introduction.
Presentation transcript:

J2EE Servlets and JSP Advanced topics Presented by Bartosz Sakowicz

JSP and servlets integration Advantages and disadvantages of servlets and JSP: Servlets are good when your application requires a lot of real programming. Generating HTML with servlets can be tedious and can yield a result that is hard to modify. JSP document provides a single overall presentation. Beans and custom tags, although very flexible, don’t overcome the limitation that the JSP page defines a relatively fixed top-level page appearance. The solution is to use both servlets and JSP. If you have a complicated application that may require several substantially different presentations, a servlet can handle the initial request, partially process the data, set up beans, then forward the results to one of a number of different JSP pages, depending on the circumstances. Presented by Bartosz Sakowicz DMCS TUL

RequestDispatcher class To let servlets forward requests or include external content you use a RequestDispatcher. You obtain a RequestDispatcher by calling the getRequestDispatcher method of ServletContext, supplying a URL relativeto the server root. Example: String url = "/presentations/presentation1.jsp"; RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url); Once you have a RequestDispatcher, you use forward to completely transfer control to the associated URL and use include to output the associated URL’s content. Presented by Bartosz Sakowicz DMCS TUL

Forwarding example public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String operation = request.getParameter("operation"); if (operation == null) { operation = "unknown"; } if (operation.equals("operation1")) { gotoPage("/operations/presentation1.jsp", request, response); } else if (operation.equals("operation2")) { gotoPage("/operations/presentation2.jsp", request, response); } else { gotoPage("/operations/unknownRequestHandler.jsp", request, response); } Presented by Bartosz Sakowicz DMCS TUL

Forwarding example(2) private void gotoPage(String address, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address); dispatcher.forward(request, response); } Presented by Bartosz Sakowicz DMCS TUL

Supplying Data to the destination page Main reasons why the destination page shouldn't look up and process all the data itself: Complicated programming is easier in a servlet than in a JSP page. Multiple JSP pages may require the same data, so it would be wasteful for each JSP page to have to set up the same data. A better approach is for the original servlet to set up the information that the destination pages need, then store it somewhere that the destination pages can easily access. There are two main places for the servlet to store the data that the JSP pages will use: in the HttpServletRequest and as a bean in the location specific to the scope attribute of jsp:useBean Presented by Bartosz Sakowicz DMCS TUL

Supplying... (2) The originating servlet would store arbitrary objects in the HttpServletRequest by using: request.setAttribute("key1", value1); The destination page would access the value by using a JSP scripting element to call: Type1 value1 = (Type1)request.getAttribute("key1"); Presented by Bartosz Sakowicz DMCS TUL

Supplying... (3) For complex values a better approach is to represent the value as a bean and store it in the location used by jsp:useBean for shared beans. To make a bean accessible to all servlets or JSP pages in the server or Web application, the originating servlet would do the following: Type1 value1 = computeValueFromRequest(request); getServletContext().setAttribute("key1", value1); The destination JSP page would normally access the previously stored value by using jsp:useBean as follows: Alternatively, the destination page could use a scripting element to explicitly call application.getAttribute("key1") and cast the result to Type1. Presented by Bartosz Sakowicz DMCS TUL

Supplying... (4) The Servlet 2.2 specification adds a third way to send data to the destination page when using GET requests: append the query data to the URL. Example: String address = "/path/resource.jsp?newParam=value"; RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address); dispatcher.forward(request, response); Presented by Bartosz Sakowicz DMCS TUL

Differences between forwarding and sendRedirect sendRedirect requires the client to reconnect to the new resource, whereas the forward method of RequestDispatcher is handled completely on the server. sendRedirect does not automatically preserve all of the request data; forward does. sendRedirect results in a different final URL whereas with forward the URL of the original servlet is maintained. Presented by Bartosz Sakowicz DMCS TUL

Interpreting relative URL in the destination page If the destination page uses relative URLs for images or style sheets, it needs to make them relative to the server root, not to the destination page’s actual location. Example: If the JSP page containing this entry is accessed by means of a forwarded request, mystyles.css will be interpreted relative to the URL of the originating servlet, not relative to the JSP page itself, almost certainly resulting in an error. The solution is to give the full server path to the style sheet file: The same approach is required for addresses used in and. Presented by Bartosz Sakowicz DMCS TUL

Including static or dynamic content If the servlet wants to generate some of the content itself but use a JSP page or static HTML document for other parts of the result, the servlet can use the include method of RequestDispatcher. The process is very similar to that for forwarding requests: call the getRequestDispatcher method of ServletContext with an address relative to the server root, then call include with the HttpServletRequest and HttpServletResponse. Presented by Bartosz Sakowicz DMCS TUL

Including static or dynamic content (3) include does one thing that forward does not: it automatically sets up attributes in the HttpServletRequest object that describe the original request path in case the included servlet or JSP page needs that information. These attributes, available to the included resource by calling getAttribute on the HttpServletRequest, are: javax.servlet.include.request_uri javax.servlet.include.context_path javax.servlet.include.servlet_path javax.servlet.include.path_info javax.servlet.include.query_string Presented by Bartosz Sakowicz DMCS TUL

Differences between include directive and include method In the JSP include directive the actual source code of JSP files was included in the page The include method of RequestDispatcher just includes the result of the specified resource. The jsp:include action has behavior similar to that of the include method, except that jsp:include is available only from JSP pages, not from servlets. Presented by Bartosz Sakowicz DMCS TUL

Forwarding requests from JSP pages In JSP, the jsp:forward action is simpler and easier to use than wrapping up RequestDispatcher code in a scriptlet. This action takes the following form: The page attribute is allowed to contain JSP expressions so that the destination can be computed at request time. Example: <% String destination; if (Math.random() > 0.5) { destination = "/examples/page1.jsp"; } else { destination = "/examples/page2.jsp"; } %> " /> Presented by Bartosz Sakowicz DMCS TUL