Presentation is loading. Please wait.

Presentation is loading. Please wait.

ISOM MIS 3150 Module 3 3-Tier Webapps using JSP/JDBC Arijit Sengupta.

Similar presentations


Presentation on theme: "ISOM MIS 3150 Module 3 3-Tier Webapps using JSP/JDBC Arijit Sengupta."— Presentation transcript:

1 ISOM MIS 3150 Module 3 3-Tier Webapps using JSP/JDBC Arijit Sengupta

2 ISOM Structure of this semester Database Fundamentals Relational Model Normalization Conceptual Modeling Query Languages Advanced SQL XML Databases Java DB Applications – JDBC/JSP Data Mining 0. Intro 1. Design 3. Applications 4. Advanced Topics NewbieUsersProfessionalsDesigners MIS415 2. Querying Developers

3 ISOM Today’s Buzzwords 3-Tier applications  Client – WebServer – ApplicationServer Basics of JDBC Basics of JSP Containers - Tomcat Web Applications using JSP

4 ISOM 3 Tier Architecture HTTP request HTTP response Web server JSP page JSP container compiles to a servlet URL request JavaBean Library DB properties, call methods HTTP page response Browser

5 ISOM JDBC  A platform-independent library of classes allowing database access from any Java application  Take advantages of Polymorphism JDBC is a set of interfaces –Driver, Connection, Statement, ResultSet, etc. Database vendors (not programmers) will implement these interfaces. If we switch from one database to another, we just need to load different driver (plug and play)! –YOU DON'T NEED TO MODIFY THE REST OF YOUR PROGRAM!

6 ISOM JDBC DriverManagerConnectionStatementResultSet Database Driver

7 ISOM JDBC DriverManagerConnectionStatementResultSet Driver Database

8 ISOM JDBC (Contd.) Register a JDBC driver Driver d = new oracle.jdbc.driver.OracleDriver(); DriverManager.registerDriver (d);  or DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); Or, use the Java reflection abilities Class.forName( "oracle.jdbc.driver.OracleDriver");  calling Class.forName() will create an instance of a driver and register it with the DriverManager automatically  This is better since we can use a constant: String DRIVER = "oracle.jdbc.driver.OracleDriver"; Class.forName(DRIVER); For mysql, use: com.mysql.jdbc.driver

9 ISOM JDBC DriverManagerConnectionStatementResultSet Driver Database

10 ISOM JDBC (Contd.) Make a connection String URL = "jdbc:oracle:thin:@unixapps1.wright.edu: 1521:ORA2"; // For Oracle String URL = ”jdbc:mysql://localhost/employees"; // For MySQL Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);

11 ISOM JDBC DriverManagerConnectionStatementResultSet Driver Database

12 ISOM JDBC (Contd.) Create a statement Statement st = conn.createStatement(); //default:TYPE_FORWARD_ONLY and CONCUR_READ_ONLY  or Statement st =conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); //the resultset will be scrollable and sensitive to changes made by others //we can update the resultset

13 ISOM JDBC DriverManagerConnectionStatementResultSet Driver Database

14 ISOM JDBC (Contd.) Execute a query String SQL = "INSERT INTO s" + " VALUES ('222-22-2222')"; int result = st.executeUpdate(SQL); //either the row count for INSERT, UPDATE or DELETE or 0 for SQL statements that return nothing Execute a query and create a resultset String SQL = "SELECT * FROM Student"; ResultSet rec = st.executeQuery(SQL);

15 ISOM JDBC (Contd.) Process the resultset while(rec.next()) { System.out.println(rec.getString("snum")); }  or while(rec.next()) { System.out.println(rec.getString(1)); // first column of the resultset }  There are methods like getString(), getInt(), etc. that take either a column name or column position See http://java.sun.com/j2se/1.3/docs/guide/jdbc/index.html for all JDBC Class documentation http://java.sun.com/j2se/1.3/docs/guide/jdbc/index.html

16 ISOM Tomcat – a J2EE Container Open Source – integrated as an Apache.org project Can be obtained from http://tomcat.apache.org http://tomcat.apache.org Provides full JSP 2.0/Servlet 2.4 functionality

17 ISOM Elements of a Java Server Page  Directives: Provide global information to the page Import statements Scripting language u Declarations: For page-wide variable and method declarations

18 ISOM Elements of a Java Server Page Scriptlets:  This is the Java code embedded in the web pages Expressions:  Formats the expression as a string to be included in the output of the web page Comments: User readable comments, contents ignored and removed by the JSP Compiler

19 ISOM JSP Directives  General syntax:  Possible values for directives are:  Page - Information for the page Include - Specifies the files whose contents are to be included in the output  e.g., Taglib  The URI for a library of custom tags that may be used in the page

20 ISOM JSP Page Directive The page directive may take the following values:   This variable tells the JSP engine what language will be used in the file "java" is the only language supported by JSP in the current specification  Comma separated list of classes and packages that are used in the JSP page  Should appear at the top of the file

21 ISOM JSP Page Directive (Contd.)  true indicates that session data is available to the page  By default, this is set to true  Determines the size of the output stream buffer  Defaults to 8kb  Use with autoFlush  When set to true, flushes the output buffer when it is full, rather than raising an exception

22 ISOM JSP Page Directive (contd.)  Specifies the relative path of the page, where control would be transferred if any exceptions are thrown from this page  The error handling JSP page should have its isErrorPage directive set to true  Marks the page as an error handler

23 ISOM JSP Declarations Class and instance variables (of the generated servlet class) may be specified using the JSP Declaration tag: <%! String name = “Web Applications"; int index = 10; int count = 0; %> Methods may also be specified: <%! private int getNextIndex() { return index ++; } %>

24 ISOM JSP Scriptlets JSP scriptlets are defined as block of Java code embedded between a pair of tags,. Example: <% java.util.Date d = new java.util.Date(); out.println(d); %>

25 ISOM JSP Expressions Useful for embedding the result of a Java expression in a HTML page  The expression is encoded between the tags  The value of the expression is converted to a string and then displayed  Conversion of primitive types to string happens automatically  Example:  The date is

26 ISOM JSP Implicit Objects When writing scriptlets and expressions, the following objects (called implicit objects) are available by default: request javax.servlet.http.HttpServletRequest response javax.servlet.http.HttpServletResponse out javax.servlet.jsp.JspWriter session javax.servlet.http.HttpSession application javax.servlet.ServletContext exception java.lang.Throwable

27 ISOM Reading inputs from Forms/URLS Remember that parameters are passed to Web applications via one of two methods:  GET method: parameters are passed directly through the URL, encoded using the urlencoding method Quick to create and test – can be created without a form Can be bookmarked URL shows in plaintext – not secure  POST method: parameters are encoded and sent to the server separately from the URL Can only be created via forms (or advanced applications) Secure – parameters cannot be seen Reading a single parameter via name: String value = request.getParameter(paramname); String [] values = request.getParameterValues(paramname); /* for multivalued parameters like checkboxes/multiple selectable lists */ Better way – using “Beans” - shortly

28 ISOM JSP Session Example Visitor Count -- JSP Session Visitor Count This JSP page demonstrates session management by incrementing a counter each time a user accesses a page. <%! private int totalHits = 0; %> <% session = request.getSession(true); Integer ival = (Integer)session.getValue("jspsession.counter"); if (ival == null) ival = new Integer(1); else ival = new Integer(ival.intValue() + 1); session.putValue("jspsession.counter", ival); %> You have hit this page time, out of a total of page hit !

29 ISOM JSP Actions Actions are tags that may affect the runtime behavior of the JSP or affect the current out stream; they may also use, modify and/or create objects. JSP specification defines the following standard actions: 

30 ISOM JSP Actions  New action types are introduced by means of custom tags

31 ISOM What is a JavaBean? A Java class that (at a minimum)  has an empty constructor  has getters and setters for each of the properties in the class  implements the serializable interface

32 ISOM JSP Actions and Attributes JSP actions can define named attributes and associated values My name is: Name using Bean is:

33 ISOM Beans and HTML forms You may connect HTML form parameters to Bean properties <jsp:setProperty name="id" property="accountHolder" value = " /> There is a shorthand for the above:  This works if the property name was exactly the same as the form parametername

34 ISOM Beans and HTML forms If the form parameter name and the property name do not match, then use the following variant of "jsp:setProperty" Another powerful variation of "jsp:setProperty" examines all the parameter names from the request object and if some of them match with property names, it sets the appropriate properties

35 ISOM Beans and HTML (contd.) Name is: Age is: Weight is:

36 ISOM Including Files JSP supports two kinds of file inclusion:  Static (using the directive "include")  Dynamic (or Request-Time) Static inclusion is specified using the "include" directive  e.g.,  In static inclusion, the contents of "header.html" are included in the output of the containing JSP file during the JSP page compilation  If the contents of the "header.html" file change, these changes are not visible to the user of the containing JSP file  Static inclusion is fast (because inclusion is done at compile time)

37 ISOM Including Files (contd.) Dynamic inclusion is supported by the use of the tag "jsp:include"  e.g.,  Each time the containing JSP file is accessed, the JSP engine includes the contents of the latest copy of the file "news/headlines.jsp“  You may also pass parameters to the included file, e.g:...

38 ISOM Forwarding Files A request to a JSP file may be forwarded, transparently to the user, to another JSP file   This is used to redirect request from one page to another page  The calling JSP file relinquishes control to the forwarded page  The calling JSP cannot send any data back to the browser before invoking this tag  If data had already been sent by the calling JSP, invoking the "jsp:forward" tag causes an exception to be thrown

39 ISOM Forwarding Requests You may also pass parameters to the forwarded page  ...  Parameters passed in this fashion may be accessed by the forwarded JSP (catalog.jsp) by the following call:  request.getParameter("color");

40 ISOM Forwarding Requests Request forwarding is typically used for authentication <% if(request.getParameter("login-info") == null) %> <% else { /* Generate Error message – no login */ } %>

41 ISOM Summary JSP – JDBC provides a stable and well-tested platform for 3-tier web application design Supports all major databases and all platforms With Servlet compilation, performance is high Many major applications like Wiki, major Government/Funding organizations like NSF – are running on Java/JSP technology. Web services are well-supported J2EE provides a well-designed software design and development platform for enterprise systems.


Download ppt "ISOM MIS 3150 Module 3 3-Tier Webapps using JSP/JDBC Arijit Sengupta."

Similar presentations


Ads by Google