JSP Notes: Introduction - Components of JSP
JSP Notes: Introduction - Components of JSP
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
javax.servlet.GenericServlet getServletContext() getInitParameter() getInitParameterNames() getServletInfo() log() init() getServletConfig() service() destroy()
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
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.
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 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()) { }
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()
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
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>
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 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