
3cbb3b956f679b0a163f494f28f2e38b.ppt
- Количество слайдов: 27
Basic Java Servlet/JSP Web Development David Lucek Consulting www. lucek. com dave@lucek. com May 13 th, 2003 @2003 Lucek Consulting
Download the Sample Application l l l Download from www. lucek. com, select the downloads tab Includes the full source Unzip to c: drive or $HOME/lucek May 13 th, 2003
What is a Servlet? l Java Servlets/JSP are part of the Sun’s J 2 EE Enterprise Architecture – l Java Servlet – – – l The web development part is a simple, consistent mechanism for extending the functionality of a web server Are precompiled Java programs that are executed on the server side. Require a Servlet container to run in Latest Servlet Spec is 2. 3 May 13 th, 2003
What is a Java Server Page (JSP) l Java Server Pages (JSP) – – l A simplified, fast way to create dynamic web content HTML or XML pages with embedded Java Code or Java Beans Can be a mix of template data in HTML/XML with some dynamic content A JSP is a complied to a Java Servlet automatically by the Servlet container, it is then cached Latest JSP Spec is 1. 2 May 13 th, 2003
Why Use Servlets? l Work well in a Heterogeneous Environments – – l OS and platform neutral Work with all major web servers (IIS, Apache, etc. . ) Well defined Web Architecture framework – Standard built in services such as: l l l Standard Approach to Authentication using declarative security vice programmatic security Database connection pooling Complete support for sessions via cookies and/or URL re-writing – Has automatic fallback to URL re-writing May 13 th, 2003
Why Use Servlets Con’t? – Robust Object-Orientated API in Java language l l Ever try to maintain a large ASP, Perl, or PHP site Clean separation of Controller/Logic from Presentation Efficient, scales very well There are good free Servlet/JSP containers and connectors – That run under both UNIX and win 32 May 13 th, 2003
J 2 EE Web Application Components l Java Servlets – l l JSP pages, normally for Presentation Java Beans – l l Extend off of Http. Servlet Normally used as value objects, pass to data to JSPs Tag Libraries – XML based JSP elements Web Deployment Descriptor – /web-inf/web. xml May 13 th, 2003
Web Deployment Descriptor l /web-inf/web. xml – – l l l Part of the standard Defines servlets used in the web application Maps servlets to URLs A servlet can map to many URLs Defines resources available to the web app Defines security constraints Defines other stuff like – – – Welcome file list Session timeout Error page mapping May 13 th, 2003
J 2 EE Web Directory Structure 1 l Top Directory is normally the context Path – – – l /tomcat/webapps/servletdemo Normally, the URL would be http: //localhost: 8080/servletdemo Contains JSP and other static content plus the web-inf directory /web-inf directory – – This is a protected directory, can not point browser to any file in this directory /classes – unpacked web application classes, auto-magically added to CLASS_PATH /lib – web application JAR files /taglib – tag library descriptor files May 13 th, 2003
J 2 EE Web Directory Structure 2 l l /web-inf/web. xml /web-inf/* – – – Would normally put any static or JSP files here Protects them from Direct Invocation Always best to call a JSP through a servlet first May 13 th, 2003
JSP Constructs 1 l l Used in JSP pages, pages that end *. jsp Comment <%-- Comment --%> Declaration <%! int x = 0; %> Expression <%= expression %> – – – l Outputs to the Response stream Like a “printf” to the browser Do NOT use semi-colon to terminate the line Scriplets - contains Java Code – <% code fragments %> May 13 th, 2003
JSP Constructs 2 <% if (value. get. Name(). length != 0) { %> <H 2>The value is: <%= value. get. Name() %></H 2> <% } else { %> <H 2>Value is empty</H 2> <% } %> l Implicit objects always available in the JSP Page – “request” – Browser’s Request Object l – Use to get HTTP headers, length etc. . “response” - Http. Response Object May 13 th, 2003
JSP Constructs 3 – – – – l “session” – internal Http. Session Object “page. Context” “application” “out”, same as <%= %> “config” – servlet configuration “page” “exception” JSP Directives – Are messages or instructions to the JSP container May 13 th, 2003
JSP Constructs 4 – – Do not produce any output “page” directive l l – “include” directive l l – <%@ page import=“com. lucek. *” %> Commonly used for importing class paths <%@ include file=“header. htm” %> Good for including static content “taglib” – lists the tag library descriptor location l Required when using tab libraries May 13 th, 2003
Java Beans as Used in Web Apps l l Normally used for all data transfers and business components Similar to how Java Beans are used in Swing and AWT – l l l But do not need the full implementation Must have no constructor or no-arg constructor Must have setter and getter methods for each property value JSP constructs/tags use Java Beans May 13 th, 2003
JSP Actions l JSP actions are special tags that affect the output stream and are normally used with Java beans – Most commonly used: <jsp: use. Bean>, <jsp: get. Property>, <jsp: set. Property> l The code below will display the last. Name property of the student bean on the output stream <jsp: use. Bean id="student" scope="request" class="com. lucek. dto. Student. Value" /> <jsp: get. Property name="student" property="last. Name" /> l May 13 th, 2003
Servlet Container/Engine l l l Servlets/JSP require a Container Apache Tomcat is the reference implementation of the Servlet/JSP Specs It is open source, small, install quickly, and is FREE Latest Version is 4. 1. 24 Web Site: jakarta. apache. org/tomcat It include a simple HTTP 1. 1 server, good enough for development and small intranets. May 13 th, 2003
Tomcat Install l l Requires a JDK, get 1. 4. 1 and install into c: jdk or $HOME/jdk Add JAVA_HOME to your environment and the “bin” directory to your PATH Good practice to unpack into c: tomcat or $HOME/tomcat Add CATALINA_HOME to your environment and the “bin” directory to your PATH May 13 th, 2003
Tomcat Directory Structure l l l Everything is relative to $CATALINA_HOME /bin – Startup/shutdown scripts /conf – l /common – common class and jar files used by Tomcat and web applications – l l l Server. xml – main configuration file Put JDBC drivers here /server – class and jar files used by Tomcat internally /shared – class and jar files for all web applications /webapps – This is where you put your web application in a sub-directory or external context file. May 13 th, 2003
Starting Tomcat l l /bin/startup. bat or startup. sh Point Browers to http: //localhost: 8080, should see default page All the Docs are there on the default page! Check out the examples pages, good tutorials May 13 th, 2003
Other Development Tools 1 l Ant Build Tool – – l Standard Java Build tool Basic on UNIX make, but much better Site: http: //ant. apache. org Install in c: ant or $HOME/ant Java IDE – – Try Net. Beans, it is nice Tomcat is built in, but is an older version Includes full Servlet and JSP debugging Site: www. netbeans. org May 13 th, 2003
Other Development Tools 2 l Junit – – l Standard Automated Unit Testing Tool Site: http: //junit. sourceforge. net Jedit – – – Slick Programmer’s Editor Written in Java Site: jedit. org May 13 th, 2003
Simple Servlet Application 1 l l l See “servletdemo” code Mount the servletdemo, servletdemo/java/src, and servletdemo/web in Net. Beans Explorer Tab For a Hello World Servlet look at: – l Java/src/com/lucek/action/Hello. World. java To build and run – – – $ cd servletdemo Setup the proper build variables in the build. properties file $ ant all $ ant deploy Point your browsers at http: //localhost: 8080/servletdemo May 13 th, 2003
Simple Servlet Application 2 l l l Look at the web. xml file and how the same servlet can be mapped to many URLs Look at Simple. Bean. java which should how to pass a Java Bean to a JSP page Look at the different ways a bean’s value can be obtained in the Edit. Student. jsp May 13 th, 2003
Best Practices/Patterns l Always Separate out the logic from the presentation – – l l l Have a clean separation between your data access and controller layers (DAO) Always use DTO or value object Use a Model-View-Controller Architecture – – l Use servlets for the logic/controller and JSP’s for presentation Ideally should never have Java Code in the JSP page Do not write it, use Struts Site: jakarta. apache. org/struts/ Use Unit tests – Junit Automation via Ant build tasks May 13 th, 2003
What we have not talked about l l l All the specific Servlet APIs Tag libraries Sessions, cookies JDBC service support from the container Container based authentication Lots of other stuff May 13 th, 2003
Next Presentation? l l Create a data driven web site using My. Sql and Servlets/JSP Setup Authentication Realm with declarative security Setup JDBC connection pooling Struts? May 13 th, 2003
3cbb3b956f679b0a163f494f28f2e38b.ppt