+ All Categories
Home > Documents > Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web...

Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web...

Date post: 13-Dec-2015
Category:
Upload: wilfred-marsh
View: 232 times
Download: 0 times
Share this document with a friend
Popular Tags:
148
Servlets Included in the J2EE
Transcript
Page 1: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Servlets Included in the J2EE

Page 2: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

TOPICS• Servlet Basics

• Setting up the Servlet API

• Creating a Web Application

• The Servlet URL and the Invoking Web Page

• Servlet Structure

• Testing a Servlet

• Passing Data

• Sessions

• Cookies

• Accessing a Database Via a Servlet

Page 3: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

What isStatic web page accessing

• www.xxx.yyy.x.html is given in IE

x.html

Page 4: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Dynamic Web Page

• NeedSuppose at server there is database of items and

stock,purchase details and server wants to make it available to the client via webpage

Server needs to process something and then generate the content

• Hence dynamic web page

Page 5: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Solutions for dynamic web page

• CGI(Common gateway interface)• Servlets and JSP• ASP• And many

Page 6: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

What is CGIWhat is the disadv. Of CGI

• CommonGatewayInterfaces• Small programs requested by client and run on server to

generate dynamic content• Ex: upon submitting a form you may request a CGI program to

be executed and transfer entries entered by client in web page to DB at server.It can be in C++/Perl.

• But these run as separate processes at Webserver increasing load on web server

• Hence expensive interms of resources• They arenot platform independent• DB connection per client request

Page 7: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Why are servletsWhat are the adv?

• Improoved Performance• They run under the addressspace of web server ,

hence no separate process environment created per request

• Platform independent since written in java• Webservers from different vendors support servlets• It can communicate with applets,databases and

other s/w via sockets

Page 8: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

What are servlets

• Servlets are java programs which run under the control of web server to help it to generate dynamic web page

Page 9: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

ServletLifeCycle• Three methods are central to the life cycle of any servlet.They are implemented by

every servlet and are called at specific times by web server(servlet container).

• User enters URL in a Webbrowser• Browser genearates an HTTP request and sends to the web server• The webserver upon receipt of this request maps this to the particular servlet• Then that servlet class is loaded and method init() is called• This method is called only once during lifetime of servlet• Init is helpful for configuring and setting initial parameters• The webserver calls service() next• This method actually processes HttpRequest and builds HttpResponse Object• For each client request a call to service is made • When server decides to shutdown or unload servlet it calls destroy() method• The code inside this relinquishes resources such as filehandlers

Page 10: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Steps forWriting your own servlet

• Installing required s/w ‘s refer other slides named servletinstallinfo.ppt

• Once everything is set then 1. create and compile the servlet source code 2. start webserver 3.start a web browser and request the servlet

Page 11: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

A simple HelloServlet• import java.io.*; • import javax.servlet.*; • • public class HelloServlet extends GenericServlet• { • • public void service(ServletRequest request, ServletResponse response) • throws ServletException, IOException• { • response.setContentType("text/html"); • PrintWriter pw = response.getWriter(); • pw.println("<B>Hello!"); • pw.close(); • } • }

Page 12: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

• The GenericServlet class provides functionality that simplifies the creation of a servlet. For example, it provides versions of init( ) and destroy( ), which may be used as it is. You need supply only the service( ) method.

• Inside HelloServlet service method is overridden.

• This method handles request from client

Page 13: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

• Notice that the first argument is a ServletRequest object. This enables the servlet to read data that is provided via the client request.

• The second argument is a ServletResponse object. This enables the servlet to formulate a response for the client.

• The call to setContentType( ) establishes the MIME type of the HTTP response. In this program, the MIME type is text/html. This indicates that the browser should interpret the content as HTML source code.

Page 14: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

• Next, the getWriter( ) method obtains a PrintWriter. Anything written to this stream is

sent to the client as part of the HTTP response. Then println( ) is used to write some simple HTML source code as the HTTP response.

Page 15: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Downloading and configering webserver

• From Apache site download Tomcat6.0 binary file and unzip(extract) it to say /home/student.

• It creates a directory named apachetomcat-versionnumber.

• Now goto lib subfolder of apachetomcat directory and see if servlet-api.jar is present.

• Copy this jar file to your student directory

Page 16: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Compilation

• Javac HelloServlet.java -classpath \home\student\servlet-api.jar

• Above step compiles your servlet code.• Now transfer your servlet’s class file to the

directory apachetomcat-versionnumber\webapps\examples\WEB-INF\classes

• In the WEB-INF directory there is an XML file called web.xml

Page 17: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

• Add the following in web.xml file• <servlet>• <servlet-name>HelloServlet</servlet-name>• <servlet-class>HelloServlet</servlet-class>• </servlet>• <servlet-mapping>• <servlet-name>HelloServlet</servlet-name>• <url-pattern>/servlet/HelloServlet</url-pattern>• </servlet-mapping>

Page 18: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

• Start Tomcat• Start Tomcat server by running startup.sh file from bin

directory of tomcat6.0. Tomcat must be running before you try to execute a servlet.

You can check if tomcat is running by typing http://localhost:8080 in webbrowser and see that it will open homepage of tomcat.

• Start a Web Browser and Request the Servlet• Start a web browser and enter the URL shown here:• http://localhost:8080/examples/servlet/HelloServlet• Alternatively, you may enter the URL shown here:• http://127.0.0.1:8080/examples/servlet/HelloServlet

Page 19: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

The Servlet API• Two packages contain the classes and

interfaces that are required to build servlets. – javax.servlet– javax.servlet.http.

• They constitute the Servlet API.

04/18/23 DEPT OF CSE 19

Page 20: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

The javax.servlet Package

04/18/23 DEPT OF CSE 20

Page 21: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Classes in the servlet package

04/18/23 DEPT OF CSE 21

Page 22: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Methods in servlet package

04/18/23 DEPT OF CSE 22

Page 23: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Servlet Interface• All servlet must implement the Servlet interface• It declares the init(), service() and destroy()

methods that are called by the server during the life cycle of a servlet- invoked by the server

• getServletConfig() method is called by the servlet to obtain initialization parameters

• getServletInfo() is overridden to provide with iuseful information

04/18/23 DEPT OF CSE 23

Page 24: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Methods in Servlet

04/18/23 DEPT OF CSE 24

Page 25: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Servlet Config Interface• This allows a servlet to obtain configuration

data when it is loaded

04/18/23 DEPT OF CSE 25

Page 26: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

The Servlet Context Interface

04/18/23 DEPT OF CSE 26

Enables servlets to obtain information about their environment

Page 27: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

ServletRequest and ServletResponse Interface

• Enables a servlet to obtain information about a client request

• Enables servlet to formulate a response for a client

04/18/23 DEPT OF CSE 27

Page 28: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Methods defined by ServletRequest

04/18/23 DEPT OF CSE 28

Page 29: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Methods defined by ServletResponse

04/18/23 DEPT OF CSE 29

Page 30: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Generic Servlet Class• Provides implementations of the basic life cycle

methods for a servlet.• Implements the Servlet and ServletConfig interfaces• Method to append a string to the server log file is

available– void log(String s)– void log(String s, Throwable e)– s is the string to be appended to the log, and e is an

exception that occurred.

04/18/23 DEPT OF CSE 30

Page 31: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

The ServletInputStream Class• The ServletInputStream class extends InputStream.• It is implemented by the servlet container and provides an

input stream that a servlet developer can use to read the data from a client request.

• It defines the default constructor. • Method is provided to read bytes from the stream. – int readLine(byte[ ] buffer, int offset, int size) throws IOException– buffer is the array into which size bytes are placed starting at

offset. The method returns the actual number of bytes read or –1 if an end-of-stream condition is encountered.

04/18/23 DEPT OF CSE 31

Page 32: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

The ServletOutputStream Class

• The ServletOutputStream class extends OutputStream.

• It is implemented by the servlet container and provides an output stream that a servlet developer can use to write data to a client response.

• A default constructor is defined. • It also defines the print( ) and println( ) methods,

which output data to the stream.

04/18/23 DEPT OF CSE 32

Page 33: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

The Servlet Exception Classes

• javax.servlet defines two exceptions.• The first is ServletException, which indicates

that a servlet problem has occurred. • Second is UnavailableException, which

extends ServletException It indicates that a servlet is unavailable.

04/18/23 DEPT OF CSE 33

Page 34: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Reading Servlet Parameters

• The ServletRequest interface includes methods that allow you to read the names and values of parameters that are included in a client request

04/18/23 DEPT OF CSE 34

Page 35: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

<html><body><center><form name="Form1“ method="post“ action="http://localhost:8080/servlets-examples/servlet/PostParametersServlet"><table><tr><td><B>Employee</td><td><input type=textbox name="e" size="25" value=""></td></tr><tr><td><B>Phone</td><td><input type=textbox name="p" size="25" value=""></td></tr></table><input type=submit value="Submit"></body></html>

04/18/23 DEPT OF CSE 35

Page 36: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

import java.io.*;import java.util.*;import javax.servlet.*;public class PostParametersServletextends GenericServlet {public void service(ServletRequest request,ServletResponse response)throws ServletException, IOException {// Get print writer.PrintWriter pw = response.getWriter();// Get enumeration of parameter names.Enumeration e = request.getParameterNames();// Display parameter names and values.while(e.hasMoreElements()) {String pname = (String)e.nextElement();pw.print(pname + " = ");String pvalue = request.getParameter(pname);pw.println(pvalue); }pw.close(); } }

04/18/23 DEPT OF CSE 36

Page 37: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

The javax.servlet.http Package

04/18/23 DEPT OF CSE 37

Page 38: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Class provided in the javax.servlet.http Package

04/18/23 DEPT OF CSE 38

Page 39: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

The HttpServletRequest and HttpServletResponse Interface Interface

• The HttpServletRequest interface enables a servlet to obtain information about a client request.

• The HttpServletResponse interface enables a servlet to formulate an HTTP response to a client.

• Constants that correspond to the different status codes that can be assigned to an HTTP response.– SC_OK indicates that the HTTP request succeeded– SC_NOT_FOUND indicates that the requested resource is

not available.04/18/23 DEPT OF CSE 39

Page 40: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

04/18/23 DEPT OF CSE 40

Page 41: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

04/18/23 DEPT OF CSE 41

Page 42: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

The HttpSession Interface

• The HttpSession interface enables a servlet to read and write the state information that is associated with an HTTP session.

• The methods throw an IllegalStateException if the session has already been invalidated.

04/18/23 DEPT OF CSE 42

Page 43: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

04/18/23 DEPT OF CSE 43

Page 44: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

The HttpSessionBindingListener Interface

• The HttpSessionBindingListener interface is implemented by objects that need to be notified when they are bound to or unbound from an HTTP session.

• The methods that are invoked when an object is bound or unbound – void valueBound(HttpSessionBindingEvent e)– void valueUnbound(HttpSessionBindingEvent e)– e is the event object that describes the binding.

04/18/23 DEPT OF CSE 44

Page 45: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

The Cookie Class• The Cookie class encapsulates a cookie.• A cookie is stored on a client and contains state information. • Cookies are valuable for tracking user activities.• The user does not need to enter this data each time he or she

visits the store.• Aservlet can write a cookie to a user’s machine via the

addCookie( ) method of the• HttpServletResponse interface. The data for that cookie is then

included in the header of the HTTP response that is sent to the browser.

04/18/23 DEPT OF CSE 45

Page 46: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

• The names and values of cookies are stored on the user’s machine. • The information that is saved for each cookie

• The name of the cookie• The value of the cookie• The expiration date of the cookie• The domain and path of the cookie

• The expiration date determines when this cookie is deleted from the user’s machine.

• If an expiration date is not explicitly assigned to a cookie, it is deleted when the current browser session ends.

• Otherwise, the cookie is saved in a file on the user’s machine.

04/18/23 DEPT OF CSE 46

Page 47: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

• The domain and path of the cookie determine when it is included in the header of an HTTP request.

• If the user enters a URL whose domain and path match these values, the cookie is then supplied to the Web server.

• Otherwise, it is not.• There is one constructor for Cookie. It has the signature

shown here:– Cookie(String name, String value)– Name and value of the cookie are supplied as arguments to the

constructor

04/18/23 DEPT OF CSE 47

Page 48: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

04/18/23 DEPT OF CSE 48

Page 49: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

The HttpServlet Class• The HttpServlet class extends GenericServlet.• It is commonly used when developing servlets that

receive and process HTTP requests.

04/18/23 DEPT OF CSE 49

Page 50: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

04/18/23 DEPT OF CSE 50

Page 51: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

The HttpSessionEvent Class• HttpSessionEvent encapsulates session events.• It extends EventObject and is generated when a

change occurs to the session. – HttpSessionEvent(HttpSession session)– session is the source of the event.– HttpSessionEvent defines one method, getSession( )– HttpSession getSession( )– It returns the session in which the event occurred.

04/18/23 DEPT OF CSE 51

Page 52: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

04/18/23 DEPT OF CSE 52

Page 53: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

The HttpSessionBindingEvent Class• The HttpSessionBindingEvent class extends HttpSessionEvent.

It is generated when a listener is bound to or unbound from a value in an HttpSession object.

• It is also generated when an attribute is bound or unbound. Here are its constructors:– HttpSessionBindingEvent(HttpSession session, String name)– HttpSessionBindingEvent(HttpSession session, String name, Object val)– Here, session is the source of the event, and name is the name associated

with the object that is– being bound or unbound. If an attribute is being bound or unbound, its

value is passed in val.

04/18/23 DEPT OF CSE 53

Page 54: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

• The getName( ) method obtains the name that is being bound or unbound. – String getName( )

• The getSession( ) method, shown next, obtains the session to which the listener is being bound or unbound:– HttpSession getSession( )

• The getValue( ) method obtains the value of the attribute that is being bound or unbound.– Object getValue( )

04/18/23 DEPT OF CSE 54

Page 55: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Handling HTTP Requests and Responses

• The HttpServlet class provides specialized methods that handle the various types of HTTP requests.

• doDelete( ), doGet( ), doHead( ), doOptions( ), doPost( ), doPut( ), and doTrace( ).

04/18/23 DEPT OF CSE 55

Page 56: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Handling HTTP GET Requests<html><body><center><form name="Form1"action="http://localhost:8080/servlets-examples/servlet/ColorGetServlet"><B>Color:</B><select name="color" size="1"><option value="Red">Red</option><option value="Green">Green</option><option value="Blue">Blue</option></select><br><br><input type=submit value="Submit"></form></body></html>

04/18/23 DEPT OF CSE 56

Page 57: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class ColorGetServlet extends HttpServlet {public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {String color = request.getParameter("color");response.setContentType("text/html");PrintWriter pw = response.getWriter();pw.println("<B>The selected color is: ");pw.println(color);pw.close(); } }

04/18/23 DEPT OF CSE 57

Page 58: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Handling HTTP POST Requests<html><body><center><form name="Form1"method="post"action="http://localhost:8080/servlets-examples/servlet/ColorPostServlet"><B>Color:</B><select name="color" size="1"><option value="Red">Red</option><option value="Green">Green</option><option value="Blue">Blue</option></select><br><br><input type=submit value="Submit"></form></body></html>

04/18/23 DEPT OF CSE 58

Page 59: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

import javax.servlet.*;import javax.servlet.http.*;public class ColorPostServlet extends HttpServlet {public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {String color = request.getParameter("color");response.setContentType("text/html");PrintWriter pw = response.getWriter();pw.println("<B>The selected color is: ");pw.println(color);pw.close();}}

04/18/23 DEPT OF CSE 59

Page 60: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Creating a Web Application

A Simple Example

Page 61: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Create the following HTML document

Page 62: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Servlet Structure

• Servlets must import the following two packages

1. javax.servlet

2. javax.servlet.http

• Servlets must extend class HttpServlet from package

java.servlet.http.

• Two most common HTTP requests

1. GET (for multiple data items)

2. POST (for single items)

Page 63: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Servlet Structure

• At the servlet end, method service will despatch either

method doGet or method doPost in response to these

requests

• All three methods have a void return type and take the following

two arguments:

1. An HttpServletRequest object;

2. An HttpServletResponse object.

Page 64: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Servlet Structure

• HttpServletResponse object has two methods

1. void setContentType(String <type>)

• specifies the data type of the response.

• Normally, this will be "text/HTML".

2. PrintWriter getWriter()

• Returns the output stream object to which the servlet can

write character data to the client.

Page 65: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Servlet Structure

• There are four basic steps in a servlet...

1. Execute the setContentType method with an argument of

"text/HTML".

2. Execute the getWriter method to generate a PrintWriter

object.

3. Retrieve any parameter(s) from the initial Web page.

4. Use the println method of the above PrintWriter object to

create elements of the Web page to be 'served up' by our

Web server.

Page 66: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

First servlet

Page 67: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

First servlet

Page 68: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Passing Data

Page 69: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Passing Data

Page 70: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Passing Data

• The methods of HttpServletRequest that are responsible for

handling values/parameters received by servlets are listed

below

Page 71: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Passing Data

Page 72: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Passing Data

Page 73: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Passing Datainitial page

Page 74: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Passing Dataservlet-generated

Page 75: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Passing DataHow to force the browser to reload the original page, rather than

retrieve it from its cache, when a return is made to this page.?

There is an HTML META tag that will do this, but the tag varies

from browser to browser.

Page 76: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Passing DataHow to force the browser to reload the original page, rather than

retrieve it from its cache, when a return is made to this page.?

There is an HTML META tag that will do this, but the tag varies

from browser to browser.

These should be placed immediately after the <HEAD> tag on the initial Web page

Page 77: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Passing DataAccepting two numbers, adds them and then displays the result

Page 78: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Passing DataAccepting two numbers, adds them and then displays the result

Page 79: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Accepting two numbers, adds them and then displays the result

• user may enter a non-numeric value, the servlet must cater

for a possible NumberFormatException.

• method getParameter will need to convert the strings it

receives into integers by using the parseInt

Page 80: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.
Page 81: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.
Page 82: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.
Page 83: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.
Page 84: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.
Page 85: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Sessions

Page 86: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Why Sessions?• HTTP is that it is a stateless protocol

• Each request and each response is a self contained

and independent transaction

• A session is a container where data about a client's

activities may be stored and accessed by any of the

servlets that have access to the session object.

Page 87: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Why Sessions?

• A session object is created by means of the

getSession ()method of HttpServletRequest

• the server returns the current session if there is one; otherwise, it creates a new session object.

For example:

Page 88: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Why Sessions?

• A session object contains a set of name-value

pairs.

• Each name is of type String and each value is of type Object.

• Note that objects added to a session must implement the

Serializable interface.

• A servlet may add information to a session object via the

following method:

Page 89: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Why Sessions?

• A session object contains a set of name-value

pairs.

Page 90: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Why Sessions?

Page 91: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Why Sessions?

Page 92: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Why Sessions?

Page 93: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

HTML file

Page 94: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

HTML file

Page 95: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

HTML file

Page 96: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

HTML page

Page 97: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Selection servlet

Page 98: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Selection servlet

Page 99: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Selection servlet

Page 100: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Selection servlet

Page 101: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Selection servlet

Page 102: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Selection servlet

Page 103: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Selection servlet

Page 104: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Initial Page

Page 105: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Weight servlet

Page 106: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Weight servlet

Page 107: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Weight servlet

Page 108: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Weight servlet

Page 109: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Weight servlet

Page 110: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Checkout servlet

Page 111: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Checkout servlet

Page 112: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Checkout servlet

Page 113: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Checkout servlet

Page 114: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Checkout servlet

Page 115: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Checkout servlet

Page 116: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Checkout page

Page 117: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

What is a Cookie?

• A cookie is an associated name-value pair in which

both name and value are strings.

• Each cookie is held in a small file sent by the server to

the client machine

• It is retrieved by the server on subsequent visits by

the user to the site.

Page 118: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

What is a Cookie?

• The constructor for a Java Cookie object must have this

signature

• Once created, it must be added to the HttpServletResponse

object via the following HttpServletResponse method :

Page 119: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

What is a Cookie?

• Cookies are retrieved via the following method of class

HttpServletRequest

Page 120: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

What is a Cookie?

setMaxAge method• Specifies the number of seconds for which the cookie will

remain in existence

• If any negative value is specified, then the cookie goes out of

existence when the client browser leaves the site

• A value of zero causes the cookie's immediate destruction.

Page 121: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Example - CookieAdder

Page 122: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Example

Page 123: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Example

Page 124: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Example

Page 125: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Example - GetPreferences

Page 126: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Example

Page 127: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Example

Page 128: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Example

Page 129: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.
Page 130: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

ShowSum Servlet

Page 131: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

ShowSum Servlet

Page 132: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

ShowSum Servlet

Page 133: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

ShowSum Servlet

Page 134: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

ShowSum Servlet

Page 135: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

ShowSum Servlet

Page 136: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

ShowSum Page

Page 137: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Accessing a Database Via a Servlet

Page 138: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Example

Page 139: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

Example

Page 140: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

servlet DbServlet

Page 141: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

servlet DbServlet

Page 142: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

servlet DbServlet

Page 143: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

servlet DbServlet

Page 144: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

servlet DbServlet

Page 145: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

servlet DbServlet

Page 146: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

servlet DbServlet

Page 147: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

servlet DbServlet

Page 148: Servlets Included in the J2EE. TOPICS Servlet Basics Setting up the Servlet API Creating a Web Application The Servlet URL and the Invoking Web Page Servlet.

servlet DbServlet


Recommended