Successfully reported this slideshow.

Basci Java Servlet/JSP Web Development

3,016 views

Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

Basci Java Servlet/JSP Web Development

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

×