互联网开发技术第六讲Web技术.ppt
互联网开发技术Internet Systems Development Technologies,蔡 剑,Ph.D.Assistant Professor in Management Science and E-BusinessGuanghua School of ManagementPeking University,本讲内容,Web层技术(II)Servlet 的新功能JSP 技术About The Course Project:Proposal due one week after midtermSchedule meetings with groups next week About The Midterm:Time:10/31(First 1.5 hours)Questions:choices,analysisCovers lecture 1-6(book p1-208),Review:J2EE Framework,Review:Servlet Process,Review:Collaboration,A Context is the Domain of An Application,Context,http:/yourip:8080/icamples/password,http:/yourip:8080/jsp-examples/*,Servlets Call Each Other,Get RequestDispatcher from HttpServletRequest:RequestDispatcher desp=req.getRequestDispatcher(“/target”)2.Get RequestDispatcher from ServletContext:ServletContext context=req.getSession().getServletContext()RequestDispatcher desp=context.getRequestDispatcher(“/target”)3.Get ServletContext for another application,then get RequestDispatcher:ServletContext context2=context.getContext(/icwork);RequestDispatcher desp=context2.getRequestDispatcher(“/target”);,To Load Another Servlet,despatcher.forward(req,resp);response.sendRedirect(targetURL);Difference?,HTTP/1.1 200 OK Content-Type:text/html Transfer-Encoding:chunked Date:Thu,17 Oct 2002 20:42:53 GMT Server:Apache Coyote HTTP/1.1 Connector48f,HTTP/1.1 302 Moved Temporarily Content-Type:text/html Location:http:/Transfer-Encoding:chunked Date:Thu,17 Oct 2002 20:40:23 GMT Server:Apache Coyote HTTP/1.1 Connect2d8 Java Web Services Developer,Filters,Filter Functions,对网络请求进行分析,对输入数据进行预处理阻止请求和响应的进行根据功能改动请求的头信息和数据体.根据功能改动响应的头信息和数据体.和其他网络资源协作,Filter Chain,Filter Setup in Web.xml,filter.jpgSimpleFilterASimpleFilter The first filter samplejwadbook.SimpleFilter param1 value1SimpleFilter/path/index.jsp,Filter Example,P109,Filter to Check Password,Security Filter,Action Listener,serlvet的事件监听器接口能够处理ServletContext,HttpSession,ServletRequest的生命周期和属性变化事件,Data Base Connection,Database,SQL,Data resource,JDBC Connection,JDBC Connection,1.Use JDBC DriverPut driver.jar in/common/system/libClass.forName(“com.pointbase.jdbc.jdbcUniversalDriver”);2.Make database connectionString pointbaseURL=jdbc:pointbase:server:/+host+/+databaseName;DriverManager.getConnection(pointbaseURL,username,password);,JDBC Connection(cont),3.Create SQL commandStatement stmt=connection.createStatement();String queryString=SELECT userid,lastname,addtime FROM Usertable ORDER BY userid;);ResultSet result=stmt.executeQuery(queryString);4.Analyze returned resultswhile(resultset.next()userID=resultset.getInt(userid);lastName=resultset.getString(lastname);addDate=resultset.getDate(addtime);5.Close database connectionconnection.close();,Connection Pool,Use Database Resources,1.Add JDBC driver2.Set server.xml(p160)3.Define database parameters DataSource reference to WorkflowAppDb defined in server.xml jdbc/WorkflowAppDb javax.sql.DataSource Container,Use Database Resources(cont),4.Use resource in ServletsDataSource dsource=(DataSource)ctx.lookup(java:comp/env/jdbc/WorkflowAppDb);if(dsource!=null)Connection conn=dsource.getConnection();if(conn!=null)Statement stmt=conn.createStatement();ResultSet rst=stmt.executeQuery(select*from Concept order by ConceptID);,JSP Technologies,A Simple JSP,Method:URI:Protocol:,JSP Result,Implicit Objects,JSP Grammar,DirectivesInclude Directive Page Directive Taglib Directive Scripting ElementsDeclarations Expressions Scriptlet,JSP Grammar,Comments Actions,An Example of Directives,Include Date JSP The current date and time aredate.jsp:,An Example of Scripting Elements,Welcome!Good Morning!12)&(currenthour Good Afternoon!Good Evening!,An Example of Actions,Include Date JSP at Request TimeThe current date and time aredate2.jsp:Getting date.,Use JavaBeans with JSP,JSP handles the face.JavaBean handles the logic and complexity.,A Example of Using JavaBean,Use Bean in JSP to Calculate 你一般每天需要 卡能量,How to Write the JavaBean,public class CalMeasure/properties defined public CalMeasure()public int getProperty()return this.proerty;public void setProperty(type value)this.property=value;Other method,Exercises 3,Programming:Create a database in pointbase,setup a table with the name,studentid,major,and contact of your group,display these contents with a servlet or jsp.(Extra credit:if user can modify the data-base information through a browser,I.E.add/update),