servlet - webapp

36
WEB PROGRAMMING WITH JSP & SERVLET WEB APPLICATION

Upload: way-rock

Post on 06-Apr-2018

231 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 1/36

WEB PROGRAMMING WITH

JSP & SERVLET

WEB APPLICATION

Page 2: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 2/36

CONTENTS• Applications

• HTTP protocols• Evolution of Servlets

• Web Application

• Web Application Development Process

• Servlet API

• Workshops

• Exercises

Page 3: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 3/36

 APPLICATIONS• A collection of programs is designed to perform a particular task 

(different purposes)

• Classification (based on running and accessibility)

Local machine

Single user

LAN, WAN, or MAN

Muli-users in particular

network only

Web ServerMulti-users having administrator or

equivalent privileges

Browsing with Web Browser

Page 4: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 4/36

 APPLICATIONS (cont)

• Advantages of Web Application –  Easier access to information (HTTP, Web Browser)

 –  Lower maintenance (modified at the server end) and deployment costs (notinstallation)

 –  Platform independency

 –  Wider visibility (access all around the world)

• Limitation of Web Servers –  Not feasible to maintain static content from the customer’s end (Static Web is

built by Web designer) –  Can not be managed the dynamic Web contain from client

 –  Overloading errors (Web servers)

 –  Unavailability due to maintenance

 –  Heavy traffic

 –  High maintenance costs –  Virus or worms attacks

Page 5: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 5/36

 HTTP PROTOCOLS

• Request – Response pairs

• Stateless

• Port 80 is default

192.168.54.3:80

http://microsoft.com/index.html

Connect

1. Convert http://microsoft.com/  to 192.168.54.3:80

2. Send a request to Web Server (index.html)

3. Web Server

processes a request

(connecting DB,

calculating, call

service …)

4. The result is responsed to Browser

5. Web Browser views

the result which

contains a markup

language

Page 6: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 6/36

 REQUEST MESSAGE

• A Stream of text•Return method

•Resource information (type&name, HTTP version)

•Ex: GET image/bar01.jsp HTTP/1.1

•Return the User-Agent (the browser) along with the Accept header

(provides information on what media types the client can accept)

•Ex: User-Agent: Mozilla/4.0 (compatible: MSIE 4.0 : Windows 95)

Accept : image/gif, image/jpeg, text/*, */*

Blank line

Page 7: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 7/36

 REQUEST OBJECTS

1. Form’s information is sent to Web

Server using request parameter.

2. Server process

requested client

(server script – 

Server Side),

connect DB ...

4. The result of processing is responsed

 3  . C onn e  c  t  

Database

• GET:

 –  Is the method commonly used to request a resource/ get information (access static resource such as  HTML doc and images or retrieve dynamicinformation such as query parameters) from server

 –  The length of query string is restricted 240 to 255

• POST: –  Is the method commonly used for passing user input/ sending information to the server (access dynamicresources and enable secure data in HTTP request because the request parameters are passed in thebody of request )

 –  No limit and Cannot be booked mark or emailed

Page 8: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 8/36

 RESPONSE MESSAGE

•Indicates status of request process (HTTP

version, response code, status)

•Ex: HTTP/1.1 200 OK

•Server. Ex: Server: JavaWebServer

•Last modified date. Ex: Last-modified: Tuesday, 24-Mar-09

8:30:34 GMT

•Content length. Ex: Content-length: 100

•Content type. Ex: Content-type: text/plain

Page 9: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 9/36

CGI• A small program (*.exe) is written in

languages such as C/C++, Perl, ... forthe gateway programs.

• Used in complex applications, suchas Web pages

• A set of standards followed tointerface applications form client sideto a Web Server

• Enables the Web server to send

information to other files and Webbrowsers

• Helps to process the inputs to theform on the Web page

• Helps to process the inputs to the

form on the Web page• Enables to obtain information and

use it on the server machine (serverside)

• When the Browser sends request toserver, CGI instantaties to receive

and process.

Page 10: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 10/36

CGI• Disadvantages

 –  Reducing efficiency –  Reloading Perl interpreter

 –  Interactive: not suitable for graphical or highly interactive

programs

 –  Time consuming and more memory consumed –  Debugging: error detection is difficult

 –  Not support Session

Page 11: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 11/36

SERVLETs• Is a Java code to generate the HTML and support the dynamic web

pages

• A server side scripting is not requirement of reloading the Servletcompiler each time a request is received from client

• Using multi threading (Overcome CGI’s consumed more memory)

• Gets auto refreshed on receiving a request each time

• A Servlet’s initializing code is used only for initializing in the 1st time

• Merits –  Enhanced efficiency (initializing only once, auto refresh)

 –  Ease to use (using Java combining HTML)

 –  Powerful (using Java)

 –  Portable –  Safe and cheap

• Demerits –  Low-level HTML documentation (Static well-formed-ness is not maintained)

 –  Unclear-session management (flow of control within the codes is very unclear)

Page 12: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 12/36

WEB APPLICATION 

• Registered into WWW

• Accessible from a public server• Comprises servlets, JSPs, images, HTML, JavaBeans,

applets, and Java classes

• Portability (easy copy/move or alternative)

• Store in single WAR file to deploy on the Web server

• Web Container

 – Is a program that manages execution of servlets and JSP

pages – Take requests from a Web Server and passes it to a servlet

for processing

 – Maintains the servlet life cycle

Page 13: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 13/36

WEB APPLICATION DIRECTORY STRUCTURE

Above structure is packaged into *.war file to deploy on Web Server

Page 14: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 14/36

WEB DEPLOYMENT DESCRIPTOR – web.xml <?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>

<servlet-name>servlet name</servlet-name>

<servlet-class>[package.]classname</servlet-class>

</servlet><servlet-mapping>

<servlet-name>servlet name</servlet-name>

<url-pattern> /context Path/root</url-pattern>

</servlet-mapping>

<session-config>

<session-timeout>30</session-timeout>

</session-config>

<welcome-file-list>

<welcome-file>default page to show</welcome-file>

</welcome-file-list>

</web-app>

Page 15: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 15/36

 EXAMPLE<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>

<servlet-name>HelloServlet</servlet-name>

<servlet-class>servlet.sample.HelloServlet</servlet-class>

</servlet><servlet-mapping>

<servlet-name>HelloServlet</servlet-name>

<url-pattern> /HelloServlet</url-pattern>

</servlet-mapping>

<session-config>

<session-timeout>30</session-timeout>

</session-config>

<welcome-file-list>

<welcome-file>HelloServlet</welcome-file>

</welcome-file-list>

</web-app>

Page 16: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 16/36

WEB APPLICATION DEVELOPMENT PROCESS

• Requirement tools: NetBeans 5.5.1

• Step 1: Creating a new project• Step 2: Creating the new Web files.

• Step 3: Specifying Name and Location for the

Project• 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

Page 17: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 17/36

Step 1: Creating a new project

• Click menu File

• Click the “New Project” Items

Page 18: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 18/36

Step 2: Creating the new Web files.

• Click Web categories

• Click the “Web Application” Projects

• Click Next button

Page 19: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 19/36

Step 3: Specifying Name and Location for the Project

Fill your project name

Browser your location

where store the project

Choose deployed server(Tomcat is default)

Choose J2EE 1.4

Modify the context pat

(if necessary). Defaults,

it is named same as

Project Name

• Click Finish button

WEB APPLICATION DIRECTORY STRUCTURE

Page 20: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 20/36

WEB APPLICATION DIRECTORY STRUCTURE

in NETBEANSProject 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

Page 21: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 21/36

Step 4: Specifying the Name of the Servlets

• Click Web categories

• Click the “Servlet” File Types

• Click Next button

Page 22: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 22/36

Step 4: Specifying the Name of the Servlets (cont)

• Click Next button

Fill your servlet name

Fill or choose package

name

Page 23: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 23/36

Step 4: Specifying the Name of the Servlets (cont)

• Click Finish button

• The servlet class (ex: HelloServlet.java) is added to source packages(with package name if it’s exist) and it’s information is added to xml

Modify the Servlet

Name or URL Pattern

if necessary) toconfigure the servlet

information to web.xml

Page 24: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 24/36

Step 5: 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("<html>");out.println("<head>");

out.println("<title>Hello Servlet</title>");

out.println("</head>");

out.println("<body>");

out.println("<h1>Welcome to Servlet course</h1>");out.println("</body>");

out.println("</html>");

out.close ();

}

Page 25: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 25/36

Step 5: Writing the code for Servlet & Compile (cont)

Page 26: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 26/36

Step 6: 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/*

Page 27: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 27/36

Step 7: Deploying in a Web Container

Page 28: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 28/36

Step 8: Executing the Servlet

CAREFULNESS

Page 29: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 29/36

CAREFULNESS

 –  Should stop the web server when modifying

 –  Should execute undeploy action when the same errors occur (the

error was corrected anyway)

• Notes:

 – Web Server should bestarted before the Webapplication is deployed orrun

ADDITIONAL

Page 30: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 30/36

ADDITIONAL

using wizard to web.xml

Modify servlet name

Modify URL Pattern

Add existing servlet toWeb App

Remove selected

servlet from Web AppChange servlet class

Process with init

parameter of Servlet

ADDITIONAL (cont)

Page 31: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 31/36

ADDITIONAL (cont)

testing any selecting servlet in project

• Click Ok button to test selected servlet

ADDITIONAL (cont)

Page 32: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 32/36

ADDITIONAL (cont)

hint

• Caches of server –  C:\Documents and Settings\Trong Khanh\.netbeans\5.5.1\apache-

tomcat-5.5.17_base\work\Catalina\localhost\ 

 –  C:\Users\usernames\.netbeans\5.5.1\apache-tomcat-

5.5.17_base\work\Catalina\localhost\  –  Above location should be gone and cleared when the application

cannot undeploy or the web servers occur the errors

GenericServlet Class

Page 33: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 33/36

GenericServlet Class• Defines a servlet that is not protocol dependent

• Implements the Servlet and the ServletConfig interfaces

• Retrieves the configuration information by implementing the

ServletObject• Some methods

Methods Descriptions

init- public void init() throws ServletException

- Initialises the servlet

service

- public abstract void service(ServletRequest req,

ServletResponse res) throws ServletException, IOException

- Called by the container to respond to a servlet request

getInitParameter- public String getInitParameter(String name)

- Return a String containing the value of name initialisation

getServletContext - public ServletContext getServletContext()- Returns the ServletContext in which this servlet instance is running

getServletConfig- public ServletConfig getServletConfig()

- returns the servlet configuration objects of this servlet instance

getServletInfo

- public String getServletInfo()

- returns the useful servlet information on the name of the creator of 

the servlet, version information and copyright information

S l Cl

Page 34: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 34/36

HttpServlet Class• Provides an abstract class to create an HTTP Servlet

• Extends the GenericServlet class

• A subclass of HttpServlet class must override at least one of thefollowing methods: doGet(), doPost, doPut(), doDelete(), init(),destroy(), and getServletInfo

• Some methods to process the request

Methods Descriptions

doGet

- protected void doGet(HttpServletRequest req,

HttpServletResponse res) throws

ServletException, IOException

- called by container to handle the GET request.

- This method is called through service() method

doPost

- protected void doPost(HttpServletRequest req,

HttpServletResponse res) throws

ServletException, IOException

- called by container to handle the POST request.

- This method is called through service() method

WORKSHOP ACTIVITIES

Page 35: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 35/36

WORKSHOP ACTIVITIES

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 5.5.9

EXERCISES

Page 36: Servlet - WebApp

8/3/2019 Servlet - WebApp

http://slidepdf.com/reader/full/servlet-webapp 36/36

EXERCISES

• Do it again all of demos

• Using servlet to write the programs that can do –  Present the Login form (naming LoginServlet) with title Login,

header h1 – Login, 02 textbox with naming txtUser and txtPass,

and the Login button

 –  Writing the ColorServlet that presents “Welcome to Servletcourse” with yellow in background and red in foreground

 –  Writing the ProductServlet includes a form with a combo box

containing Servlet & JSP, Struts & JSF, EJB, XMJ, Java Web

Services, and the button with value Add to Cart