Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Topic : Servlet & JSP Kaster Nurmukan • It is standard : API Standard jdbc , RMI , Email ,JMS , webservice API , • Also include component standard : serverlet , EJB, Connector , JSP (java service page ) , webservice • Java Servlets/JSP are part of the Sun’s J2EE Enterprise Architecture – The web development part – Request controller , Filter , Adeppter – Javax.Servelt • Process or store data that was submitted from an HTML form • Provide dynamic content such as the results of a database query • Manage state information • Latest Servlet Spec is 3.0 (JSR 315 ) • Java Server Pages (JSP) – 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 2.2 (JSR 245) servlet HttpServlet GenericServlet init(ServletConfig); service(ServletRequest, doGet(HttpServletRequest, HttpServletResponse); ServletResponse); doPost(HttpServletRequest, destroy(); HttpServletResponse); ……. package kz.edu; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Servlet extends HttpServlet { public init(ServletConfig) throws IOException, ServletException { //...... To do } public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("<html><head><title>Sample Servlet"); out.println("</title></head><body>"); out.println("<h1>Hello World at " + req.getRequestURI() + " !</h1>"); out.println(”<p>Key is " + req.getParameter("KEY")); out.println(”</p></body></html>"); } } • A catalog of servlet methods: – init(config) – service(req, res) • doGet(req, res) • doPut(req, res) • doDelete(req, res) • Destry()… • A catalog of request methods: – getParameter(name) – getParameterNames(), getParameterValues(name) – getCookies() • A catalog of response methods: – sendRedirect(url), sendError(errCode, errStr) – setContentType(mimeType) – addCookie(cookieObj) • Java Servlets – Extend off of HttpServlet • JSP pages, normally for Presentation • Java Beans – Normally used as value objects, pass to data to JSPs • Tag Libraries – XML based JSP elements • Web Deployment Descriptor – /web-inf/web.xml May 13th, 2003 • /web-inf/web.xml – – – – 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 13th, 2003 • Top Directory is normally the context Path – /tomcat/webapps/testServlet – Normally, the URL would be http://localhost:8080/testServlet – 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 13th, 2003 • /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 13th, 2003 • • • • Used in JSP pages, pages that end *.jsp Comment <%-- Comment --%> Declaration <%! int x = 0; %> Expression <%= expression %> – 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 13th, 2003 <% if (value.getName().length != 0) { %> <H2>The value is: <%= value.getName() %></H2> <% } else { %> <H2>Value is empty</H2> <% } %> • Implicit objects always available in the JSP Page – “request” – Browser’s Request Object • Use to get HTTP headers, length etc.. – “response” - HttpResponse Object May 13th, 2003 – – – – – – – “session” – internal HttpSession Object “pageContext” “application” “out”, same as <%= %> “config” – servlet configuration “page” “exception” • JSP Directives – Are messages or instructions to the JSP container May 13th, 2003 – Do not produce any output – “page” directive • <%@ page import=“com.lucek.*” %> • Commonly used for importing class paths – “include” directive • <%@ include file=“header.htm” %> • Good for including static content – “taglib” – lists the tag library descriptor location • Required when using tab libraries May 13th, 2003 • Normally used for all data transfers and business components • Similar to how Java Beans are used in Swing and AWT – 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 13th, 2003 • JSP actions are special tags that affect the output stream and are normally used with Java beans – Most commonly used: • <jsp:useBean>, <jsp:getProperty>, <jsp:setProperty> • The code below will display the lastName property of the student bean on the output stream <jsp:useBean id="student" scope="request" class="com.lucek.dto.StudentValue" /> <jsp:getProperty name="student" property="lastName" /> May 13th, 2003 • 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 • Web Site: jakarta.apache.org/tomcat • It include a simple HTTP 1.1 server, good enough for development and small intranets. • Other Servlet Engine – GlassFish – IBM WebSphere Application Server – Jetty (web server) May 13th, 2003 • Requires a JDK, get lates verstion 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 13th, 2003 • Everything is relative to $CATALINA_HOME • /bin – Startup/shutdown scripts • /conf – Server.xml – main configuration file • /common – common class and jar files used by Tomcat and web applications – 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 13th, 2003 • /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 13th, 2003 • Ant Build Tool – Standard Java Build tool – Basic on UNIX make, but much better – download: http://ant.apache.org • Java IDE – – – – Try NetBeans, it is nice Tomcat is built in, but is an older version Includes full Servlet and JSP debugging download: www.netbeans.org May 13th, 2003 • Junit – Standard Automated Unit Testing Tool – Site: http://junit.sourceforge.net • Jedit – Slick Programmer’s Editor – Written in Java – Site: jedit.org May 13th, 2003 • Always Separate out the logic from the presentation – Use servlets for the logic/controller and JSP’s for presentation – Ideally should never have Java Code in the JSP page • Have a clean separation between your data access and controller layers (DAO) • Always use DTO or value object • Use a Model-View-Controller Architecture – Do not write it, use Struts – Site: jakarta.apache.org/struts/ • Use Unit tests – Junit Automation via Ant build tasks May 13th, 2003 • • • • • • All the specific Servlet APIs Tag libraries Sessions, cookies JDBC service support from the container Container based authentication Lots of other stuff May 13th, 2003 • Create a data driven web site using MySql and Servlets/JSP • Setup Authentication Realm with declarative security • Setup JDBC connection pooling • Struts? May 13th, 2003 • http://www.oracle.com