0% found this document useful (0 votes)
106 views

JSP Notes: Introduction - Components of JSP

This document provides an overview of key concepts related to JavaServer Pages (JSP) including JSP directives, actions, implicit objects, scripting, servlets, JavaBeans, JDBC, configuration, errors, forms, and communication with servlets. It covers the page life cycle, components of JSP like directives, actions, implicit objects, and scripting. It also discusses Java servlets architecture, JavaBeans and their scope in JSPs, using JDBC with JSPs, and configuring the JSP server.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

JSP Notes: Introduction - Components of JSP

This document provides an overview of key concepts related to JavaServer Pages (JSP) including JSP directives, actions, implicit objects, scripting, servlets, JavaBeans, JDBC, configuration, errors, forms, and communication with servlets. It covers the page life cycle, components of JSP like directives, actions, implicit objects, and scripting. It also discusses Java servlets architecture, JavaBeans and their scope in JSPs, using JDBC with JSPs, and configuring the JSP server.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

JSP Notes

Introduction Components of JSP


Directives
The page Directives
<%@ page language=java %> Defines information that will be globally availably for this page Atributes: language=java extends=className import=importList session=true|false default is true buffet=none|size in kb default is 8kb autoflush=true|false default is true isThreadSafe=true|false default is true info=text can be accessed by Servlet.getServletInfo() errorPage=error_url isErrorPage=true|false default is false contentType=ctinfo

The include Directive


<%@ include file=relativeURLspec %> used to insert text or code at JSP translation time

The taglib Directive


<%@ taglib uri=tagLibriaryURI prefix=tagPrefix %>

Actions
<jsp:useBean> <jsp:setProperty> <jsp:getProperty> <jsp:include> <jsp:forward> <jsp:param> <jsp:plugin>

Implicit Objects
application config exception out page pageContext request response session

JSP Scripting
Declaration: Expression: Scriptlets: <%! declaration %> <%= expression %> <% codes %>

Servlet Basic
Java Servlet Architecture
Two Packages:
javax.servlet: contains the generic interfaces and classes javax.servlet.http: contains classed that are extended for http

Heart: javax.servlet interface


<<Interface>> javax.servlet.Servlet init() getServletConfig() service() getServletInfo() destroy() <<Interface>> javax.servlet.ServletConfig getInitParameter() getServletContext() getInitParameterNames() <<Interface>> javaz.io.Serializable

javax.servlet.GenericServlet getServletContext() getInitParameter() getInitParameterNames() getServletInfo() log() init() getServletConfig() service() destroy()

javax.servlet.http.HttpServlet doDelete() doGet() doOptions() doPost() doPut() doTrace() getLastModified() service()

GenericServlet and HttpServlet


HttpServlet extends GenericServlet

Java servlets do not have main. Thats why it must implement javax.servlet.Servlet interface If you extends GenericServlet, you must implement the service() method

The Life Cycle of a Servlet


A servlet is constructed and initialized. It services zero or more request until the server shuts down. At this point the servlet is destroyed. init(): servlet life begins. It is called immediately after the servlet is instantiated and called only once. In this method, servlet creates and initializes any resources.
public void init(ServletConfig config) throws ServletException

You should save the ServletConfig parameter by call super.init(config) service(): handles all requests. It can not start service until init() has been executed.
public void void(SeveletRequest req, ServletResponse res) throws ServletException, IOException

destroy(): end of life. When service is shut down. Resource cleaned up.

JavaBeans and JSP Concepts


A java class that implements the java.io.Serializable interface and uses public get/set methods to expose its properties Java Bean

Adding JavaBeans to JSPs JavaBean Standard Actions


<jsp:useBean> syntax: <jsp:useBean id=name scope=page|request|session|application typeSpec> body </jsp:useBean> typeSpec ::= class=className | class=className type=typeName | type=typeName class=className | beanName=beanName type=typeName | type=typeName beanName=beanName | type=typeName attributes: id: identity of the instance of the object in the scope. Case-sensitive. scope: life of the bean class: fully qualified class name that defines the implementation. beanName: references the name of the bean type: type of the scripting variable. If not defined, same as class attribute example: <jsp:useBean id = "pi_bean" scope= "request" class="edu.tmc.uth.oac.cphs.PIBean" /> <jsp:setProperty> syntax: <jsp:setProperty name=beanName prop_expr /> prop_expr ::= property=* | property=propertyName | property=propertyName param=parameterName | property=propertyName value=propertyValue attributes: name: define by <jsp:useBean>

property: bean property, * for all param: represent the name of the request parameter whose value you want to set the named property value: value assigned to the named property example: <jsp:setProperty name="pi_bean" property="*" /> <jsp:getProperty> syntax: <jsp:getProperty name=name property=propertyName /> attributes: name: defined by <jsp:useBean> property: bean property

JDBC and JSP Concepts


JDBC Drive Types
Type I: JDBC-ODBC Bridge, plus ODBC driver Native-API JDBC-net, pure Java driver Native-protocol, pure Java driver

JDBC Basics
Establishing a Database connection Import package: Import java.sql.*; Loading a JDBC driver: Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); Make a connection: Connection con = DriverManager.getConnection(jdbc:odbc:dataSource, usrer, password); Creating a JDBC Statement Object: Statement statement = con.createStatement(); Execute SQL creating table, inserting, updating and deleting data statement.executeUpdate(SQL Update Comamnd); Execute SQL selecting data ResultSet rs = statement.executeQuery(SQL Select Command); while(rs.next()) { }

Configuring the JSP Server


Installing the Tomcat Server
download from http://jakarta.apache.org decompress to a directory: c:\tomcat\ set JAVA_HOME= Test: http://serverName:8080/

Handling JSP Errors


JSP Translation Time Error Compile time, error code 500 JSP Request Time Error Runtime error, need catch the exception Creating an error page <html> <body text=red> <%@ page isErrorPage = true> Error: <%= exception.getMessage() %> has been reported. </body> </html> Using error page <%@ page errorPage=errorpage.jsp %> <% if(true) { throw new Exception(A JSP Error); } %>

Using the include Directive


<%@ include file=relativeURLspec %> evaluated at translation time if the include file changed, the page need to recompile

JSP and Inheritance


Avoid this

The superclass
Must implement the HttpJspPage if it use the HTTP protocol or JspPage if not

All methods from the Servlet interface must be declared final The service() must invoke the _jspService() The init() must invike the jspInit() The destroy() must invoke the jspDestroy()

The JSP Subclss


Must provide a jspInit() method Must provide a jspDestroy() method

JSPs Implicit Objects


request
common use to access the parameters: request.getParameter(parameterName);

response pageContext
provides access to the namespace associated with the jsp page and several other JSP implicit objects

session
session.setAttribute(attributeName, attributeValue); String s = (String) session.getAttribute(attributeName);

application out
out.println(content);

config page
= this

exception

Using JSP Standard Actions


<jsp: param>

provides tag/value pairs of information by including them as subattributes of the <jsp:include>, <jsp:forward> and <jsp:plugin> syntax: <jsp:param name=paramName value=paramValue/> <jsp:include> includes additional static and dynamic resource in the current JSP page syntax: <jsp:include page=urlSpec flush=true /> or <jsp:include page=urlSpec flush=true > <jsp:param name=paramName value=paramValue/> </jsp:include> <jsp:forward> enables the JSP engine to dispatch, at runtime, the current request to a static resource, servlet, or another JSP syntax: <jsp: forward page=relativeURLSpec /> or <jsp: forward page= relativeURLSpec > <jsp:param name=paramName value=paramValue/> </jsp: forward > <jsp:plugin> enables the JSP author to generate HTML that contains the appropriate clientbroswer-dependent construct, for example, OBJECT or EMBED, that will result in the download Java plugin syntax: <jsp:plugin type=pluginType code=classFile codeBase=relativeURLpath <jsp:params> </jsp:params> </jsp:pluin>

JSPs and JavaBean Scope


page accessible only within the page where they were created reference is released when the response is sent back to the client or the request is forward to another source objects are stored in the pageContext most often used for single calculation or transaction request accessible only within pages processing the same request that the object was created in reference is released after the request is processed completely if the request is forward to another source, the object is still in scope most often used when you need to share information between resources that is pertinent for the current request only session

accessible within pages that in the same session it is illegal to define an object with session scope from within a page that has an attribute session=false release after the session ends stored in session object often used for information shared for a single client application it is available until the JSP engine restarted global

JSP and HTML Forms


Using a JSP to Create an HTML form Retrieving Form Data with a JSP
Create a bean that has properties that match the tag/value pairs sent in the request Include this bean in the calling JSP and use the get() method to retrieve the parameter Example: Suppose we have a bean called Company <jsp:useBean id=company scope=request class=Company /> <jsp:setProperty name=company property=* /> <html> <head> </head> <body> Company: <%=company.getCompany() %> <br> . </body> </html>

JSP and a JDBC Connection Pool Bean JSP and XML JSP Communication with Servlet
MVC Model
Model: represents the data or application object View: screen representation of the model, current state of the model Controller: defines the way the user interface reacts to the user inputs, the object that manipulate the model

JavaMail Tag Library

You might also like