Unit-5
Unit-5
9Client
-Web
Web browser (IE,
(IE Firefox)
9Server
-Web server (Tomcat, IIS)
9Protocoll
9P
-HTTP
9Language
-HTML
9As we are talking about web related stuff so, for us client
is our veryy own browsers
Protocol
Language
9Gi
9Gives i
instructions
i to the
h browser
b
Lets put them together
9Requestt is
9R i some kind
ki d off data
d t which
hi h is
i taken
t k by
b HTTP to
t
the server
R
Request
t
9Request
q is browser's interaction with server
9Key elements of request stream
1.HTTP method
-Get
-Post
2 The page URL
2.The
3.Form parameters
Response
HTTP Request
9Every
Every request has a request line, request headers and
message body (in the case of post method)
9I has
9It h response headers
h d withi h the
h response content
9 The
Th contents
t t off those
th W b pages mustt be
Web b dynamically
d i ll
generated in order to reflect the latest information in the
database
9In the early days of the Web, a server could dynamically
construct a ppage
g byy creatingg a separate
p pprocess to handle
each client request
9A variety
i off different
diff l
languages were usedd to build
b ild CGI
programs. These included C, C++, and Perl
9However, CGI suffered serious performance problems
9In
In addition,
addition the CGI programs were not platform
platform-
independent
9First, performance
9Fi f iis significantly
i ifi l better
b
-Servlets execute within the address space of a Web server
-It
It is not necessary to create a separate process to handle
each client request
Tomcat Installation
9Set JAVA_HOME environment Variable
9Set the environment variable for CATALINA_HOME as the
Tomcat’s directory. It is needed to start the Tomcat server
9Goto Tomacat.exe file
FirstServlet.java
j
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FirstServlet extends HttpServlet
{
public void doGet(HttpServletRequest
request,HttpServletResponse response)
throws IOException,ServletException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<head>");
p ( );
out.println("<title>My First Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello! WELCOME to Servlets</h1>");
out.println("</body>");
t i tl ("</b d >")
out.println("</html>");
}
}
9Create firstservlet folder under C:\Program Files\Tomcat 6.0\
webapps
9Create web.xml
<web-app>
<servlet>
<servlet-name>demo</servlet-name>
<servlet-class>FirstServlet</servlet-class>
l l Fi S l / l l
</servlet>
pp g
<servlet-mapping>
<servlet-name>demo</servlet-name>
<url-pattern>/First</url-pattern>
</servlet-mapping>
</web-app>
9Pl
9Place web.xml
b l under
d C \P
C:\Program Fil \T
Files\Tomcat
t 6.0\
6 0\ webapps\
b \
firstservlet\WEB-INF
9Start the Tomcat Server, and run the FirstServlet by typing
http://localhost:8080/firstservlet/First in the URL of the web browser
9Our fi
9O firstservlet
t l t
folder will look like
this
Life cycle of a servlet
9Three methods are central to the life cycle
y of a servlet
Th Servlet
The S l t API
9Two packages contain the classes and interfaces that are
required to build servlets
ServletRequest methods
9GenericServlet implements
p the Servlet and ServletConfigg
interfaces
Reading Servlet Parameters
9The ServletRequest
q class includes methods that allow yyou
to read the names and values of parameters that are included
in a client request
Example
E l (PostParameters.html)
(P tP t ht l)
<html>
<body>
<center>
<form name="Form1"
method get
method="get"
action="http://localhost:8080/postparameters/Para">
<table>
<t >
<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
pu type=submit
ype sub vvalue="Submit">
ue Sub
</form>
</center>
</body>
</html>
(PostParametersServlet.java)
import java.io.*;
import
p jjava.util.*;;
import javax.servlet.*;
public class PostParametersServlet extends GenericServlet {
public void service(ServletRequest request,
request ServletResponse response)
throws ServletException, IOException {
// Get print writer.
P i tW it pw = response.getWriter();
PrintWriter tW it ()
// Get enumeration of parameter names.
Enumeration e = request.getParameterNames();
// Display parameter names and values.
while(e.hasMoreElements()) {
String pname = (String)e.nextElement();
(String)e nextElement();
pw.print(pname + " = ");
String pvalue = request.getParameter(pname);
pw.println(pvalue);
i tl ( l )
}
pw.close();
}
}
9Follow the steps which are described in the firstservlet
example
p and then
1.Start Tomcat (if it is not already running)
2.Display the Web page (PostParameters.html) in a browser
9Used
9U d to build
b ild servlets
l that
h workk with
i h HTTP requests andd
responses
9It enables
bl a servlet
l t to
t readd andd write
it the
th state
t t information
i f ti
that is associated with an HTTP session
HttpSession methods
The Cookie Class
9The Cookie class encapsulates a cookie. A cookie is stored
on a client and contains state information
3.Select a color
4.Submit the Web page. The browser will display the
response that is dynamically generated by the servlet
9Parameters for an HTTP GET request are included as part
of the URL that is sent to the Web server
Usingg Cookies
9Let’s develop a servlet that illustrates how to use cookies
(AddCookie.html)
<ht l>
<html>
<body>
<center>
<form name="Form1"
method="post"
action http://localhost:8080/addcookie/Add
action="http://localhost:8080/addcookie/Add">
<B>Enter a value for MyCookie:</B>
<input type=textbox name="data" size=25 value="">
<input type=submit value="Submit">
</form>
</center>
/
</body>
</html>
(AddCookieServlet.java)
import java.io.*;
import javax.servlet.
javax servlet *;;
import javax.servlet.http.*;
public class AddCookieServlet extends HttpServlet {
public
bli void id doPost(HttpServletRequest
d P (H S l R request, HttpServletResponse
H S l R
response) throws ServletException, IOException {
// Get pparameter from HTTP request.
q
String data = request.getParameter("data");
// Create cookie.
Cookie cookie = new Cookie(
Cookie("MyCookie"
MyCookie , data);
// Add cookie to HTTP response.
response.addCookie(cookie);
// Write output to browser.
response.setContentType("text/html");
C T (" /h l")
PrintWriter pw = response.getWriter();
ppw.println("<B>MyCookie
p ( y has been set to");
);
pw.println(data);
pw.close();
}
}
(GetCookiesServlet.java)
i
import j
java.io.*;
i *
import javax.servlet.*;
import
p jjavax.servlet.http.*;
p
public class GetCookiesServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException,
ServletException IOException {
// Get cookies from header of HTTP request.
Cookie[] cookies = request.getCookies();
// Display these cookies.
response.setContentType("text/html");
tC t tT ("t t/ht l")
PrintWriter pw = response.getWriter();
pw.println("<B>");
for(int i = 0; i < cookies.length; i++) {
String name = cookies[i].getName();
String value = cookies[i].getValue();
cookies[i] getValue();
pw.println("name = " + name +
"; value = " + value);
}
pw.close();
}
}
(Web.xml)
web app
<web-app>
<servlet>
<servlet-name>add</servlet-name>
<servlet-class>AddCookieServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>gcs</servlet-name>
<servlet-class>GetCookiesServlet</servlet-class>
se v e c ss Ge Coo esSe v e /se v e c ss
</servlet>
<servlet-mapping>
<servlet name>add</servlet name>
<servlet-name>add</servlet-name>
<url-pattern>/Add</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>gcs</servlet-name>
<url-pattern>/Getcs</url-pattern>
p p
</servlet-mapping>
</web-app>
9Follow the steps which are described in the firstservlet
example
p and then
1.Start Tomcat (if it is not already running)
2.Display the Web page (AddCookie.html) in a browser
9In
In some applications, it is necessary to save state
information so that information can be collected from
several interactions between a browser and a server