Comp2513 java servlet basics
This presentation is the property of its rightful owner.
Sponsored Links
1 / 19

Comp2513 Java Servlet Basics PowerPoint PPT Presentation


  • 109 Views
  • Uploaded on
  • Presentation posted in: General

Comp2513 Java Servlet Basics. Daniel L. Silver, Ph.D. Objectives. To introduce the basic concepts of Java Servlets To discuss FORMs and Java Servlets Review more complex servlets Reference: DDEA Ch.7, Sharma p.110-122 and EJP (Ch.4) p.48-63. The Problems with Applets and CGI.

Download Presentation

Comp2513 Java Servlet Basics

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Comp2513 java servlet basics

Comp2513Java Servlet Basics

Daniel L. Silver, Ph.D.


Objectives

Objectives

  • To introduce the basic concepts of Java Servlets

  • To discuss FORMs and Java Servlets

  • Review more complex servlets

  • Reference: DDEA Ch.7, Sharma p.110-122 and EJP (Ch.4) p.48-63

Daniel L. Silver


The problems with applets and cgi

The Problems with Applets and CGI

  • Java Applets have three major drawbacks:

    • Take time to load onto client

    • May not work as planned (depends on JVM)

    • Security risk for client

  • Server-side code is preferred for business logic

  • CGI allows an application to run on server but creates server performance problems

  • Most notably each time a separate process must be spawned (for Java a separate JVM is run)

Daniel L. Silver


Enter servlets

Enter Servlets

  • Servlets overcome this problem

  • Servlets relie on a Servlet Engine (Application Server) to manage multiple requests for the same application

  • Java servlets have the advantages of Java applets but run on the server side

  • The Jserv engine from SUN was the first Java servlet engine

Daniel L. Silver


Java servlet technology

Java Servlet Technology

  • Applications run on the server.

  • Extend functionality of a web server and provide structure for a business environment.

  • Servlets can be operating system and hardware platform independent.

  • Servlets process HTTP/HTML requests with all the benefits of the mature Java language (portability, performance, reusability, and crash protection.)

Daniel L. Silver


Applet vs servlet

Applet

Client side.

Takes time to load.

JVM varies with browser.

Require compatible browser

Security issue if client side program needs to access sensitive data via browser.

Servlet

Server side.

Runs on request.

Constant JVM

No GUI required, can generate HTML, Javascript, Applet code

Server side programming and data access preferred for business applications.

Applet vs. Servlet

Daniel L. Silver


Cgis vs servlets

CGI programs

Separate process for each CGI program

Mod_perl and FastCGI improves performance of CGI but not to level of servlets

Have difficult time maintaining state across requests

Servlets

Run under single JVM (better performance)

Servlets loaded into memory with first call, and stay in memory

Have built in state preservation methods

Java's inherent security

Proprietary source code can be retained by only giving up *.class files to the server

CGIs vs. Servlets

Daniel L. Silver


Websphere java servlet request processing

Websphere Java ServletRequest Processing

Client

http://eagle.acadiau.ca/demo/servlet/HelloWorld

Browser

Tomcat

App. Server

HTTP

Server

HTML

Internet

JVM

servlet/HelloWorld

HelloWorld.class

demo/servlet/ equates to

…/demo/WEB-INF/classes/HelloWorld.class

Daniel L. Silver


Servlet life cycle

Servlet Life-Cycle

  • The servlet is initialized and is loaded into the servers memory

  • The servlet resides in memory and responds to requests from clients

  • The servlet is destroyed (by the server engine)

    NOTE: When you update a servlet, Tomcat checks the modification date of the .class file and if it is more recent then the running version, the servlet is destroy and then re-initialize

Daniel L. Silver


Fundamental parts of a servlet

Fundamental parts of a Servlet

1. import javax.servlet.*; and import javax.servlet.http.*;

- packages of servlet classes that implement the Java Servlet API

2. public class HelloWorld extends HttpServlet {

- extends the HTTPServlet class

3. init()

-intializes servlet after loading into memory

- place to perform operations done only once at start-up

- reading a current properties

- clearing log files, notifying other services that the servlet is running

4. service(), doGet(), doPost()

- this is where work is done

- each time the servlet is called a new thread of execution begins

- input is passed to the Java code via either HTTP GET or POST commands

5. destoy()

- executed when server engine calls servlet to terminate

- used to flush I/O, disconnect from database

Daniel L. Silver


Structure of a servlet helloworld

Structure ofa Servlet:HelloWorld

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws IOException, ServletException

{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<html>");

out.println("<body>");

out.println("<head>");

out.println("<title>Hello World!</title>");

out.println("</head>");

out.println("<body>");

out.println("<h1>Hello World!</h1>");

out.println("</body>");

out.println("</html>");

}

}

Daniel L. Silver


Some basic java servlets

Some Basic Java Servlets

  • Several examples …

    http://eagle.acadiau.ca/demo/servletexample.html

    http://eagle.acadiau.ca:8080/examples/servlets/index.html

    http://www.servlets.com/jservlet2/examples/index.html

  • Java Developers Almanac:

    http://javaalmanac.com/egs/javax.servlet/pkg.html

Daniel L. Silver


Forms and java servlets

FORMS and Java Servlets

  • Within HTML code on the client: A FORM tag similar to CGI is used to pass data from a browser to a Java servlet:

    <FORM ACTION = 'http://eagle/store35/servlet/ShowFormVariables' METHOD=‘GET'>

    Enter your first name <INPUT TYPE=text NAME='firstName' VALUE=' '><br>

  • This will cause the web browser to generate a URL (if “Danny” is entered):

    http://eagle/demo/servlet/ShowFormVariables?firstName=Danny

Daniel L. Silver


Forms and java servlets1

FORMS and Java Servlets

  • Within Java code on the server: The doGet servlet method is used to obtain the two objects:

    • HttpServletRequest and HttpServletResponse

  • HttpServletRequest is used to get the INPUT parameter names and values:

    • getParameterNames()

    • getParameterValues()

  • HttpServletResponse used to return HTML response

    • printlin()

Daniel L. Silver


An example of a java servlet handling form input

An Example of a Java Servlet handling FORM input

  • From page 117 of Sharma – servlet that returns browser input passed via a FORM tag

    http://eagle.acadiau.ca/danstech/ShowFormVariables.html

  • Here is the java source code :

    http://eagle/danstech/ShowFormVariables.java

Daniel L. Silver


A more complex servlet example

A more complex Servlet Example

  • AddToShopppingCart.java from your store provides a larger and more complex servlet example

  • Also see ClearShoppingCart.java

Daniel L. Silver


Problems with servlets

Problems with Servlets ?

  • Do you see any problems with servlets from an E-Commerce perspective?

Daniel L. Silver


Additional web info

Additional Web Info

  • http://java.sun.com/products/servlet/

  • http://developer.java.sun.com/developer/onlineTraining/Servlets/Fundamentals/

  • http://hotwired.lycos.com/webmonkey/99/07/index3a.html?tw=programming

  • http://www.servlets.com/jservlet2/index.html

  • http://www.servlets.com/jservlet2/examples/index.html

Daniel L. Silver


The end danny silver@acadiau ca

THE [email protected]


  • Login