1 / 28

Web Development in Java

Web Development in Java. Andrew Simpson. Overview. Background Language Details Java Server Pages (JSP) Servlets Database Connectivity (JDBC) Samples and Application. Background. Java is a compiled language Can be server side or client side, servlets or applets

hien
Download Presentation

Web Development in Java

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Web Development in Java Andrew Simpson

  2. Overview • Background • Language Details • Java Server Pages (JSP) • Servlets • Database Connectivity (JDBC) • Samples and Application

  3. Background • Java is a compiled language • Can be server side or client side, servlets or applets • Java has many applications outside of web development • Java is syntactically very similar to C++ and other compiled languages

  4. Typical Java Code 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("<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>"); } }

  5. Primitive Data Types

  6. Java’s Tools • Similar to the stdlib in C++, Java has many common data types and other procedures already implemented • AbstractCollection, AbstractList, ArrayList, Array, BitSet, Calendar, Collections, Currency, Date, Dictionary, HashMap, HashSet, LinkedHashMap, Properties, Stack, StringTokenizer, Timer, TreeMap, TreeSet, Vector

  7. Comparisons • The usual operators work on the primitive data types • Class defined comparisons are required for all other data types • Comparator lets the programmer define their own criteria • Comparator can be defined for Java to sort different structures automatically

  8. Error Handling • Try/Catch Blocks • Functions can throw exceptions Public int foo(int x, char y) thows ServletException { if (x < 0 ) throw new ServletException(“X is negative”); if (y >= ‘a’ && y <= ‘z’) throw new ServletException(“Y is not a lower case letter”); return 1; }

  9. Java Server Pages (JSP) • Similar to Perl in that it is not compiled at first, but rather compiled on the server • Can contain static HTML/XML components • Uses “special” JSP tags • Optionally can have snippets of Java in the language called scriptlets

  10. JSP Translation

  11. JSP Syntax There are multiple styles that a JSP translator recognizes to writing a JSP • Embedding JAVA into static content • Creating new dynamic tags to do embedding • Embedding static content into JAVA

  12. Embedding Java <table border="1"> <thead> <td><b> Exp</b></td> <td><b>Result</b></td> </thead> <tr> <td>\${1}</td> <td>${1}</td> </tr> <tr> <td>\${1 + 2}</td> <td>${1 + 2}</td> </tr> <tr> <td>\${1.2 + 2.3}</td> <td>${1.2 + 2.3}</td> </tr>

  13. Using Dynamic Tags <%@ taglib prefix="mytag" uri="/WEB-INF/jsp2/jsp2-example-taglib.tld" %> <html> <head> <title>JSP 2.0 Examples - Hello World SimpleTag Handler</title> </head> <body> <h1>JSP 2.0 Examples - Hello World SimpleTag Handler</h1> <hr> <p>This tag handler simply echos "Hello, World!" It's an example of a very basic SimpleTag handler with no body.</p> <br> <b><u>Result:</u></b> <mytag:helloWorld/> </body> </html> Result: Hello, world!

  14. Tag Library (Pseudo class) package jsp2.examples.simpletag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; import java.io.IOException; /** * SimpleTag handler that prints "Hello, world!" */ public class HelloWorldSimpleTag extends SimpleTagSupport { public void doTag() throws JspException, IOException { getJspContext().getOut().write( "Hello, world!" ); } }

  15. Embedding HTML • This is the more common form that is actually used • This form is dominated mostly by scripting • HTML is a quick and easy output method far less verbose than trying to use a servlet to write out the entire output stream Refer to embedhtml.jsp file for example

  16. Model for Server Handling

  17. Request Handling

  18. General Application Flow

  19. Using A Java Servlet • Compiled to form a class before being put on the server • Does not allow embedded code • Functions very much like a class in C++ • Has several built in functions specific to web development that are very useful

  20. JSP vs. Servlets • JSP is really just an extension of the Servlet API • Servlets should be used as an extension of web server technology, specialized controller components, database validation. • JSP handles text while Servlets can interface other programs

  21. Servlets and HTML Forms • Post vs. Get Methods • Built in handling doPost and doGet • Good for taking in information in servlet request, processing it, generating a servlet response and returning it back to the browser • Notion that server always passes a separate class object for Requests and Responses between pages which carry a persisting Session object in many cases.

  22. Session Object for Requests

  23. General Servlet Info • Similar to C++ class • Member variables/functions • Private and Public options • Usually extension of some other class, new class inherits functions of extended class

  24. JDBC • Java.sql.* package serves as that java ODBC equivalent • Basic Methods: Driver, DriverManager, Connection, Statement, PreparedStatement, Callable Statement, ResultSet • Statements allow JDBC to execute SQL commands

  25. Starting a Database • Connect by passing a driver to the DriverManager • Obtain a Connection with URL, username and password • Pass SQL commands with a Statement • Examine ResultSet if applicable • Close the database View dbsamp.jsp for startup sequence and simple query

  26. Data Navigation and Extraction • Result.next(); • Result.getInt(1); • Result.getString(“Customer”); • Result.getDate(4); (java.sql.Date not java.util.Date)

  27. Prepared Statements pstmtU = con.prepareStatement( "UPDATE myTable SET myStringColumn = ? " + "WHERE myIntColumn = ?" ); pstmtU.setString( 1, "myString" ); pstmtU.setInt( 2, 1024 ); pstmtU.executeUpdate();

  28. Conclusion • This is a really general fast overview to outline the overarching concepts • Refer to http://java.sun.com for lots of good documentation, API descriptions • Excellent collection of basic tutorials at, http://www.jguru.com/learn/index.jsp • I will now go over a real example of a Java web based application

More Related