Servlet
Servlet
Servlet
CO 5 Building enterprise level applications and manipulate web databases using JDBC K3, K4
CO6 Design interactive web applications using Servlets and JSP K2, K3
Subject:Web
Subject: Web Technology
Technology
Syllabus
Unit-5
Servlets: Servlet Overview and Architecture, Interface Servlet and the
Servlet Life Cycle, Handling HTTP get Requests, Handling HTTP post
Requests, Redirecting Requests to Other Resources, Session Tracking,
Cookies, Session Tracking with Http Session
Java Server Pages (JSP): Introduction, Java Server Pages Overview, A First
Java Server Page Example, Implicit Objects, Scripting, Standard Actions,
Directives, Custom Tag Libraries.
Subject:Web
Subject: Web Technology
Technology
Static Web Page Processing
Throws:
HttpServlet's doGet req - res - java.io.IOException - if an input or
method is getting an HttpServletRequest obje an HttpServletResponse output error is detected when the
overridden. ct that contains the object that contains servlet handles the GET request.
request of the client the response the servlet ServletException - if the request for the
sends back GET could not be handled. It Defines a
general exception a servlet can throw
when it encounters difficulty.
out.println("<HTML><HEAD>
<TITLE>Hello World!</TITLE>"+ In these lines we use the PrintWriter to write the text of type
"</HEAD><BODY>Hello World!</BODY> text/html (as specified through the content type).
</HTML>");
1. Loading of Servlet
2. Creating instance of Servlet
3. Invoke init() once
4. Invoke service() repeatedly for each
client request
5. Invoke destroy()
GET POST
In GET method, values are visible in the URL. In POST method, values are not visible in the URL.
GET has a limitation on the length of the values,
POST has no limitation on the length of the values
generally 255 characters. since they are submitted via the body of HTTP.
GET performs are better compared to POST It has lower performance as compared to GET
because of the simple nature of appending the method because of time spent in including POST
values in the URL. values in the HTTP body.
This method supports different data types, such
This method supports only string data types.
as string, numeric, binary, etc.
GET results can be bookmarked. POST results cannot be bookmarked.
GET request is often cacheable. The POST request is hardly cacheable.
GET Parameters remain in web browser history. Parameters are not saved in web browser history.
Observe that:
• The URL http://localhost:8080/6_HttpRequest/add is retrieved from the attribute action="add" of the <form> start-tag.
• A '?' follows the URL, which separates the URL and called query string. Notice the "query string" ?first=10&second=15.
• This part contains two parameters with parameter values:
first=10
Subject:
second=15Web Technology
Servlet for handling HTTP GET request
Writing a Servlet to Process Form Data
• A name and a value is separated using an equal = sign, a parameter name/value pair is separated
from another parameter using the ampersand(&).
• When the user clicks the hyperlink, the parameter name/value pairs will be passed to the server.
From a Servlet, we can use getParameter() method to obtain a parameter value.
index.html
<form action="servlet1">
Name:<input type="text" name="userName"/><br/>
<input type="submit" value="go"/>
</form>
String n=request.getParameter("userName");
out.print("Welcome "+n);