j2ee t echnologies these are the technologies required to build large scale distributed...

Post on 19-Jan-2016

213 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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.

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.

SERVLET MODEL

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

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

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

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");

………………

}

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>");

}

SERVLETCONFIG AND SERVLETCONTEXT

11

ServletContext

MyServlet1 MyServlet2 MyServlet3

ServletConfig ServletConfig ServletConfig

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

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

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>smcafaculty@thapar.edu</param-value>

</init-param>

</servlet>

FIRSTSERVLET.JAVA

//Code to get init parameters from Web.xml

Private String mailid;

public void init(ServletConfig config) throws ServletException

{

mailid=config.getInitParameter("ID");

}

CONTEXT PARAMATERS

<servlet>

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

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

</servlet>

<context-param>

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

<param-value>smca@thapar.edu</param-value>

</context-param>

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.

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.

SOLUTION:

Redirect Request to different URL

Dispatch Request to some other component

in web-app

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.

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.

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);

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.

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.

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)

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”);

ATTRIBUTE API

Object getAttribute(String name)

Void setAttribute(String name,Object Value)

Void removeAttribute(String name)

Enumeration getAttributeNames()

ARCHITECTURE STYLE FOR WEB APP

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)

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.

CASE STUDY

top related