Initialization And Communication: Presenter: Deepika Nair

  • Uploaded by: Himanshu Srivastava
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Initialization And Communication: Presenter: Deepika Nair as PDF for free.

More details

  • Words: 914
  • Pages: 26
Initialization And Communication Initialization and Communication

Presenter: Deepika Nair

1

Initialization

• Servlet instance variables are initialized in init(). • The ServetConfig object is a configuration object used by a servlet container to pass information to a servlet during initialization. Most of the time this information will be required to initialize variables. • There are two ways to initialize: Override init(ServletConfig config) Override init()and get config object using getServletConfig() method(of Servlet interface) 8

ServletConfig

• This object is used to fetch the initialization parameters for individual servlets. • This object is passed on by the application server to the servlet during initialization when init(ServletConfig) method is called.

9

ServletConfig • Getting ServletConfig object: ServletConfig getServletConfig() method in Servlet interface • Methods to get init parameters: public String getInitParameter(String name) of ServletConfig

10

init param for a particular servlet <servlet> <servlet-name>servlet-name <param-name>param-name <param-value>param-value

In servlet servlet-name : String var-name=getInitParameter(“param-name”); 11

ServletContext

• This object is global to all servlets and jsps in an application context. • It is used to set and get variables that are global application variables. • It is also used by the servlet to get info about the servlet engine this servlet is running and info about other servlets.

13

ServletContext

Getting ServletContext • ServletContext getServletContext() method of HttpServlet or • Initialize an instance of cxt by overriding init(ServletContext) method.

14

Methods of ServletContext • String getInitParameter(String name) • void setAttribute(String name,Object obj) • Object getAttribute(String name) • void removeAttribute(String name)

15

Status 2xx

GET Request for servlet s1

s1

Client

Response packet HTTP/1.1 200 OK header … …
19

Server body

HTTP status code 2xx indicating the requested resource is available.

Status 4xx GET Request for servlet s2

s1

Client

Response packet HTTP/1.1 404 NOT FOUND header … Server body

20

HTTP status code 4xx indicating the requested resource is not available.

Status 5xx

doPost( ) not defined!

POST Request for servlet s1

s1

Client

Response packet HTTP/1.1 500 Internal Server Error … Server

HTTP status code 5xx server error. In this case the request is made for the method not defined in the servlet. 21

Status 3xx • HttpServletResponse has method called sendRedirect(String url) • This method sends a temporary redirect response to client with status code 3xx. Location specifies the URL page to which the redirection is going to be. • Usage: response.sendRedirect(“http://localh ost:8080/direct/s?name=‘Harry Potter’”)

22

HTTP status code 3xx indicating the redirect

public class S1{ … response.sendRedirect (“http://pondser/app2); … }

Request for servlet S1

S1

Client

Response packet HTTP/1.1 307 Temporary Redirect …

Request for http://pondser/app2

PondServer 23

Server

Inter Servlet Communication • Two servlets (or JSPs) can communicate with each other. • There are two ways in which the communicate can happen based on where the control is finally going to be: – forward – include

24

forward request response GET Request for servlet S1 S1

forward S2

Calls doGet() method of S2. If the request was for POST, then doPost() of S2 gets called. Control finally is in S2. Response is generated by S2 25

include

request response GET Request for servlet S1 S1

include S2

Calls doGet() method of S2. If the request was for POST, then doPost() of S2 gets called. Control finally is in S1. Response is generated by S1

26

javax.servlet.RequestDispatcher • RequestDispatcher is an interface that has methods to forward or include. • void forward(ServletRequest request, ServletResponse response) • void include(ServletRequest request, ServletResponse response. • RequestDispatcher instance is obtained through – ServletContext or ServletRequest methodRequestDispatcher getRequestDispatcher(String path) w.r.t. to or – ServletContext method RequestDispatcher getNamedDispatcher(String name) w.r.t. to <servlet-name> 27

Request Attributes

• Request and Response objects are shared between the servlets when they communicate. Needless to say that form parameters etc. will be accessible by both the servlets. • In addition to this, request object can be used to send additional data to the a servlet or a JSP while forwarding (or including). • HttpSessionRequest methods that allow this: – Object getAttribute(String name) – void setAttribute(String name , Object obj) – void removeAttribute(String name) 29

Example Register

Register

Name: Email:
On submitting this form, a servlet is invoked that checks for the validity of data. If the data is invalid, the same form is displayed with an error message, otherwise another servlet is invoked that displays a friendly message.

30

web.xml <web-app> Register <servlet> <servlet-name>Register <servlet-class>Register <servlet> <servlet-name>Success <servlet-class>Success <servlet> <servlet-name>Failure <servlet-class>Error

31

<servlet-mapping> <servlet-name>Register /register.do <servlet-mapping> <servlet-name>Success /success.do <servlet-mapping> <servlet-name>Failure /error.do 32

Register Servlet // assume imports public class Register extends HttpServlet { public void doPost(HttpServletRequest request,HttpServletResponse response) IOException, ServletException { PrintWriter out = response.getWriter();

throws

String name=request.getParameter("name"); String email=request.getParameter("email"); if( (name!=null && name.trim().length()!=0) && (email!=null && email.trim().length()!=0)) request.getRequestDispatcher("success.do").forward(request,res ponse);

33

else{ request.setAttribute("error","Name or email not entered"); request.getRequestDispatcher("error.do" ).include(request,response); request.getRequestDispatcher("index.htm l").include(request,response); } }}

34

Success Servlet //assume imports

public class Success extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { PrintWriter out = response.getWriter(); try{

response.setContentType("text/html"); out.println("Success "); 35

out.println(""); out.println("Thanks "+request.getParameter("name")+"" ); out.println("
For further details we will contact in the email-id "+request.getParameter("email")+""); out.println(""); }catch(Exception e){out.println(e.toString()); }} }

36

J2EE Application Servers support for Connection Pooling • Most application servers implement DataSource interface that supports connection pooling. • Therefore when you get connection form Data Source instance, you get it from the pool and when you close it, the connection goes back to the pool.

48

Related Documents


More Documents from "darshan"