1 Web Programming with Servlets & JSPs WEB APPLICATIONS – AN OVERVIEW
Objectives Web Client – Server Model Evolution of Servlets Web Application Web Application Development Process Servlet API Workshops 2
Client – Server Model 3 Advantages of Web Application Easier access to information Lower maintenance and deployment costs Platform independency Wider visibility
Architecture of Web Applications 4 N – tiers Architecture
5 How Web Server works ? Translate 2. Server name – Microsoft.com 3. File name – index.htm 1. Protocol – HTTP address Connect Browser send a GET request Server response as HTML format
6 Web server …? File system contains HelloWorld.html
Communication/ Protocols Http Protocol Request Message structures Response Message structures
Request Objects 8 Request Parameters Request Methods
Limitation of Web Servers 9 Not feasible to maintain static content from the customer’s end Can not be managed the dynamic Web contain from client Overloading errors Unavailability due to maintenance Heavy traffic High maintenance costs Virus or worms attacks
10 Architechture : Web Application Collect information Send Request to Web ServerExecute Server Script Response result to browser
11 Server Side Script Common Gateway Interface (CGI). ISAPI Server-side JavaScript (SSJS). Personal Home Page (PHP). Java Servlet Active Server Page (ASP) Java Server Page (JSP). Ruby on Rail ASP.net …………………………………..
CGI 12 Disadvantages Reduced efficiency Reloading Perl interpreter No Session #include void main() { printf(“ \n”); printf(“ My CGI program \n”); printf(“ Hello \n”); printf(“ \n”); }
Servlets 13
Web Application 14 Concepts Registered into WWW Accessible from a public server Comprises servlets, JSPs, images, HTML, JavaBeans, applets, and Java classes Portability Store in WAR files to deploy on the Web server
Web Application Directory Structure 15
Web Deployment Descriptor – web.xml servlet name [package.]classname servlet name /context Path/root 30 default page to show Place in WEB-INF folder
Example HelloServlet demo.HelloServlet HelloServlet /Welcome Welcome index.jsp Alias Name
Web Application Development Process in NetBeans 18 Step 1: Creating a new project Step 2: Specifying Name and Location for the Project Step 3: Creating the new Web files. Step 4: Specifying the Name of the Servlets Step 5: Writing the code for Servlet & Compile Step 6: Package Servlet into WAR file Step 7: Deploying in a Web Container Step 8: Executing the Servlet
Creating a new project Click menu File Click the “New Project” Items
Creating the new Web Project Click Web categories Click the “Web Application” Projects Click Next button
Specifying Name and Location for the Project Fill your project name Browser your location where store the project Click Finish button
Choose Server 22 Choose WebServer and Context Path, click “Next”, “Finish”
Web Application Directory Structure in Netbeans Project name Interactive Web Directory Web Deployment descriptor Configuration directory related define for Web App Source java class required for Web App Lib directory required for Web App
Create Servlet 24
Named for Servlet 25 Name Must have Package name Click :”Next” “Finish”
Writing the code for Servlet & Compile Modify the processRequest method in servlet class. Ex: protected void processRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType ("text/html;charset=UTF-8"); PrintWriter out = response.getWriter (); out.println(" "); out.println(" Hello Servlet "); out.println(" "); out.println(" Welcome to Servlet course "); out.println(" "); out.close (); }
Writing the code for Servlet & Compile (cont)
Package Servlet into WAR file Package War file with command prompt jar –cvf fileName.war directoryOrFile (using blank to separate) Ex: jar –cvf HelloServlet.war *.jsp WEB-INF/* You can manual deploy this application : copy.war file to folder TOMCAT_HOME/webapps
Executing the Servlet
GenericServlet Class 30
HTTPServlet Class 31
Workshop Activities 32 Building the first Servlet named HelloServlets to greet the user “Hello Servlets” in the Web browser. Notes: The NetBean runs the Servlet with the Tomcat Server version