0% found this document useful (0 votes)
13 views54 pages

Understanding Java Servlets and HTTP

Uploaded by

parmar2100parmar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views54 pages

Understanding Java Servlets and HTTP

Uploaded by

parmar2100parmar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Servlets

PMCA502L: Thilagavathi M, AP(Sr.), SCORE


Introduction to Web

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
HTTP

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
HTTP Important Methods
• GET
• POST
• OPTIONS
• DELETE
• HEAD

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Anatomy of an HTTP GET request

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Anatomy of an HTTP POST request

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Servlet

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
CGI

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Advantages of Servlets over Traditional CGI

• Less response time because each


request run in a separate thread.

• Servlet are scalable.


• Servlet are robust and object oriented.
• Servlet are platform independent.
PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Servlet Life Cylce

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
How does a Servlet Application Work?
1) User sends request for a servlet by
clicking a link that has URL to a servlet.

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
How does a Servlet Application Work?
2) The container finds the servlet using
deployment descriptor and creates two
objects
HttpServletRequest
HttpServletResponse

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
How does a Servlet Application Work?
3) The container creates or allocates a
thread for that request and calls the
servlet's service() method and passes the
request, response objects as argument

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
How does a Servlet Application Work?
4) The service method decide which servlet method
doGet() or doPost() to call based on HTTP Request
Method(Get,Post) sent by the client.

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
How does a Servlet Application Work?
5) The servlet uses response object to
write the response back to the client

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
How does a Servlet Application Work?
6) After the service() method is completed
the thread dies. The request and response
object are ready for garbage collection

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Servlet Program Essentials

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Servlet API
• Servlet API consists of two important
packages
 [Link]
 [Link]

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
[Link]
• Some Important Classes and Interface
of [Link]

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
[Link]

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Servlet Interface
• Methods
void init(ServetConfig config) throws ServletException
void service(ServletRequest req, ServletResponse resp)
throws ServletException, IOException
void destroy()
String getServletInfo()

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Servlet Interface
• Methods
void init(ServetConfig config) throws ServletException
ServletConfig getServletConfig()
void service(ServletRequest req, ServletResponse resp)
throws ServletException, IOException
String getServletInfo()
void destroy()

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
ServletConfig Interface

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
ServletContext Interface

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
ServletRequest Interface
• int getContentLength();
• String getContentType();
• ServletInputStream getInputStream();
• BufferedReader getReader();
• String getParameter(String pname);
• String[] getParameterValues(String pname);
• Enumeration getParameterNames();

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
ServletRequest Interface

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
ServletRequest Interface

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
ServletRequest Interface

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
ServletResponse Interface

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
GenericServlet
• Provides implementations of the basic life cycle
methods for a servlet.
• Implements Servlet and ServletConfig interfaces.
• Additional methods
void log(String s)
void log(String s, Throwable e)

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
[Link]

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
HTTPServlet
• The HttpServlet class is an abstract class that
extends the GenericServlet class and
implements a Serializable interface.

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
HttpServlet Class

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
HttpServlet Class

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
HttpServletRequest Interface

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
HttpServletRequest Interface

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
HttpServletResponse Interface

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
HttpServletResponse Interface

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Example 1

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
import [Link].*;
[Link].*;
[Link].*;
public class PostParametersServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
PrintWriter pw = [Link]();
Enumeration e = [Link]();
while([Link]( )) {
String pname = (String)[Link]();
[Link](pname + " = ");
String pvalue = [Link](pname);
[Link](pvalue);
}
[Link]();
}
}

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Example
html>
<head>
</head>
<body>
<form name="Form1" action="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>
PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class ColorGetServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
String color = [Link]("color");
[Link]("text/html");
PrintWriter pw = [Link]();
[Link]("<B>The selected color is: ");
[Link](color);
[Link]();
}
}
PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Cookies

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Cookies
• Cookies are text files stored on the client
computer and they are kept for various
information tracking purpose.
• A cookie is a small piece of information that
persists between the multiple client requests.
• A cookie has a name, a single value, and
optional attributes such as a comment, path
and domain qualifiers, a maximum age, and a
version number.

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
How Cookie Works

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Types of Cookie
• Non-persistent
• Persistent

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Methods
• HttpServletRequest Interface
 Cookies[] getCookies() method is provided to
get the array of Cookies from request, since
there is no point of adding Cookie to request,
there are no methods to set or add cookie to
request.
• HttpServletResponse Interface
 addCookie(Cookie c) method is provided to
attach cookie in response header, there are no
getter methods for cookie.
• Cookie class has a single constructor that takes
name and value because they are mandatory
parameters for a cookie, all other parameters are
optional.

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Methods of Cookie Class
• getComment() setComment()
• getDomain() setDomain()
• getMaxAge() setMaxAge()
• getName()
• getPath() setPath()
• boolean getSecure(); setSecure()
• getValue() setValue()
• getVersion()
• isHttpOnly()

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Sessions
• HTTP is a "stateless" protocol which means each
time a client retrieves a Web page, the client opens
a separate connection to the Web server and the
server automatically does not keep any record of
previous client request.
• Still there are three ways to maintain session
between web client and web server:
 Cookies
 Hidden Form Fields
 Example
<input type="hidden" name="sessionid" value="12345">
 URL Rewriting
 Example
[Link]
PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Sessions
• The HttpSession Object:
 Apart from the above mentioned three ways, servlet
provides HttpSession Interface which provides a way to
identify a user across more than one page request or visit
to a Web site and to store information about that user.
 The servlet container uses this interface to create a session
between an HTTP client and an HTTP server. The session
persists for a specified time period, across more than one
connection or page request from the user.
 You would get HttpSession object by calling the public
method getSession() of HttpServletRequest, as below:
HttpSession session = [Link]();
HttpSession session = [Link](boolean);

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
HttpSession Interface

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Servlet with DB
<html>
<head>
<title>Servlet DB</title>
</head>
<body>
<Form name="form1" method="post" action="NewServlet_DB">
Enter ID: <Input Type="text" name="id"><BR>
Enter Name: <Input Type="text" name="name"><BR>
Enter Total: <Input Type="text" name="total"><BR>
Enter CGPA: <Input Type="text" name="cgpa"><BR>
<input type="submit" value="Submit">
</Form>
</body>
</html>

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE
Servlets with DB
public class NewServlet_DB extends HttpServlet {
Connection c=null; Statement s=null; ResultSet rs=null;
protected void processRequest(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
try{
[Link]("[Link]");
c=[Link]("jdbc:derby://localhost:1527/Login;
create=true;user=test;password=test");
[Link]("Connection established");
s=[Link]();
String id=[Link]("id");
String name=[Link]("name");
int total = [Link]([Link]("total"));
double cgpa=[Link]([Link]("cgpa"));
int n=[Link]("INSERT INTO student
VALUES('"+id+"','"+name+"',"+total+","+cgpa+")");
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
if(n==1)
[Link]("<h2>Inserted<h2>");
[Link](); [Link](); [Link]();
}
catch(SQLException | ClassNotFoundException e){
[Link](e);
} PMCA502L: Thilagavathi M,
} AP(Sr.), SCORE
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
processRequest(request, response);
}
}

PMCA502L: Thilagavathi M,
AP(Sr.), SCORE

You might also like