Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 A seminar on j2ee by saritha. s

2 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. platformmainframeenterprisethin clienttier

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

4 Life cycle of a servlet

5 Signature of the life cycle methods  public void init(ServletConfig config) throws ServletException  public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException  public void destroy()

6 Directory structure of servlet application

7 HTML file Copy the following code into form.html file and save it under servlet- example/pages directory. The servlet example A simple web application Enter your name

8 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; /*

9 * 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(" "); out.println(" A very simple servlet example "); out.println(" "); out.println(" "); out.println(" "+welcomeMessage+" "); out.println(" "+"Click here to go back to input page "+" "); out.println(" "); out.println(" "); out.close(); } public void destroy() { }

10 Deployment descriptor file Copy the following code into web.xml file and save it directly under servlet-example/WEB-INF directory. WelcomeServlet jsptube.tutorials.servletexample.WelcomeServlet WelcomeServlet /WelcomeServlet /pages/form.html

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

12 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 /webapp directory. Copy servletexample.war to /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 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.

13

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

15

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

17 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

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

19 Example Application on JSP Welcome Today is Day: Year: Good Morning Good Afternoon

20 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:  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. 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:

21  Declarations are similar to variable declarations in Java, and define variables for subsequent use in expressions or scriptlets. Declarations are defined between. In the above sample page an int is declared and given a value corresponding to the time of day (AM or PM): ex:  Expressions are variables or constants that are inserted into the data returned by the web server, and are defined with the. In the sample page, expressions make calls on a JavaBean component and insert the resulting data into the page: Ex:

22  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. A scriptlet in the above sample determines the time of day and greets the user accordingly: Ex: Good Morning Good Afternoon

23 ANY QUERIES

24 THANK U.


Download ppt "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."

Similar presentations


Ads by Google