Professional Documents
Culture Documents
Chapter 06 Servlet
Chapter 06 Servlet
PUT This is same as POST, but POST is used to create, PUT can be
used to create as well as update. It replaces all current
representations of the target resource with the uploaded content.
Mr. Nilesh Vishwasrao Patil
HTTP methods
GET Request POST Request
Data is sent in header to the Data is sent in the request body
server
Get request can send only Large amount of data can be
limited amount of data sent.
<param-name>dname </param-name>
<param-value> sun.jdbc.odbc.JdbcOdbcDriver
</param-value>
</context-param>
HttpServlet
It extends GenericServlet class and
implements Servlet, ServletConfig and
Serializable interface.
It provides http specific methods such as
doGet, doPost, doHead, doTrace etc.
HttpServlet
Create two servlet file, one will save user name into
session and that session information is send to another
servlet. This example shows the session tracking.
RequestDispatcher
The RequestDispatcher class enables your servlet to "call"
another servlet from inside another servlet.
We can obtain a RequestDispatcher from the
HttpServletRequest object.
Example:
Non-persistent cookie:
It is valid for single session only. It is removed each time
when user closes the browser.
Persistent cookie:
It is valid for multiple session . It is not removed each time
when user closes the browser. It is removed only if user
logout or sign-out or clear cookies/cache memory of
browsers.
Cookie: Pros/Cons
Advantages:
Simplest technique of maintaining the state.
Cookies are maintained at client side.
Disadvantages
It will not work if cookie is disabled from the browser.
Only textual information can be set in Cookie object.
Cookie: Constructor
javax.servlet.http.Cookie class provides the
functionality of using cookies. It provides a lot of
useful methods for cookies.
Constructor Description
Cookie() constructs a cookie.
Cookie(String name, String value) constructs a cookie with a specified
name and value.
Cookie: Methods
Useful methods:
Method Description
public void setMaxAge(int expiry) Sets the maximum age of the cookie in
seconds.
Cookie ck[]=request.getCookies();
for(int i=0;i<ck.length;i++)
{
out.print("<br>"+ck[i].getName()+" "+ck[i].getValue());
//printing name and value of cookie
}
Cookie: Example
Cookie: Example
Create one Html file which send user name to
first servlet.
First servlet file set cookies of that user name and
call second servlet file.
Second servlet file retrieve name of user from
cookies instead of from session.
MCQ
Which is of the following are classes and which are
interfaces?
1. Servlet
2. ServletConfig
3. ServletRequest
4. ServletResponse
5. HttpServlet
6. GenericServlet
7. Cookies
8. Session
MCQ
What is returntype of the getSession() method?
1. Session
2. int
3. HttpSession
4. boolean
5. void
MCQ
Javax.servlet packages does not have:
1. HttpServlet
2. ServletConfig
3. ServletContext
4. Servlet
5. HttpServletRequest
6. ServletResponse
7. HttpServletResponse
8. Cookies
MCQ
Javax.servlet packages does not have:
1. HttpServlet
2. ServletConfig
3. ServletContext
4. Servlet
5. HttpServletRequest
6. ServletResponse
7. HttpServletResponse
8. Cookies
MCQ
Which is correct package for HttpServlet and
HttpServletResponse?
1. javax.servlet.*;
2. javax.servlet.http.*;
3. javax.servlet.httpservlet.*;
4. java.lang.*;
MCQ
Which of the following method is invoked when Http
post request?
1. doPost()
2. doPostCall()
3. doHttpPost()
4. doPut()
5. doTrace()
6. doPostOptions()
MCQ
Which of the following method is invoked when Http
post request?
1. doPost()
2. doPostCall()
3. doHttpPost()
4. doPut()
5. doTrace()
6. doPostOptions()