Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg."— Presentation transcript:

1 J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg. Servlets, JSP, Beans – Service Technologieseg. JDBC – Communication TechnologiesHTTP, TCP/IP, RMI

2 W EB C ONTAINER 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.

3 S ERVLET D EFINITION 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.

4 S ERVLET M ODEL

5 B ASIC S ERVLET S TRUCTURE 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 stub PrintWriter out = response.getWriter(); response.setContentType("text/html"); try { out.println(" HelloWorld "); } finally {out.close(); } 5

6 WEB. XML maps internal name to fully qualified name URL Hello maps internal name to Public URL name URL /URL 6

7 B ENEFITS OF M APPING Mapping Servlet Names improves application’s flexibility and security. The Deployment Descriptor provides “declarative” mechanism for customizing web applications without touching Source Code

8

9 R EQUEST P ARAMETERS 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"); ……………… }

10 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(" "); }

11 S ERVLET C ONFIG AND S ERVLET C ONTEXT 11 ServletContext MyServlet1MyServlet2MyServlet3 ServletConfig

12 S ERVLET C ONFIG 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

13 S ERVLET C ONTEXT 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

14 I NIT P ARAMETERS Web.xml FirstServlet classes.FirstServlet ID smcafaculty@thapar.edu

15 F IRST S ERVLET. JAVA //Code to get init parameters from Web.xml Private String mailid; public void init(ServletConfig config) throws ServletException { mailid=config.getInitParameter("ID"); }

16 C ONTEXT P ARAMATERS FirstServlet classes.FirstServlet mailid smca@thapar.edu

17 S ERVLET C OLLABORATION 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.

18 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.

19 S OLUTION : Redirect Request to different URL Dispatch Request to some other component in web-app

20 R EDIRECT 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.

21 R EDIRECT R ESPONSE 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.

22

23 R EQUEST D ISPATCH 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);

24 R EQUEST D ISPATCH forward Once 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. Include In 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.Servlet

25 A TTRIBUTES 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.

26 S COPE OF A TTRIBUTES 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)

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

28 A TTRIBUTE API Object getAttribute(String name) Void setAttribute(String name,Object Value) Void removeAttribute(String name) Enumeration getAttributeNames()

29 A RCHITECTURE S TYLE FOR W EB A PP

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

31 A DVANTAGES OF MVC Motivation behind MVC approach is the desire to separate the code that creates and manipulates from the code that represents the data.

32 C ASE S TUDY

33


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

Similar presentations


Ads by Google