Академический Документы
Профессиональный Документы
Культура Документы
Before we can request a servlet, we need to map the servlet to a URL (Uniform
Resource Locator).
This is also known as URL mapping. This binding of URL (Uniform Resource Locator)
to servlet class is necessary
for web container to decide which servlet to activate.
URL :- http://ipaddress:portnumber/<url_pattern>
OR
http://domainname/<url_pattern>
url patterns :- always start with / (forward slash) and followed by pattern.
/product
/product/redmi
/email/*
/email/add
For mapping url to Servlet, there are two ways :-
-> With Servlet 3.0, we can use @WebServlet annotation to map a servlet
to one or more URL patterns.
-> To map a servlet to a single URL pattern, code the URL pattern
in parentheses that follow the @WebServlet annotation.
@WebServlet("/test")
or
@WebServlet(urlPatterns="/test")
@WebServlet(urlPatterns = {"/test123","/test123123"})
---------------------------------------------------------------------
<web-app>
<servlet>
<servlet-name> ...... </servlet-name>
<servlet-class> ...... </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> ...... </servlet-name>
<url-pattern> ....... </url-pattern>
</servlet-mapping>
</web-app>
-----------------------------------------------------------------------------------
a) init(ServletConfig config)
it is life cycle method of servlet that is used to initialse
the servlet and it invoked by web container only once after
instantiation of Servlet class. An object of ServletConfig is
also create by web container for each servlet object. This
object is passed to init method by web container.
c) destroy()
it invoked only once when servlet is clearing out from
memory or when web server is shutting down. It indicates that
servert is being destroyed.
d) getServletConfig()
returns object of ServletConfig class created by web container
for each servlet.
e) getServletInfo()
retuns a string containing information about servlet such as
servet name, servlet version, servlet summary, servlet
copyright info etc, declared by programmer.
-----------------------------------------------------------------
--------------------------------------------------------------
?name=value&name2=value2&name3=value3........
username=ravi&password=123456
Key points :-
-----------------------------------------------------------------------------------
-----
-----------------------------------------------------------------
HTTP Response Body :- carries data such as text, html, image data,
file data, etc......
PrintWriter pw = responseobject.getWriter();
-----------------------------------------------------------------
responseobject.setHeader("header-name", "header-value");
-----------------------------------------------------------------
ServletConfig :-
----------------
<servlet>
<servlet-name>.....</servlet-name>
<servlet-class>.....</servlet-class>
<init-parameter>
<param-name>.......</param-name>
<param-value>......</param-value>
</init-parameter>
</servlet>
ServletConfig sc = getServletConfig();
RequestDisptacher rd =
httpservletrequestobj.getRequestDispatcher(URL-PATTERN);
----------------------------------------------------------------
Redirecting to a Different URL
-------------------------------
responseobj.sendREdirect("url");
---------------------------------------------------------------
ServletContext :-
<web-app>
<context-parameter>
<param-name>.......</param-name>
<param-value>......</param-value>
</context-parameter>
</web-app>
ServletContext sc = getServletContext();
----------------------------------------------------------------
--------------------------------------------------------------
<error-page>
<error-code>404</error-code>
<location>/error.html</location>
</error-page>
OR
<error-page>
<exception-type>......</exception-type>
<location>/ErrorServlet</location>
</error-page>
AutoRefresh in Servlet:-
-----------------------
responseobject.setIntHeader("headername",integervalue);
responseobject.setIntHeader("refresh",timeinterval);