Java Web Programimi Servlet and JSP demo.

Slides:



Advertisements
Similar presentations
Java Server Pages (JSP)
Advertisements

Introduction to MVC Adding a View Page NTPCUG Tom Perkins, Ph.D.
Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages.
Object-Oriented Enterprise Application Development Tomcat 3.2 Configuration Last Updated: 03/30/2001.
J4www/jea Week 3 Version Slide edits: nas1 Format of lecture: Assignment context: CRUD - “update details” JSP models.
Servlets and a little bit of Web Services Russell Beale.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
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.
Web Applications Basics. Introduction to Web Web features Clent/Server HTTP HyperText Markup Language URL addresses Web server - a computer program that.
Julien Thibault / Phil Brewster / Kristina Doing-Harris
Java Servlets and JSP.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Java Server Pages (JSP) Presented by: Ananth Prasad & Alex Ivanov May 10, 2001.
JSP and Servlets Lecture notes by Theodoros Anagnostopoulos.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
1 Servlet How can a HTML page, displayed using a browser, cause a program on a server to be executed?
CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES LECTURE 5_1 George Koutsogiannakis/ Summer
Functionality of a web server What does the web server do? Let a user request a resource Find the resource Return something to the user The resource can.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
SKT-SSU IT Training Center Servlet and JSP. Chapter Three: Servlet Basics.
111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration.
 Embeds Java code  In HTML tags  When used well  Simple way to generate dynamic web-pages  When misused (complex embedded Java)  Terribly messy.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
CS-4220 Dr. Mark L. Hornick1 Servlet configuration and deployment.
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 Servlets example using NetBeans 6. Pre-requirements: Install Java JDK 1.6 Install NetBeans IDE 6 (we will use version NetBeans IDE update 16)
Java Servlets & Java Server Pages Lecture July 2013.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
Topic Java EE installation (Eclipse, glassfish, etc.) Eclipse configuration for EE Creating a Java Web Dynamic Project Creating your first servlet.
Chapter 2 Web app architecture. High-level web app architecture  When a client request coming in and needs servlet to serve dynamic web content, what.
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.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
1 Web Programming with Servlets & JSP ASSIGNMENT GUIDELINE.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Core basic Java web server technologies. Tools Eclipse IDE for Java EE Developers (Netbeans also works) nloads/packages/eclipse-
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
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.
 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,
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
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.
1 Web Programming with Servlets & JSPs WEB APPLICATIONS – AN OVERVIEW.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
Deploying Web Applications to Tomcat Server Chun Guo
Introduction to Servlets
CS3220 Web and Internet Programming Introduction to Java Servlets
Building Web Apps with Servlets
JSP (Java Server Page) JSP is server side technology which is used to create dynamic web pages just like Servlet technology. This is mainly used for implementing.
Google Web Toolkit Tutorial
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Java Servlets.
Java Server Pages.
Servlets and JSP 20-Nov-18 servletsJSP.ppt.
Java Server Pages (JSP)
COP 4610L: Applications in the Enterprise Spring 2005
Java Servlets and JSP.
CS3220 Web and Internet Programming Introduction to Java Servlets
Chengyu Sun California State University, Los Angeles
Installing Tomcat.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Directories and DDs 25-Apr-19.
Chengyu Sun California State University, Los Angeles
Directories and DDs 21-Jul-19.
Chengyu Sun California State University, Los Angeles
Directories and DDs 14-Sep-19.
Presentation transcript:

Java Web Programimi Servlet and JSP demo

Veglat JDK 1.7 MySQL 5.x MySQL Connector J Tomcat 6.x Eclipse

Building Web Applications Using Servlets and JSP Start (add) web server from tomcat Or from eclipse

A HelloWord Servlet New Dinamic Web Project HelloWord project Name Target runtime: Tomcat v7 Dynamic Web Module 2.5 Contex Name and root project WebContent On Java resources right click and add Servlet Package gui; class HelloWorld; doGet doPost Init and destroy extra info

In HelloWorld servlet class PrintWriter out = response.getWriter() Ctrl+shift+o for imports out.println("<html>"); out.println("<b>Hello World</b>"); out.println("</html>"); Go to start… Open in browser http://localhost:8080/HelloWorldProject/HelloWorld Do view source

In HelloWorld Dinamic Web Project Web content folder? New html page => HelloWorld.html Change the Title and Body content If Servlet are simply html in java Then JSP is java in html

HelloWorld JSP Add new JSP file to WebContent JSP are compiled to servlet by Tomcat Change the title and body to show the Hello World text in bold Now some Java <p> <%= "Hello world JSP from Java".toUpperCase() %> </p> <%= new java.util.Date() %> <body> <% java.util.Date today = new java.util.Date(); String toShow = "Today’s date is: "+today.toString(); %> out.println("<p>"); out.println(toShow); out.println("</p>");

Deployment Descriptors the Web.xml File File New Dynamic Project (keep defaults) Project Name Temp1 Check the Generate web.xml Add Servlet name: Test Start Url mapping is by default done directly in servlet Temp1 is context root (content folder) Test is resource name This is problematic since deployment to “real” servers will not work.

Deployment Descriptors the Web.xml File File New Dynamic Project Name: Deployment Dynamic web module version: 2.5 Keep defaults Check the Generate web.xml Add Servlet name: Hello To doGet() method add this: PrintWriter out = response.getWriter(); out.println("Hello from Servlet"); Execute the servlet Url is http://localhost:8080/Deployment/Hello The first part http://localhost:8080/ is servers address The second part is mapping which is defined in web.xml

Web.xml Is in WebContent->WEB-INF To open right click and open with text editor <welcome-file-list> … defines the home-pages Add a jsp file index.jsp to WebContent folder Go to root folder in url (http://localhost:8080/Deployment/)… Some words for servlet-name, servlet-class and servlet mapping to url-pattern Change the /Hello to /helloWorld Observe… The application server on real servers use this file in order to make real mappings

Web.xml Add login.jsp to WebContent Add a text in login.jsp “this could be a login page” In web.xml copy and paste <servlet>…</servlet-mapping> Change to <servlet-name>Login</servlet-name> And to <url-pattern>/loginpage</url-pattern> Change class-name line to => <jsp-file>demo.Hello</jsp-file> As a result: <servlet> <description></description> <servlet-name>Login</servlet-name> <jsp-file>/login.jsp</jsp-file> </servlet> <servlet-mapping> <url-pattern>/loginpage</url-pattern> </servlet-mapping>

Deployment Right click to project select export WAR file to desktop name it DeploymentDemo Copy file to …Desktop\APP\apache-tomcat-7.0.59\webapps which is the home of the tomcat http server Go to eclipse and stop local tomcat server!!! Go to Desktop\APP\apache-tomcat-7.0.59\bin and start the tomcat general server: startup.bat Go to http://localhost:8080/Deployment/ And give a try to loginpage, helloWorld Observe the folder structure of the webapps folder …

Importing Java Classes Into JSPs Import standard java classes Import your specific classes <%@ page import="java.util.Date, gui.*" %> <%= new Date() %> <% TextOutput textOut = new TextOutput(); out.println(textOut.getInfo()); %> package gui; public class TextOutput { public String getInfo() { return "This is returned from a method of a class in the gui package."; }

Getting URL Parameters http://localhost:8080/Scriptlets/index.jsp?id=4&user=bob&session=4453 Mechanism for passing data into your servlets So add new servlet named URLParameters The same applies to jsp Add new jsp file named parameters.jsp // http://localhost:8080/Scriptlets/URLParameters?id=8&user=john String user = request.getParameter("user"); String id = request.getParameter("id"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("The 'user' parameter is: " + user); out.println("<br/>"); out.println("The 'id' parameter is: " + id); out.println("</html>"); ======================== <% String name = request.getParameter("name"); // http://localhost:8080/Scriptlets/parameters.jsp?name=bob out.println("Name param is: " + name); %>

Scripting HTML Html generators <% for(int i=0; i<5; i++) { %> <p>This HTML is in a scriptlet loop, iteration: <%= i %></p> <p/> <% } %> <% String id = request.getParameter("id"); if(id == null) { %> <strong>ID parameter missing</strong> <% } else { %> ID parameter is: <%= id %>

Including Files In Other Files Create file includes.jsp Suppose you have copyright.txt file to include Static include Create copyright.txt Copyright aab.com Dynamic include updates.txt: Today's weather is: sunny JSP includes Create new jsp file: Variables.jsp <% String name = "Sue"; %> Runtime file including idnotfound.html : The ID parameter is missing. Idfound.html : The ID parameter was found. <!-- static include: content changes infrequently --> <%@ include file="copyright.txt" %> <p/> <!-- dynamic include: content changes a lot --> <jsp:include page="updates.txt" /> <!-- Must use static include if it's got java code in it that we want to access --> <%@ include file="variables.jsp" %> <%= name %> <!-- Must use include jsp tag if you don't know what file you want till run time --> <% String id = request.getParameter("id"); %> <% if(id == null) { %> <jsp:include page="idnotfound.html" /> <% } else { %> <jsp:include page="idfound.html" /> <% } %>

Forwarding and Redirecting To demonstrate forwarding add new jsp file forward.jsp Add the code from notes And observe the URL Hello <!-- Forward to another page (server side; the browser never sees forward.jsp, only index.jsp, but the url remains forward.jsp) the using jsp:forward tag --> <jsp:forward page="index.jsp"></jsp:forward> <% // forward to another page (server side) via Java // request.getRequestDispatcher("index.jsp").forward(request, response); // redirect to another page (client side) via Java. The browser will briefly // see forward.jsp, before going to the url for index.jsp // response.sendRedirect("index.jsp"); %>

Model 1 vs. Model 2 Architecture Start by creating new dynamic web project and name it MVC Add following JSP files to project login.jsp, about.jsp, error.jsp Model 1 architecture simply uses one starting jsp file and then by using forwarding and redirecting makes things work This model is unscalable and unstable so the model 2 is proposed which is also known as a MVC model Controller is servlet which makes routing So add an servlet in package controller name it Controller Now add index.jsp login.jsp => This is the login page. about.jsp => This is the about page. error.jsp => The action parameter is missing. Index.jsp => <p>This is the home page.</p> <p><a href="/MVC/Controller?action=login">Go to login page.</a></p> <p><a href="/MVC/Controller?action=about">Go to about page.</a></p> Controller.java String action = request.getParameter("action"); String page = null; if(action == null) { page = "/error.jsp"; } else if(action.equals("about")) { page = "/about.jsp"; else if(action.equals("login")) { page = "/login.jsp"; else { getServletContext().getRequestDispatcher(page).forward(request, response);