Java Servlets and Java Server Pages Norman White Stern School of Business.

29
Java Servlets and Java Server Pages Norman White Stern School of Business

Transcript of Java Servlets and Java Server Pages Norman White Stern School of Business.

Page 1: Java Servlets and Java Server Pages Norman White Stern School of Business.

Java Servlets and Java Server Pages

Norman WhiteStern School of Business

Page 2: Java Servlets and Java Server Pages Norman White Stern School of Business.

Agenda

• Servlets• JSPs• J2EE Architecture• EJBs

Page 3: Java Servlets and Java Server Pages Norman White Stern School of Business.

But first, Applets

• Java Applet– Self contained Java program that is downloaded

and run under browser– Java

• Object oriented language similar to C++– Supports classes, inheritance etc.

• Compiled to “Java Virtual Machine” (JVM) object code• JVM can be implemented I many environments• Java originally designed as a machine control

language• Java Applets run under Browser with security features

that restrict what they can do (like read hard drive)

Page 4: Java Servlets and Java Server Pages Norman White Stern School of Business.

What are Java Servlets

• Java Servlets are essentially server side applets (I.e. run on a server as opposed to under the browser).

• Since they run on the server, their output is sent directly to the browser, hence they generate the web page output dynamically

Page 5: Java Servlets and Java Server Pages Norman White Stern School of Business.

Servlets

• Servlets are Java classes managed by a servlet container hosted by a Web server.

• Servlets are mapped to URLs by the servlet container. HTTP requests to a servlet's URL are passed to an instance of the servlet, which generates response content that is returned to the client as the HTTP response.

• Servlets receive input variables from the HTTP request, can maintain state across invocations, and have control of the content type the server returns. Servlets provide a portable, safe, efficient, and highly general mechanism for extending a Web server.

Page 6: Java Servlets and Java Server Pages Norman White Stern School of Business.

Java Server Pages

• Servlets are powerful, but they require programming. JSP pages provide a "structural" way to create dynamic textual content, as an alternative to the "programmatic" method offered by servlets.

• A JSP page is an HTML page with special markup that produces dynamic content. – I.e. embedded scripting statements which are

executed on the server , not the client.

Page 7: Java Servlets and Java Server Pages Norman White Stern School of Business.

JSPs

• Although a JSP page's source code looks like HTML, it's actually implemented by a

servlet. When a client accesses a JSP page, the Web server translates the JSP page into a Java language servlet, compiles and loads the servlet, and then passes the request to the servlet. (If the servlet is already loaded, the server skips the translation, compilation, and loading.) So, a JSP page is simply an easy way to specify a servlet that produces mostly structured data.

Page 8: Java Servlets and Java Server Pages Norman White Stern School of Business.

JSP Custom Tags

• But even JSP demands that the page developer knows some Java

• JSP developers can use JSP to define new XML (eXtensible Markup Language)

tags for non-technical users to use in JSP pages.

• The JSP custom tags are expanded into JSP Scriptlets and then executed.

Page 9: Java Servlets and Java Server Pages Norman White Stern School of Business.

But Wait, doesn’t this involve a lot of overhead

• Answer, yes but…• Trick is, JSP pages are only turned

into Servlets when they change.• Non-technical developers can

make changes at the JSP Custom Tag level, and they automatically get translated into JSP scriptlets, and then the whole page is translated into a Servlet.

Page 10: Java Servlets and Java Server Pages Norman White Stern School of Business.

I Am Completely Confused!!

This is way too complex…

• But that is why you get the big bucks…

Page 11: Java Servlets and Java Server Pages Norman White Stern School of Business.

Advantages over CGI

• Efficient– Less process startup overhead with Servlets, they stay in

memory for reuse.

• Convenient– Built-in support for HTML forms, cookies etc. Written in

Java

• Powerful– Can talk directly to web server, share data, make Data

base connections, save information , track sessions etc.

• Portable- – Write once, run anywhere. Great for a mixed environment

• Inexpensive– Inexpensive to add servlet support

Page 12: Java Servlets and Java Server Pages Norman White Stern School of Business.

Examples

• import java.io.*;• import javax.servlet.*;• import javax.servlet.http.*;

• public class SomeServlet extends HttpServlet {• public void doGet(HttpServletRequest request,• HttpServletResponse response)• throws ServletException, IOException {• • // Use "request" to read incoming HTTP headers (e.g. cookies)• // and HTML form data (e.g. data the user entered and submitted)• • // Use "response" to specify the HTTP response line and headers• // (e.g. specifying the content type, setting cookies).• • PrintWriter out = response.getWriter();• // Use "out" to send content to browser• }• }

Page 13: Java Servlets and Java Server Pages Norman White Stern School of Business.

Hello World

• package hall;

• import java.io.*;• import javax.servlet.*;• import javax.servlet.http.*;

• public class HelloWorld extends HttpServlet {• public void doGet(HttpServletRequest request,• HttpServletResponse response)• throws ServletException, IOException {• PrintWriter out = response.getWriter();• out.println("Hello World");• }• }

Page 14: Java Servlets and Java Server Pages Norman White Stern School of Business.

Hello WWW

• package hall;

• import java.io.*;• import javax.servlet.*;• import javax.servlet.http.*;

• public class HelloWWW extends HttpServlet {• public void doGet(HttpServletRequest request,• HttpServletResponse response)• throws ServletException, IOException {• response.setContentType("text/html");• PrintWriter out = response.getWriter();• out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +• "Transitional//EN\">\n" +• "<HTML>\n" +• "<HEAD><TITLE>Hello WWW</TITLE></HEAD>\n" +• "<BODY>\n" +• "<H1>Hello WWW</H1>\n" +• "</BODY></HTML>");• }• }

Page 15: Java Servlets and Java Server Pages Norman White Stern School of Business.

Package-Puts out Title

• package hall;

• public class ServletUtilities {• public static final String DOCTYPE =• "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0

Transitional//EN\">";

• public static String headWithTitle(String title) {• return(DOCTYPE + "\n" +• "<HTML>\n" +• "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n");• }• • // Other utilities will be shown later...• }

Page 16: Java Servlets and Java Server Pages Norman White Stern School of Business.

Using the Package

• package hall;

• import java.io.*;• import javax.servlet.*;• import javax.servlet.http.*;

• public class HelloWWW2 extends HttpServlet {• public void doGet(HttpServletRequest request,• HttpServletResponse response)• throws ServletException, IOException {• response.setContentType("text/html");• PrintWriter out = response.getWriter();• out.println(ServletUtilities.headWithTitle("Hello WWW") +• "<BODY>\n" +• "<H1>Hello WWW</H1>\n" +• "</BODY></HTML>");• }• }

Page 17: Java Servlets and Java Server Pages Norman White Stern School of Business.

OK, I think I get it

• Java servlets allow me to write CGI like programs in Java, but without the overhead of CGI, and with more power.

• But what good is this? Is it that much better than ASP, PhP etc.?

Page 18: Java Servlets and Java Server Pages Norman White Stern School of Business.

Answer

• Servlets can generate any output type, so they can stream multimedia, generate images, etc.

• Especially nice if they are sending something to a Java applet running on the browser.

Page 19: Java Servlets and Java Server Pages Norman White Stern School of Business.

Servlets are not just Java Applets

• Servlets are part of a complete development environment that can run anywhere.

• Java Version 2 Enterprise Edition (J2EE) includes Servlets (and Java Server Pages) as part of the environment

• They are quickly becoming part of a standard cross platform development environment supported by many Manufacturers

Page 20: Java Servlets and Java Server Pages Norman White Stern School of Business.

What is new here?

• Write once, run anywhere development• Ability to easily redistribute applications

across multiple servers of any type• High level standards support of many

new technologies• Component based development

environment• In short, an architecture for enterprise

application development

Page 21: Java Servlets and Java Server Pages Norman White Stern School of Business.

J2EE Architecture elements

• Distributed Communications– Network, web, RMI, CORBA, DCOM

• Enterprise Data Enabling– JDBC

• Common Services– Naming Services Directory and Trading services, Activation

Services, messaging Services, Transaction Services• Enterprise Systems Assurance

– Built-in Security supporting Java, CORBA• Enterprise Web Enabling

– XML, Java Servlets, Java Server Pages• Enterprise Applications Enabling

– Enterprise Application Platforms, Application servers and Enterprise Java Beans (EJBs).Modeling with EJBs

Page 22: Java Servlets and Java Server Pages Norman White Stern School of Business.

EJBs – Enterprise Java Beans

• These are business logic components which depend on services provided by the EJB container.

• Applications are built by creating new types of session beans and entity beans for handling transactions. Since they extend the session or entity Bean class, Beans inherit services including security, transaction management and data base integration.

Page 23: Java Servlets and Java Server Pages Norman White Stern School of Business.
Page 24: Java Servlets and Java Server Pages Norman White Stern School of Business.

EJBs – Enterprise Java Beans

Page 25: Java Servlets and Java Server Pages Norman White Stern School of Business.

Application Tiers & Responsibilities

Page 26: Java Servlets and Java Server Pages Norman White Stern School of Business.

Management Takeaway

• Java Servlets and Java Server Pages are part of a much larger environment that can provide an integrated, cross-platform, scalable, secure, robust set of “industry standard” systems.

Page 27: Java Servlets and Java Server Pages Norman White Stern School of Business.

But what about more mundane uses

• Java Server Pages (JSP)• These are HTML files with embedded

Java code (Scriptlets)• The first time the file is referenced, the

complete JSP file is turned into a Java Servlet and compiled.

• The resulting servlet can be developed much faster and more reliably without sacrificing performance.

Page 28: Java Servlets and Java Server Pages Norman White Stern School of Business.

Advantages

• Easy to maintain, user doesn’t really need to know much if any Java

• Runs faster, since everything is compiled

• Opens WEB development up to full Java toolkit including Enterprise Java Beans

Page 29: Java Servlets and Java Server Pages Norman White Stern School of Business.

Conclusion

• Java Servlets and Java Beans are being used for most high-end web sites

• Extends web model to include tightly integrated Java Applets and Java servlets

• Basis of most new peer to peer applications

• Learn Java!!