PA165 - Lab session - Web Presentation Layer



Similar documents
Arjun V. Bala Page 20

Java EE Introduction, Content. Component Architecture: Why and How Java EE: Enterprise Java

Managing Data on the World Wide-Web

Creating Java EE Applications and Servlets with IntelliJ IDEA

Visa Checkout September 2015

JSP. Common patterns

Principles and Techniques of DBMS 5 Servlet

Java Servlet Tutorial. Java Servlet Tutorial

An introduction to web programming with Java

Ch-03 Web Applications

INTRODUCTION TO WEB TECHNOLOGY

Web Container Components Servlet JSP Tag Libraries

Penetration from application down to OS

Web Applications. For live Java training, please see training courses at

Web Application Programmer's Guide

Listeners se es. Filters and wrappers. Request dispatchers. Advanced Servlets and JSP

Servlet and JSP Filters

ACI Commerce Gateway Hosted Payment Page Guide

Database Applications Recitation 10. Project 3: CMUQFlix CMUQ s Movies Recommendation System

Servlet 3.0. Alexis Moussine-Pouchkine. mercredi 13 avril 2011

2. Follow the installation directions and install the server on ccc

Aspects of using Hibernate with CaptainCasa Enterprise Client

JSP Java Server Pages

ACM Crossroads Student Magazine The ACM's First Electronic Publication

Controlling Web Application Behavior

We automatically generate the HTML for this as seen below. Provide the above components for the teaser.txt file.

CHAPTER 9: SERVLET AND JSP FILTERS

Developing Web Views for VMware vcenter Orchestrator

Implementing the Shop with EJB

Outline. CS 112 Introduction to Programming. Recap: HTML/CSS/Javascript. Admin. Outline

Configure a SOAScheduler for a composite in SOA Suite 11g. By Robert Baumgartner, Senior Solution Architect ORACLE

Java Server Pages combined with servlets in action. Generals. Java Servlets

CSc31800: Internet Programming, CS-CCNY, Spring 2004 Jinzhong Niu May 9, Java Servlets

Introduction to Web Development

Java and Web. WebWork

2.8. Session management

Developing an EJB3 Application. on WebSphere 6.1. using RAD 7.5

How To Understand The Architecture Of Java 2Ee, J2Ee, And J2E (Java) In A Wordpress Blog Post

Supplement IV.E: Tutorial for Tomcat. For Introduction to Java Programming By Y. Daniel Liang

CONTROLLING WEB APPLICATION BEHAVIOR WITH

Liferay Enterprise ecommerce. Adding ecommerce functionality to Liferay Reading Time: 10 minutes

Drupal CMS for marketing sites

Agenda. Summary of Previous Session. Application Servers G Session 3 - Main Theme Page-Based Application Servers (Part II)

Pure server-side Web Applications with Java, JSP. Application Servers: the Essential Tool of Server-Side Programming. Install and Check Tomcat

Introduction to J2EE Web Technologies

Piotr Nowicki's Homepage. Java EE 6 SCWCD Mock Exam. "Simplicity is the ultimate sophistication." Important!

Model-View-Controller. and. Struts 2

Citrix StoreFront. Customizing the Receiver for Web User Interface Citrix. All rights reserved.

Java EE 6 New features in practice Part 3

Course Information Course Number: IWT 1229 Course Name: Web Development and Design Foundation

Web Development in Java Live Demonstrations (Live demonstrations done using Eclipse for Java EE 4.3 and WildFly 8)

Please send your comments to:

Fast track to HTML & CSS 101 (Web Design)

Java Technologies. Lecture X. Valdas Rapševičius. Vilnius University Faculty of Mathematics and Informatics

Hello World RESTful web service tutorial

Course Name: Course in JSP Course Code: P5

Short notes on webpage programming languages

Web Frameworks and WebWork

Web-JISIS Reference Manual

Java Web Programming. Student Workbook

Java Servlet 3.0. Rajiv Mordani Spec Lead

Developing Web Applications using JavaServer Pages and Servlets

Handling the Client Request: Form Data

Web Programming II JSP (Java Server Pages) ASP request processing. The Problem. The Problem. Enterprise Application Development using J2EE

JJY s Joomla 1.5 Template Design Tutorial:

Hello World Portlet Rendered with JSP for WebSphere Portal Version 4.1

Course Number: IAC-SOFT-WDAD Web Design and Application Development

Advanced Web Development SCOPE OF WEB DEVELOPMENT INDUSTRY

Servlets. Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun

Session Tracking Customized Java EE Training:

Multiple vulnerabilities in Apache Foundation Struts 2 framework. Csaba Barta and László Tóth

Application Security

CSCI110 Exercise 4: Database - MySQL

Beginning Web Development with Node.js

7 Web Databases. Access to Web Databases: Servlets, Applets. Java Server Pages PHP, PEAR. Languages: Java, PHP, Python,...

WebSphere and Message Driven Beans

Workshop for WebLogic introduces new tools in support of Java EE 5.0 standards. The support for Java EE5 includes the following technologies:

Complete Java Web Development

SSC - Web applications and development Introduction and Java Servlet (II)

Sonatype CLM for Maven. Sonatype CLM for Maven

Web Development CSE2WD Final Examination June (a) Which organisation is primarily responsible for HTML, CSS and DOM standards?

Debugging JavaScript and CSS Using Firebug. Harman Goei CSCI 571 1/27/13

Web Page Redirect. Application Note

Web Applications. Originals of Slides and Source Code for Examples:

CrownPeak Java Web Hosting. Version 0.20

CS506 Web Design and Development Solved Online Quiz No. 01

11.1 Web Server Operation

Learnem.com. Web Development Course Series. Quickly Learn. Web Design Using HTML. By: Siamak Sarmady

Specialized Programme on Web Application Development using Open Source Tools

Interactive Data Visualization for the Web Scott Murray

Web Designing with UI Designing

How To Create A Web Page On A Windows (For Free) With A Notepad) On A Macintosh (For A Freebie) Or Macintosh Web Browser (For Cheap) On Your Computer Or Macbook (

IBM Watson Ecosystem. Getting Started Guide

BAPI. Business Application Programming Interface. Compiled by Y R Nagesh 1

CTSU SSO (Java) Installation and Integration Guide

Transcription:

PA165 - Lab session - Web Presentation Layer Author: Martin Kuba <makub@ics.muni.cz> Goal Experiment with basic building blocks of Java server side web technology servlets, filters, context listeners, Java Server Pages, JSP EL Prerequisites Netbeans 7.3.x, Tomcat 7, Java 7, Maven 3 Maven web application Create a Maven web application. In NetBeans, choose File New Project, a proct creation wizard open. In its first step, select Categories: Maven, Projects: Web application : In its seconds step, type into the Package text field:: cz.muni.fi.pa165.web1 In its third step, select Tomcat and JavaEE 6 web. Click Finish. A new project is created. In Tools Options General Web browser choose you favorite browser. Press F6 or select Run Run project in menu. The project should compile, build, start Tomcat, deploy and open browser showing a page with Hello World!. Scream if something goes wrong.

Servlet games Find the source file src/main/webapp/index.jsp, delete its content and write the following: <%@page contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!DOCTYPE html> <html> <head> <title>home</title> </head> <body> <h1>hooray, it works!</h1> <ul> <li><a href="${pagecontext.request.contextpath/text/direct?a=1&b=ccc">text directly from servlet</a></li> <li><a href="${pagecontext.request.contextpath/file">file</a></li> <li><a href="${pagecontext.request.contextpath/redirect">redirect</a></li> <li><a href="${pagecontext.request.contextpath/text/bla">intentional error</a></li> </ul> <h2>will work later</h2> <ul> <li><a href="${pagecontext.request.contextpath/text/fromjsp">text from JSP</a></li> <li><a href="${pagecontext.request.contextpath/protected/protectedfile.txt">protected by password</a></li> </ul> </body> </html> The expression ${pagecontext.request.contextpath includes URL prefix of the web application. In the cz.muni.fi.pa165.web1 package in the src/main/java directory create a new Java class (do not select New Servlet) named MyDemoServlet.java :

Put there the following code: package cz.muni.fi.pa165.web1; import javax.servlet.servletexception; import javax.servlet.servletoutputstream; import javax.servlet.annotation.webservlet; import javax.servlet.http.*; import javax.xml.bind.datatypeconverter; import java.io.ioexception; import java.io.printwriter; import java.net.urlencoder; import java.util.*; import java.util.zip.*; @WebServlet(urlPatterns = {"/text/*", "/file/*", "/redirect/*") public class MyDemoServlet extends HttpServlet { protected void doget(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException { if ("/text".equals(request.getservletpath()) && "/direct".equals(request.getpathinfo())) { response.setcontenttype("text/html;charset=utf 8"); PrintWriter out = response.getwriter(); out.println("<h1>text</h1><pre>generated directly from servlet code"); out.println("serverinfo=" + getservletcontext().getserverinfo()); out.println("parameters:<i>"); for (String p : Collections.list(request.getParameterNames())) { out.println(p + "=" + request.getparameter(p)); else if ("/text".equals(request.getservletpath()) && "/fromjsp".equals(request.getpathinfo())) { request.setattribute("mylist", Arrays.asList("milk", "bread", "pizza")); request.getrequestdispatcher("/mypage.jsp").forward(request, response); else if ("/file".equals(request.getservletpath())) { response.setcontenttype("application/zip"); response.setheader("content disposition", "attachment; filename=\"myfile.zip\""); ServletOutputStream sos = response.getoutputstream(); ZipOutputStream zos = new ZipOutputStream(sos); zos.putnextentry(new ZipEntry("something.txt")); zos.write("some text".getbytes()); zos.putnextentry(new ZipEntry("differentfile.txt")); zos.write("other text".getbytes()); zos.close(); else if ("/redirect".equals(request.getservletpath())) { String url = request.getcontextpath() + "/text/direct?z=" + URLEncoder.encode("?/ =", "utf 8"); response.sendredirect(response.encoderedirecturl(url)); else { response.senderror(httpservletresponse.sc_not_found, "Nothing here, you are lost"); Run the project again (F6 or Run Run project) Try to click the links on the displayed page, and note the following: the servlet class is mapped to URLs using the @WebServlet annotation the mapped URL contains 3 parts contextpath, servletpath and pathinfo the method request.getparameternames() returns java.util.enumeration for historical reasons, it can be converted to Iterable using Collections.list()

all request parameters are of type String when forwarding to another servlet or JSP, data are passed using the setattribute() method when sending binary content, like the ZIP file, the header Content disposition selects what the browser should do with the file inline means to display, attachment means to save to a file when redirecting, the application relative URL must be prepared: prepended with contextpath to make it absolute encoded using encoderedirecturl() to keep session parameter values must be URL encoded instead of content or a redirect, we can send an error message using senderror() JSP - Java Server Page In the previous example, the link /text/fromjsp did not work, because the servlet forwarded to a non existing JSP page. Now is the time to create it. Create the file src/main/webapp/mypage.jsp. (Do not write the.jsp extension in the file name, NetBeans adds it even when it is already present, naming the file mypage.jsp.jsp!) Put there the following code, which is a mix of 4 languages: HTML (Hyper Text Markup Language), CSS (Cascading Style Sheets), JavaScript and Java:: <%@page import="java.util.list"%> <%@page contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!DOCTYPE html> <html> <head> <title>my JSP page</title> <style> #myheading { color: red; table.mytable { border-collapse: collapse; margin: 10px; table.mytable th, table.mytable td { border: solid 1px black; padding: 4px; </style> <script> console.log("hello JavaScript console!"); </script> </head> <body> <h1 id="myheading">my JSP page</h1> <p>list</p> <table class="mytable"> <% List<String> list = (List<String>) pagecontext.findattribute("mylist"); for(int i=0,n=list.size();i<n;i++) { %> <tr> <td><%=i%></td> <td><%=list.get(i)%></td> </tr> <% %> </table> </body> </html> Run the project again (F6). In Firefox press CTRL+Shift+K, in Chrome press F12, a javascript console will open showing the log message. Please note the following: at the first lines of a JSP, the @page directive should define

the output encoding in contenttype, otherwise the default iso 8859 1 will be used the source file encoding in the pageencoding attribute, otherwise OS default will be used the CSS selector #myheading select the one element with attribute id= myheading the CSS selector.mytable selects all elements with attribute class= mytable in JavaScript, we can write tracing messages using console.log() Java code in JSP is written between <% %> there is always implicit variable pagecontext in a JSP of type PageContext there is always implicit variable out in a JSP of type JspWriter instead of <%out.print(i);%> it is possible to write <%=i%> direct output of arbitrary texts is dangerous, as they may contain special HTML characters <>&, but there is no standard class for HTML encoding using entities < > & JSTL - JSP Standard Tag Library Instead of Java code, we can use JSTL library, which, albeit being standard, is not a standard part of a web container. We have to add a dependency to the pom.xml Maven file: <dependency> <groupid>org.glassfish.web</groupid> <artifactid>javax.servlet.jsp.jstl</artifactid> <version>1.2.2</version> </dependency> Edit the mypage.jsp and add the line <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> and replace the loop with: <c:foreach items="${mylist" var="s" varstatus="i"> <tr> <td>${i.count</td> <td><c:out value="${s"/></td> </tr> </c:foreach>

Filter Create a new class in src/main/java/cz/muni/fi/pa165/web1 named ProtectFilter.java: package cz.muni.fi.pa165.web1; import java.io.ioexception; import javax.servlet.filter; import javax.servlet.filterchain; import javax.servlet.filterconfig; import javax.servlet.servletexception; import javax.servlet.servletrequest; import javax.servlet.servletresponse; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.xml.bind.datatypeconverter; import javax.servlet.annotation.webfilter; @WebFilter("/protected/*") public class ProtectFilter implements Filter { private String username = "pepa"; private String password = "heslo"; public void init(filterconfig fc) throws ServletException { String u = fc.getinitparameter("username"); if(u!=null) username = u; String p = fc.getinitparameter("password"); if(p!=null) password = p; public void dofilter(servletrequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; String auth = request.getheader("authorization"); if (auth == null) { response.setstatus(httpservletresponse.sc_unauthorized); response.setheader("www Authenticate", "Basic realm=\"type password\""); String[] creds = new String(DatatypeConverter.parseBase64Binary(auth.split(" ")[1])).split(":", 2); if(!creds[0].equals(username)!creds[1].equals(password)) { response.setstatus(httpservletresponse.sc_unauthorized); response.setheader("www Authenticate", "Basic realm=\"type password\""); chain.dofilter(req, res); public void destroy() { Create a folder src/main/webapp/protected and inside it create file protectedfile.txt with content top secret. Run the project (F6) and click the protected link. Log in using the username and password.

Context listener Create a new class in src/main/java/cz/muni/fi/pa165/web1 named StartListener.java: package cz.muni.fi.pa165.web1; import javax.servlet.servletcontext; import javax.servlet.servletcontextevent; import javax.servlet.servletcontextlistener; import javax.servlet.annotation.weblistener; @WebListener public class StartListener implements ServletContextListener { public void contextinitialized(servletcontextevent ev) { ServletContext servletcontext = ev.getservletcontext(); servletcontext.log("starting application "); //initialize your app here... servletcontext.setattribute("mydb", "value"); public void contextdestroyed(servletcontextevent ev) { Run the project again (F6), you will in the Apache log the log message. Context listener is the right place for one time initialization at application start. That s all folks...