33
J2EE TECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into Component Technologies eg . Servlets, JSP, Beans Service Technologies eg. JDBC Communication Technologies HTTP, TCP/IP, RMI

J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

Embed Size (px)

Citation preview

Page 1: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

J2EE TECHNOLOGIES These are the technologies required to build large

scale distributed applications, can be divided into – Component Technologies eg . Servlets, JSP, Beans– Service Technologies eg. JDBC– Communication Technologies HTTP, TCP/IP, RMI

Page 2: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

WEB CONTAINER Servlets don’t have main method. They are under

control of another Java Application called a Container.

Eg When a Socket Connection is made,

we create socket, identify port to listen request, create streams etc. But container already knows protocol between web server and itself.

Page 3: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

SERVLET DEFINITION A Servlet, in simple terms, is a Java program running

under a web server taking a 'request' object as an input and responding back by a 'response' object.

Typically a web browser will send the request in HTTP format. The Servlet container will convert that into a request object. Similarly the response object - populated by the Servlet is converted into an HTTP response by the Servlet container.

Page 4: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

SERVLET MODEL

Page 5: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

BASIC SERVLET STRUCTURE

Import javax.servlet.*;Import java.io.*;/** * Servlet implementation class FirstServlet */public class FirstServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stubPrintWriter out = response.getWriter();response.setContentType("text/html");try{out.println("<html><body>HelloWorld</body></html>");

}finally {out.close();

}

}

}

5

Page 6: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

WEB.XML<MAPPING SERVLETS TO URL><web-app>

maps internal name to fully qualified name<servlet>

<servlet-name>URL</servlet-name><servlet-class>Hello</servlet-class>

</servlet>maps internal name to Public URL name<servlet-mapping>

<servlet-name>URL</servlet-name><url-mapping>/URL</url-mapping>

</servlet-mapping></web-app>

6

Page 7: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

BENEFITS OF MAPPING

Mapping Servlet Names improves application’s flexibility and security.

The Deployment Descriptor provides “declarative” mechanism for customizing web applications without touching Source Code

Page 8: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg
Page 9: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

REQUEST PARAMETERS

When a request is sent from Jsp Page to Servlet, Parameters set in form are also sent in Request Body for Post Method.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{

PrintWriter out= response.getWriter();

String Name=request.getParameter("name");

String rollno=request.getParameter("rollno");

………………

}

Page 10: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

Some form input types,like set of CheckBoxes can have multiple selections.To view all selections, getParamterValues(String a) can be used.

String[] courses =request.getParameterValues("course");

for(int x=0;x<courses.length;x++)

{

out.println("Course :" + courses[x]);

out.println("<br>");

}

Page 11: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

SERVLETCONFIG AND SERVLETCONTEXT

11

ServletContext

MyServlet1 MyServlet2 MyServlet3

ServletConfig ServletConfig ServletConfig

Page 12: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

SERVLETCONFIG

One ServletConfig object per servlet

Provided to a servlet upon initialization by the web

server (container)

Can also access ServletContext

Parameters are configured in Deployment Descriptor

Page 13: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

SERVLETCONTEXT

One ServletContext per web-app

Use it to access web-app parameters

Can be used as a global data store (like an

application-wide session)

Parameters are configured in Deployment

Descriptor

Page 14: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

INIT PARAMETERS

Web.xml<servlet>

<servlet-name>FirstServlet</servlet-name>

<servlet-class>classes.FirstServlet</servlet-class>

<init-param>

<param-name> ID</param-name>

<param-value>[email protected]</param-value>

</init-param>

</servlet>

Page 15: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

FIRSTSERVLET.JAVA

//Code to get init parameters from Web.xml

Private String mailid;

public void init(ServletConfig config) throws ServletException

{

mailid=config.getInitParameter("ID");

}

Page 16: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

CONTEXT PARAMATERS

<servlet>

<servlet-name>FirstServlet</servlet-name>

<servlet-class>classes.FirstServlet</servlet-class>

</servlet>

<context-param>

<param-name>mailid</param-name>

<param-value>[email protected]</param-value>

</context-param>

Page 17: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

SERVLET COLLABORATION

In a web application, a servlet receives an HTTP request,executes some application logic, and prepares a response but sometimes servlet dosen’t send response.

For Below Scenarios:

A servlet receives an http request from a client,process

application logic and a JSP page drives the response. In

this case,JSP is responsible for dynamic content.

Page 18: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

Second Scenario:

A servlet receives request, processes application logic

partially, and hands over response to another

servlet.The second servlet completes the application

logic , and either prepares the response or request a

JSP to drive the response.

Note: In both scenarios, servlet which receives request

dosen’t deal with response itself.

Page 19: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

SOLUTION:

Redirect Request to different URL

Dispatch Request to some other component

in web-app

Page 20: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

REDIRECT

Servlet Redirect makes the browser do the work.

public void sendRedirect(String Location) Client receives the HTTP response code 302 indicating

that temporarily the client is being redirected to specified location.

Page 21: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

REDIRECT RESPONSE Steps to redirect:

a) Client request for a URL in request:

http://localhost:8080/ExampleCLASS/StudentForm.jsp

b) Servlet logic decides that request should go to a completely different URL

c) Servlet calls sendRedirect(String) on response and sends response to

client.

d) Http response has status code “302” and a “location” header with new

URL.

e) Browser get response ,sees 302 status code and looks for “location”

header.

Page 22: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg
Page 23: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

REQUEST DISPATCH Request Dispatcher works on Server-side.

Steps:

a) User types URL into browser

b) Request goes to container which transfer request to servlet

c) Servlet logic decides that request should go to other web

component eg. “.jsp” page

d) Servlet calls

RequestDispatcher rd=

request.getRequestDispatcher("result.jsp");

rd.forward(request, response);

Page 24: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

REQUEST DISPATCH

forwardOnce you forward the request from say Servlet A to any other Servlet/JSP control gets transferred from Servlet A to forwarded patrty & it never returns back to A for that request.IncludeIn include what you are doing is if Servlet A(Above example) is including the response of other Servlet/JSP(say B or B.jsp) so momentarily Control goes to B or B.jsp (they will genrate the response) control comes back to A & generated response is added in A's Response.

Page 25: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

ATTRIBUTES

An attribute is an object set in three other servlet API objects:

-Servlet Context

-HttpServletRequest

-HttpSession

Attribute is a name/value pair where name is String and Value is an Object.

Page 26: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

SCOPE OF ATTRIBUTES

Context : Everyone in Application has Access

Request : Accessible only to specific servlet request

Session : Accessible to components in specific

session

Method to set Attribute on a Servlet:

getServletContext().setAttribute(String Name,Object value)

request.setAttribute(String name,Object value)

request.getSession().setAttribute(String name, Object value)

Page 27: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

Method to get Attributes:

- request.getAttribute(String name)

- getServletContext().getAttribute(String name)

- request.getSession().getAttribute(String name)

Return Type of getAttribute(String) :Object

eg

Student st= request.getAttribute(“StudentName”);

Page 28: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

ATTRIBUTE API

Object getAttribute(String name)

Void setAttribute(String name,Object Value)

Void removeAttribute(String name)

Enumeration getAttributeNames()

Page 29: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

ARCHITECTURE STYLE FOR WEB APP

Page 30: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

MVC

MVC stands for Model-View-Controller The Model is the actual internal

representation of data (Java Classes) The View (or a View) is a way of looking at or

displaying the model ( JSP) The Controller provides for user input and

modification (Servlets)

Page 31: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

ADVANTAGES OF MVC

Motivation behind MVC approach is the desire to

separate the code that creates and manipulates from

the code that represents the data.

Page 32: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg

CASE STUDY

Page 33: J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg