Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Development in Java Andrew Simpson. Overview Background Language Details Java Server Pages (JSP) Servlets Database Connectivity (JDBC) Samples and.

Similar presentations


Presentation on theme: "Web Development in Java Andrew Simpson. Overview Background Language Details Java Server Pages (JSP) Servlets Database Connectivity (JDBC) Samples and."— 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(" "); out.println(" Hello World! "); out.println(" "); out.println(" Hello World! "); out.println(" "); }

5 Primitive Data Types KeywordDescriptionSize/Format ByteByte-length integer8-bit 2’s complement ShortShort integer16-bit 2’s complement IntInteger32-bit 2’s complement LongLong integer64-bit 2’s complement FloatSingle precision float32-bit IEEE 754 DoubleDouble precision float64-bit IEEE 754 CharA single character16 bit Unicode char BooleanA boolean valueTrue or false

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 Embedding JAVA into static content Creating new dynamic tags to do embedding Embedding static content into JAVA There are multiple styles that a JSP translator recognizes to writing a JSP

12 Embedding Java Exp Result \${1} ${1} \${1 + 2} ${1 + 2} \${1.2 + 2.3} ${1.2 + 2.3} ExpResult ${1}1 ${1+2}3 ${1.2+2.3}3.5

13 Using Dynamic Tags JSP 2.0 Examples - Hello World SimpleTag Handler JSP 2.0 Examples - Hello World SimpleTag Handler This tag handler simply echos "Hello, World!" It's an example of a very basic SimpleTag handler with no body. Result: 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 Refer to embedhtml.jsp file for example 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

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 descriptionshttp://java.sun.com Excellent collection of basic tutorials at, http://www.jguru.com/learn/index.jsp http://www.jguru.com/learn/index.jsp I will now go over a real example of a Java web based application


Download ppt "Web Development in Java Andrew Simpson. Overview Background Language Details Java Server Pages (JSP) Servlets Database Connectivity (JDBC) Samples and."

Similar presentations


Ads by Google