WT Unit - 3 Servlets
WT Unit - 3 Servlets
WT Unit - 3 Servlets
PRASAD B
Assoc. Prof.
Dept. of Computer and Engineering
Email: [email protected]
PALLAVI
AssT. Prof.
Dept. of Computer and Engineering
Email: [email protected]
UNIT - 3
Objective
Email: [email protected]
UNIT - 3
Scope
Email: [email protected]
UNIT – 3
Course Outcomes
Email: [email protected]
UNIT - 3
SERVLETS
Client - Server Model
1. Requests
2. Responses
PRASAD B, Assoc. prof., Pallavi,
Asst. Prof, Dept. of CSE,
MLRITM,JNTU-H
PRASAD B, Assoc. prof., Pallavi,
Asst. Prof, Dept. of CSE,
MLRITM,JNTU-H
PRASAD B, Assoc. prof., Pallavi,
Asst. Prof, Dept. of CSE,
MLRITM,JNTU-H
PRASAD B, Assoc. prof., Pallavi,
Asst. Prof, Dept. of CSE,
MLRITM,JNTU-H
HTTP - Requests
SERVLETS
SERVLET - Introduction
applications.
are Secured, Scalable and Robust.
User clicks a link that has URL to a dynamic page instead of a static page.
Web Servers run the CGI program in seperate OS shell. The shell includes OS
The CGI response is sent back to the Web Server, which wraps the response in
OS shell.
It is Platform dependent.
Because of these disadvantages, developers started looking for better CGI
solutions. And then Sun Microsystems developed Servlet as a solution over
traditional CGI technology.
PRASAD B, Assoc. prof.,
MLRITM,JNTU-H
Advantages of using Servlets
separate thread.
SERVLETS
Servlet API
https://docs.oracle.com/cd/E17802_01/products/products/servlet/2.1/api/
packages.html
1. javax.servlet
2. javax.servlet.http
follows:
1. Servlet
2. ServletConfig
3. ServletContext
4. ServletRequest
5. ServletResponse
PRASAD B, Assoc. prof.,
MLRITM,JNTU-H
Classes in javax.servlet package
follows:
1. GenericServlet
2. ServletInputStream
3. ServletOutputStream
4. ServletException
PRASAD B, Assoc. prof.,
MLRITM,JNTU-H
Interface - javax.servlet
public interface Servlet
This interface is for developing servlets.
These are known as life-cycle methods, and are called by the network service in
public interface ServletConfig
getInitParameter (String name) Returns the parameter value for the specified
parameter name.
public interface ServletContext
significant events.
public interface ServletRequest
This interface is for getting data from the client to the servlet
The methods are then used by servlets when the service method is
service method.
getContentLength() Returns the size of the request entity data, or -1 if not known.
getContentType() Returns the Internet Media Type of the request entity data, or null if not
known.
getInputStream() Returns an input stream for reading binary data in the request body.
getProtocol() Returns the protocol and version of the request as a string of the
form <protocol>/<major version>.<minor version>.
getReader() Returns a buffered reader for reading text in the request body
getServerName() Returns the host name of the server that received the request.
getServerPort() Returns the port number on which this request was received.
PRASAD B, Assoc. prof.,
MLRITM,JNTU-H
Interface - javax.servlet.ServletResponse
public interface ServletResponse
This interface helps the servlet to formulate the response for the
clients request.
then used by servlets when the service method is run, to return data
to clients.
service method.
PRASAD B, Assoc. prof.,
MLRITM,JNTU-H
Method Description
easier.
destroy() is invoked only once throughout the life cycle and indicates that
servlet is being destroyed.
log(String msg) writes the given message in the servlet log file.
PRASAD B, Assoc. prof.,
log(String msg,Throwable t) writes the explanatory message in the servlet log file and a
MLRITM,JNTU-H
Class - javax.servlet.ServletInputStream
javax.servlet.ServletInputStream
class.
The getInputStream() method of ServletRequest interface
javax.servlet.ServletOutputStream
The getOutputStream() method ofServletResponse interface
public class ServletException
message.
as follows:
1. HttpSession
2. HttpServletRequest
3. HttpServletResponse
4. HttpSessionBindingListener
as follows:
1. Cookie
2. HttpServlet
3. HttpSessionEvent
4. HttpSessionBindingEvent
public interface HttpSession
1, 1970 UTC.
getLastAccessedTime() Returns the last time the client sent a request carrying
public interface HttpServletRequest
This interface gets data from the client to the servlet for use in
the HttpServlet.service method.
the service method.
getPathInfo() Gets any optional extra path information following the servlet path of this
request's URI, but immediately preceding its query string
getSession() Gets the current valid session associated with this request, if create is false
or, if necessary, creates a new session for the request.
getHeader(String) Gets the value of the requested header field of this request.
public interface HttpServletResponse
containsHeader(String f)
public interface HttpSessionBindingListener
Objects implement this interface so that they can be notified when they are being
communicates the event and identifies the session into which the object is bound.
bound or unbound
public class Cookie
This class represents a "Cookie", as used for session management with HTTP
Cookies are used to get user agents (web browsers etc) to hold small amounts of
low security user signon facilities, and helping collect data used for "shopping
doGet(HttpServletRequest, Performs the HTTP GET operation; the default implementation reports
doPost(HttpServletRequest, Performs the HTTP POST operation; the default implementation reports
doPut(HttpServletRequest, Performs the HTTP PUT operation; the default implementation reports
.
PRASAD B, Assoc. prof.,
MLRITM,JNTU-H
Interface - javax.servlet.http.HttpSessionEvent
public interface HttpSessionevent
1, 1970 UTC.
getLastAccessedTime() Returns the last time the client sent a request carrying
public interface HttpSessionBindingListener
Objects implement this interface so that they can be notified when they are being
communicates the event and identifies the session into which the object is bound.
bound or unbound
1. interface javax.servlet.http.HttpServletRequest
2. interface javax.servlet.http.HttpServletResponse
3. interface javax.servlet.http.HttpSession
4. interface javax.servlet.http.HttpSessionBindingListener
5. interface javax.servlet.http.HttpSessionContext
6. interface javax.servlet.RequestDispatcher
7. interface javax.servlet.Servlet
8. interface javax.servlet.ServletConfig
9. interface javax.servlet.ServletContext
10. interface javax.servlet.ServletRequest
11. interface javax.servlet.ServletResponse
12. interface javax.servlet.SingleThreadModel
SERVLETS
Handling
HTTP Request and Response
Methods
String getParameter( String name )
Returns value of parameter name (part of GET or POST)
Enumeration getParameterNames()
Returns names of parameters (POST)
Cookie[] getCookies()
Returns array of Cookie objects, can be used to identify client
HttpServletResponse Interface
HttpServletResponse
ServletOutputStream getOutputStream()
Gets byte-based output stream, send binary data to client
PrintWriter getWriter()
Gets character-based output stream, send text to client
https://www.youtube.com/watch?v=wHWG3cYQj3k
The request contains only the request line Along with request line and header
HTTP Request and HTTP header. it also contains HTTP body.
Parameter passing The form elements are passed to the server by The form elements are passed in the
appending at the end of the URL. body of the HTTP request.
Size The parameter data is limited (the limit Can send huge amount of data to
depends on the container) the server.
Usage Generally used to fetch some information Generally used to process the sent
from the host. data.
Example servlet
Handles HTTP GET requests
3. INPUT TYPE
Program Output
Example Explained
3 import javax.servlet.*;
4 import javax.servlet.http.*;
setContentType
Specify content
text/html for HTML documents
12
15 PrintWriter output;
output = response.getWriter(); // get writer
getWriter
Returns PrintWriter object, can send text to client
getOutputStream to send binary data (returns
ServletOutputStream object)
19 buf.append( "<HTML><HEAD><TITLE>\n" );
20 buf.append( "A Simple Servlet Example\n" );
21 buf.append( "</TITLE></HEAD><BODY>\n" );
22 buf.append( "<H1>Welcome to Servlets!</H1>\n" );
23 buf.append( "</BODY></HTML>" );
24 output.println( buf.toString() );
25 output.close(); // close PrintWriter stream
Example servlet
Survey
Store results in file on server
SERVLETS
SESSION TRACKING
https://www.youtube.com/watch?
v=VLg7WV5L0S4&list=UUhwKlOVR041tngjerWxVccw&index=1
Program Name :
1. HttpSessionAttributeDemoApp
2. HttpSession
3. HttpSession Login and Logout Example
https://www.youtube.com/watch?v=XyJe3jXEeqo
User Authorization
The web server requests the user name and password. The
subsequent request.
Data about the user and the user’s state can be saved in a shared
object.
Cookies
https://www.youtube.com/watch?v=lxxYdV92LLM
https://www.youtube.com/watch?v=Eu3YXeXC9e8
https://www.youtube.com/watch?v=Wi-zmexenB0
https://www.youtube.com/watch?
v=lbwFTNyRv8I&list=UUhwKlOVR041tngjerWxVccw&index=3
Program Name :
1.CookieDemoApp
2.Cookies Login and Logout Example
https://www.youtube.com/watch?v=0Ch2Bfea5yw
https://www.youtube.com/watch?
v=64hZwZ8hDs0&list=UUhwKlOVR041tngjerWxVccw&index=4
SERVLETS
JDBC
http://ramj2ee.blogspot.in/2014/07/jdbc-architecture.html#.Vq5eeJp97Mw
https://www.youtube.com/watch?v=JCtOzn1r2MY
https://www.youtube.com/watch?v=ry8AUOCccdY
Environment Used :
JDK version : 1.7.0_51
Mysql Server version : 5.6.19
https://www.youtube.com/watch?v=uRrctWrry4I
Java Decompiler
https://www.youtube.com/watch?v=z-dYli574dE
Environment Used :
JDK version : 1.7.0_51
Mysql Server version : 5.6.19
https://www.youtube.com/watch?v=KbAo5Ii-QLk
PRASAD B, Assoc. prof.,
MLRITM,JNTU-H
Extra :
https://www.youtube.com/watch?v=fA539fwWtHY
https://www.youtube.com/watch?v=fA539fwWtHY
PRASAD B, Assoc. prof.,
MLRITM,JNTU-H
Extra :
JDBC Connection
https://www.youtube.com/watch?v=qDD6YaK0W_U
https://www.youtube.com/watch?v=O4SNaqaWjA0
https://www.youtube.com/watch?v=fA539fwWtHY
PRASAD B, Assoc. prof.,
MLRITM,JNTU-H
Extra :
JDBC Demo –
Connect to Oracle Database
https://www.youtube.com/watch?v=D-c3EyuHUlA
https://www.youtube.com/watch?v=D-c3EyuHUlA
PRASAD B, Assoc. prof.,
MLRITM,JNTU-H
JDBC Driver Types
https://www.youtube.com/watch?v=-MLj-NIkEWo
Driver)
(Middleware Driver)
Driver)
PRASAD B, Assoc. prof.,
MLRITM,JNTU-H
Type 1 JDBC Driver :
JDBC-ODBC Bridge Driver
(Bridge Driver)
https://www.youtube.com/watch?v=np3TQe9mE0o
https://www.youtube.com/watch?v=9L7BRUTxbu8
https://www.youtube.com/watch?v=QV75ARl8ays