24
A seminar on j2ee by saritha. s

A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

Embed Size (px)

Citation preview

Page 1: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

A seminar on j2eeby

saritha. s

Page 2: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

What is J2EE

• J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing typical of large enterprises. Sun Microsystems (together with industry partners such as IBM) designed J2EE to simplify application development in a thin client tiered environment. J2EE simplifies application development and decreases the need for programming and programmer training by creating standardized, reusable modular components and by enabling the tier to handle many aspects of programming automatically.

Page 3: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

Definition of a Servlet

• A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model

• The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. All servlets must implement the Servlet interface, which defines life-cycle methods.

Page 4: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

Life cycle of a servlet

Page 5: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

Signature of the life cycle methods

public void init(ServletConfig config) throws ServletException<br />

public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException<br />

public void destroy()

Page 6: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

Directory structure of servlet application

Page 7: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

HTML file• Copy the following code into form.html file and save it under servlet-example/pages

directory. • <html>• <head>• <title>The servlet example </title>• </head>• <body>• <h1>A simple web application</h1>• <form method="POST" action="WelcomeServlet">• <label for="name">Enter your name </label>• <input type="text" id="name" name="name"/><br> <br>• <input type="reset" value="Reset Form"/>• </form>• </body>• </html>

Page 8: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

Servlet class• import javax.servlet.ServletConfig;• import javax.servlet.ServletException;• import javax.servlet.http.HttpServlet;• import javax.servlet.http.HttpServletRequest; • import javax.servlet.http.HttpServletResponse; • public class WelcomeServlet extends HttpServlet { • @Override public void init(ServletConfig config) throws ServletException

{ super.init(config);• } • protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException• {• /* * Get the value of form parameter */• String name = request.getParameter("name");• String welcomeMessage = "Welcome "+name;• /*

Page 9: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

• * Set the content type(MIME Type) of the response.• */• response.setContentType("text/html"); • PrintWriter out = response.getWriter();• /*• * Write the HTML to the response• */ • out.println("<html>");• out.println("<head>");• out.println("<title> A very simple servlet example</title>"); out.println("</head>");• out.println("<body>"); • out.println("<h1>"+welcomeMessage+"</h1>"); • out.println("<a href="/servletexample/pages/form.html">"+"Click here to go back to input page "+"</a>");

out.println("</body>"); out.println("</html>"); out.close(); • } • public void destroy() {• • }• }

Page 10: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

Deployment descriptor file• Copy the following code into web.xml file and save it directly under servlet-example/WEB-INF

directory. • <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

• <servlet>• <servlet-name>WelcomeServlet</servlet-name>• <servlet-class>jsptube.tutorials.servletexample.WelcomeServlet• </servlet-class>• </servlet>• <servlet-mapping>• <servlet-name>WelcomeServlet</servlet-name>• <url-pattern>/WelcomeServlet</url-pattern> • </servlet-mapping> • <welcome-file-list> <welcome-file> /pages/form.html </welcome-file> </welcome-file-list> • </web-app>

Page 11: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

Create the War file• Create the WAR (Web application archive) file.• Web applications are packaged into a WAR (web application archive). There are

many different ways to create a WAR file. You can use jar command, ant or an IDE like Eclipse. This tutorial explains how to create the WAR file using jar tool.

• Open the command prompt and change the directory to the servlet-example directory, and execute the following command.

• jar cvf servletexample.war * • This command packs all the contents under servlet-example directory, including

subdirectories, into an archive file called servletexample.war.We used following command-line options:

• -c option to create new WAR file.• -v option to generate verbose output.• -f option to specify target WAR file name.• You can use following command to view the content of servletexample.war file. • Jar –tvf servletexample.war. This command lists the content of the WAR file.

Page 12: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

Deploying the application to tomcat web container.

• Deployment steps are different for different J2EE servers. This tutorial explains how to deploy the sample web application to tomcat web container. If you are using any other J2EE server, consult the documentation of the server.

• A web application can be deployed in tomcat server simply by copying the war file to <TOMCAT_HOME>/webapp directory.Copy servletexample.war to <TOMCAT_HOME>/webapp directory. That’s it! You have successfully deployed the application to tomcat web server. Once you start the server, tomcat will extract the war file into the directory with the same name as the war file.

• To start the server, open the command prompt and change the directory to <TOMCAT_HOME/bin> directory and run the startup.bat file.

• Our sample application can be accessed at http://localhost:8080/servletexample/. If tomcat server is running on port other than 8080 than you need to change the URL accordingly.

• If the application has been deployed properly, you should see the screen similar to below when you open the application in browser.

Page 13: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing
Page 14: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

• Enter your name in text box and click on submit button. It will display welcome message with your name as shown in below figure.

Page 15: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing
Page 16: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

Disadvantages of servlet• There are some disadvantages of writing pure servlet in view of

developer, these are some of are listed below.• Servlet is a mixture of java skills and web related HTML skills, because

you have to write the business logic in java and for presentation you should the HTML, so the role based development is missing in pure servlet. The developer who is writing servlet should know java and HTML both.

• If your application is build on using servlet technology, it is very difficult for enhancement and bug fixing.

• The servlet technology require more steps to develop, servlet require too long time for development.

• To overcome these above disadvantages java people introduce JSP technology. Actually the JSP is finally a Servlet. Using JSP you can achieve the role based development.

Page 17: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

Definition and Features of JSP

• JavaServer Pages (JSP) technology allows you to easily create Web content that has both static and dynamic components. JSP technology projects all the dynamic capabilities of Java Servlet technology but provides a more natural approach to creating static content.

The main features of JSP technology are • A language for developing JSP pages, which are text-based

documents that describe how to process a request and construct a response

• Constructs for accessing server-side objects • Mechanisms for defining extensions to the JSP language

Page 18: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

Life Cycle Of JSP Initialization:• jspInit() method is called immediately after the instance was created. It is

called only once during JSP life cycle. _jspService() :• This method is called for every request of this JSP during its life cycle. This

is where it serves the purpose of creation. Oops! it has to pass through all the above steps to reach this phase. It passes the request and the response objects. _jspService() cannot be overridden.

jspDestroy() :• This method is called when this JSP is destroyed. With this call the servlet

serves its purpose and submits itself to heaven (garbage collection). This is the end of jsp life cycle.

• jspInit(), _jspService() and jspDestroy() are called the life cycle methods of the JSP.

Page 19: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

Example Application on JSP• <HTML>

<%@ page language="java" imports="java.util.*" %>

<H1>Welcome</H1>

<P>Today is </P> <jsp:useBean id="clock" class="jspCalendar" /> <UL> <LI>Day: <%= clock.getDayOfMonth() %> <LI>Year: <%= clock.getYear() %> </UL> <%-- Check for AM or PM --%> <%! int time = Calendar.getInstance().get(Calendar.AM_PM); %> <% if (time == Calendar.AM) { %> Good Morning <%} else { %> Good Afternoon <%} %> <%@ include file="copyright.html" %> </HTML>

Page 20: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

Description about jsp tags JSP actions (or JSP tags) perform a variety of functions and extend the

capabilities of JSP. JSP actions use XML-like syntax, and are used to (among other things) manage JavaBean components. In the sample page, a jsp:useBean action initializes a JavaBean that is used in subsequent portions of the page:

• Ex:<jsp:useBean id=="clock" class=="jspCalendar" /> Directives are instructions that are processed by the JSP engine when the

page is compiled to a servlet. Directives are used to set page-level instructions, insert data from external files, and specify custom tag libraries. Directives are defined between <%@ and %>. In the above example, directives define the language of the page (Java), import the Java classes needed by the embedded code, and specify that the contents of an HTML file should be inserted at the bottom of the page:

• Ex:<%@ page language=="java" imports=="java.util.*" %> <%@ include file=="copyright.html" %>

Page 21: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

Declarations are similar to variable declarations in Java, and define variables for subsequent use in expressions or scriptlets. Declarations are defined between <%! and %>. In the above sample page an int is declared and given a value corresponding to the time of day (AM or PM):

• ex:<%! int time = Calendar.getInstance().get(Calendar.AM_PM); %>

Expressions are variables or constants that are inserted into the data returned by the web server, and are defined with the <%= and %>. In the sample page, expressions make calls on a JavaBean component and insert the resulting data into the page:

• Ex:<%= clock.getDayOfMonth() %> <%= clock.getYear() %>

Page 22: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

Scriptlets are blocks of Java code embedded within a JSP page. Scriptlet code is inserted verbatim into the servlet generated from the page, and is defined between <% and %>. A scriptlet in the above sample determines the time of day and greets the user accordingly:

• Ex:<% if (time == Calendar.AM) { %> Good Morning <%} else { %> Good Afternoon <% } %>

Page 23: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

• ANY QUERIES

Page 24: A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing

• THANK U.