太原理工大學(xué)JSP報(bào)告_第1頁(yè)
太原理工大學(xué)JSP報(bào)告_第2頁(yè)
太原理工大學(xué)JSP報(bào)告_第3頁(yè)
太原理工大學(xué)JSP報(bào)告_第4頁(yè)
太原理工大學(xué)JSP報(bào)告_第5頁(yè)
已閱讀5頁(yè),還剩25頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

本科實(shí)驗(yàn)報(bào)告課程名稱(chēng):Web應(yīng)用開(kāi)發(fā)技術(shù):JSP實(shí)驗(yàn)工程:Web系統(tǒng)與技術(shù)實(shí)驗(yàn)實(shí)驗(yàn)地點(diǎn):專(zhuān)業(yè)班級(jí):學(xué)號(hào):學(xué)生姓名:指導(dǎo)教師:2023年1月13日實(shí)驗(yàn)1JSP常用內(nèi)置對(duì)象一、實(shí)驗(yàn)?zāi)康?.掌握J(rèn)SP常用內(nèi)置對(duì)象2.掌握J(rèn)SP數(shù)據(jù)庫(kù)編程技術(shù)。二、實(shí)驗(yàn)要求1.獨(dú)立完成實(shí)驗(yàn)2.書(shū)寫(xiě)實(shí)驗(yàn)報(bào)告書(shū)三、實(shí)驗(yàn)內(nèi)容1、做一下application對(duì)象應(yīng)用舉例?!睵158網(wǎng)頁(yè)計(jì)數(shù)器〕2、做一下P1655.8,5.93、做一下P152下面的session對(duì)象應(yīng)用舉例。實(shí)驗(yàn)2使用JDBC連接數(shù)據(jù)庫(kù)實(shí)驗(yàn)?zāi)康暮鸵?.掌握J(rèn)SP與數(shù)據(jù)庫(kù)的連接技術(shù)——JDBC2.掌握J(rèn)SP數(shù)據(jù)庫(kù)編程技術(shù)。1.獨(dú)立完成實(shí)驗(yàn)2.書(shū)寫(xiě)實(shí)驗(yàn)報(bào)告書(shū)二、實(shí)驗(yàn)內(nèi)容和原理利用數(shù)據(jù)庫(kù)建立一個(gè)職工數(shù)據(jù)表,然后通過(guò)JDBC編寫(xiě)一系列基于Web方式的JSP程序,來(lái)對(duì)職工數(shù)據(jù)表的數(shù)據(jù)庫(kù)進(jìn)行添加、查詢(xún)等功能,要求在網(wǎng)頁(yè)上顯示出來(lái)。主要儀器設(shè)備HP筆記本電腦Eclipse軟件開(kāi)發(fā)工具Accesss數(shù)據(jù)庫(kù)操作方法與實(shí)驗(yàn)步驟1.建立數(shù)據(jù)庫(kù)建立名為employDB的Access數(shù)據(jù)庫(kù),在庫(kù)中建立employb表,字段包括:字段名中文含義數(shù)據(jù)類(lèi)型id職工號(hào)文本name姓名文本sex性別文本salary工資數(shù)值title職稱(chēng)文本對(duì)數(shù)據(jù)表employb,輸入5條記錄,如課本P191習(xí)題7.7所示。2.創(chuàng)立數(shù)據(jù)源名翻開(kāi)數(shù)據(jù)源〔ODBC〕;添加MicrosoftAccessDriver(*.mdb)數(shù)據(jù)源驅(qū)動(dòng)程序。數(shù)據(jù)源名稱(chēng):employDB;更改默認(rèn)數(shù)據(jù)庫(kù),選擇employDB。3、編寫(xiě)JSP程序,共有3個(gè)程序。程序1:課本P191習(xí)題7.8。程序2:課本P191習(xí)題7.10?!舶颜n本習(xí)題7.10改為:編寫(xiě)JSP程序讀取習(xí)題7.8的職工表,在該表最后插入一條新記錄〔由自己設(shè)計(jì)〕,并顯示插入后的表的內(nèi)容。程序3:課本P191習(xí)題7.11。4.運(yùn)行所編寫(xiě)的應(yīng)用程序啟動(dòng)MyEclipse,建立工程exp1,將編寫(xiě)的程序放入該工程的WebRoot下,右鍵點(diǎn)擊WebRoot,選擇“刷新〞。運(yùn)行時(shí),執(zhí)行3個(gè)步驟。第1步,把程序部署在Tomcat效勞器上。第2步,啟動(dòng)Tomcat效勞器。第3步,在MyEclipseWebBrowser瀏覽器或外部的IE,或360瀏覽器中輸入運(yùn)行的URL,如://localhost:8080/exp1/xiti7_8.jsp可運(yùn)行程序1,即課本P191習(xí)題7.8,并查看運(yùn)行結(jié)果。實(shí)驗(yàn)數(shù)據(jù)記錄和處理1:<%@pagelanguage="java"import="java.sql.*"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"":///TR/html4/loose.dtd"><html><head><meta-equiv="Content-Type"content="text/html;charset=ISO-8859-1"><title>Access數(shù)據(jù)庫(kù)</title></head><body>從Access數(shù)據(jù)庫(kù)讀取表數(shù)據(jù):<HR><TABLEborder=1><TR><TD>職工號(hào)</TD><TD>姓名</TD><TD>性別</TD><TD>工資</TD><TD>職稱(chēng)</TD></TR><%StringdriverName="sun.jdbc.odbc.JdbcOdbcDriver";StringdbName="employDB";StringtableName="employb";StringuserName="";StringuserPasswd="";StringconURL="jdbc:odbc:"+dbName;Stringsql="SELECT*FROM"+tableName;Class.forName(driverName);Connectioncon=DriverManager.getConnection(conURL,userName,userPasswd);Statements=con.createStatement();ResultSetrs=s.executeQuery(sql);while(rs.next()){ out.println("<TR>"); out.println("<TD>"+rs.getString("id")+"</TD>"); out.println("<TD>"+rs.getString("name")+"</TD>"); out.println("<TD>"+rs.getString("sex")+"</TD>"); out.println("<TD>"+rs.getInt("salary")+"</TD>"); out.println("<TD>"+rs.getString("title")+"</TD>"); out.println("</TR>");}rs.close();%></TABLE><%Stringup="updateemploybsetsalary=salary+50";PreparedStatementps=con.prepareStatement(up);ps.executeUpdate();ps.close();%><h3>修改后的數(shù)據(jù)庫(kù)記錄</h3><hr><tableborder=1><TR><TD>職工號(hào)</TD><TD>姓名</TD><TD>性別</TD><TD>工資</TD><TD>職稱(chēng)</TD></TR><%Statements1=con.createStatement();ResultSetrs1=s.executeQuery(sql);while(rs1.next()){out.println("<TR>");out.println("<TD>"+rs1.getString("id")+"</TD>");out.println("<TD>"+rs1.getString("name")+"</TD>");out.println("<TD>"+rs1.getString("sex")+"</TD>");out.println("<TD>"+rs1.getInt("salary")+"</TD>");out.println("<TD>"+rs1.getString("title")+"</TD>");out.println("</TR>");}rs1.close();s1.close();con.close();%></table></body></html>2:<%@pagelanguage="java"import="java.sql.*"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"":///TR/html4/loose.dtd"><html><head><meta-equiv="Content-Type"content="text/html;charset=ISO-8859-1"><title>Access數(shù)據(jù)庫(kù)</title></head><body>從Access數(shù)據(jù)庫(kù)讀取表數(shù)據(jù):<HR><TABLEborder=1><TR><TD>職工號(hào)</TD><TD>姓名</TD><TD>性別</TD><TD>工資</TD><TD>職稱(chēng)</TD></TR><%StringdriverName="sun.jdbc.odbc.JdbcOdbcDriver";StringdbName="employDB";StringtableName="employb";StringuserName="";StringuserPasswd="";StringconURL="jdbc:odbc:"+dbName;Stringsql="SELECT*FROM"+tableName;Class.forName(driverName);Connectioncon=DriverManager.getConnection(conURL,userName,userPasswd);Statements=con.createStatement();ResultSetrs=s.executeQuery(sql);while(rs.next()){ out.println("<TR>"); out.println("<TD>"+rs.getString("id")+"</TD>"); out.println("<TD>"+rs.getString("name")+"</TD>"); out.println("<TD>"+rs.getString("sex")+"</TD>"); out.println("<TD>"+rs.getInt("salary")+"</TD>"); out.println("<TD>"+rs.getString("title")+"</TD>"); out.println("</TR>");}rs.close();%></TABLE><%PreparedStatementps=con.prepareStatement("insertintoemploybvalues(?,?,?,?,?)");ps.setString(1,"0005");ps.setString(2,"王剛");ps.setString(3,"男");ps.setString(4,"2000");ps.setString(5,"工程師");ps.execute();ps.close();%><h3>修改后的數(shù)據(jù)庫(kù)記錄</h3><hr><tableborder=1><TR><TD>職工號(hào)</TD><TD>姓名</TD><TD>性別</TD><TD>工資</TD><TD>職稱(chēng)</TD></TR><%Statements1=con.createStatement();ResultSetrs1=s.executeQuery(sql);while(rs1.next()){out.println("<TR>");out.println("<TD>"+rs1.getString("id")+"</TD>");out.println("<TD>"+rs1.getString("name")+"</TD>");out.println("<TD>"+rs1.getString("sex")+"</TD>");out.println("<TD>"+rs1.getInt("salary")+"</TD>");out.println("<TD>"+rs1.getString("title")+"</TD>");out.println("</TR>");}rs1.close();s1.close();con.close();%></table></body></html>3:<%@pagelanguage="java"import="java.sql.*"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"":///TR/html4/loose.dtd"><html><head><meta-equiv="Content-Type"content="text/html;charset=ISO-8859-1"><title>Access數(shù)據(jù)庫(kù)</title></head><body>從Access數(shù)據(jù)庫(kù)讀取表數(shù)據(jù):<HR><TABLEborder=1><TR><TD>職工號(hào)</TD><TD>姓名</TD><TD>性別</TD><TD>工資</TD><TD>職稱(chēng)</TD></TR><%StringdriverName="sun.jdbc.odbc.JdbcOdbcDriver";StringdbName="employDB";StringtableName="employb";StringuserName="";StringuserPasswd="";StringconURL="jdbc:odbc:"+dbName;Stringsql="SELECT*FROM"+tableName;Class.forName(driverName);Connectioncon=DriverManager.getConnection(conURL,userName,userPasswd);Statements=con.createStatement();ResultSetrs=s.executeQuery(sql);while(rs.next()){ out.println("<TR>"); out.println("<TD>"+rs.getString("id")+"</TD>"); out.println("<TD>"+rs.getString("name")+"</TD>"); out.println("<TD>"+rs.getString("sex")+"</TD>"); out.println("<TD>"+rs.getInt("salary")+"</TD>"); out.println("<TD>"+rs.getString("title")+"</TD>"); out.println("</TR>");}rs.close();%></TABLE><%Stringa="DELETEFROMemploybwhereid='1001'ORid='1005'";PreparedStatementps=con.prepareStatement(a);ps.execute();ps.close();%><h3>修改后的數(shù)據(jù)庫(kù)記錄</h3><hr><tableborder=1><TR><TD>職工號(hào)</TD><TD>姓名</TD><TD>性別</TD><TD>工資</TD><TD>職稱(chēng)</TD></TR><%Statements1=con.createStatement();ResultSetrs1=s.executeQuery(sql);while(rs1.next()){out.println("<TR>");out.println("<TD>"+rs1.getString("id")+"</TD>");out.println("<TD>"+rs1.getString("name")+"</TD>");out.println("<TD>"+rs1.getString("sex")+"</TD>");out.println("<TD>"+rs1.getInt("salary")+"</TD>");out.println("<TD>"+rs1.getString("title")+"</TD>");out.println("</TR>");}rs1.close();s1.close();con.close();%></table></body></html>實(shí)驗(yàn)結(jié)果與分析1:七、討論、心得使用JDBC連接數(shù)據(jù)庫(kù),實(shí)驗(yàn)3JSP與JavaBean一、實(shí)驗(yàn)?zāi)康牧私釰avaBean的工作原理,學(xué)會(huì)使用和配置JavaBean程序。二、實(shí)驗(yàn)要求1.獨(dú)立完成實(shí)驗(yàn)2.書(shū)寫(xiě)實(shí)驗(yàn)報(bào)告書(shū)三、實(shí)驗(yàn)內(nèi)容1、編寫(xiě)JSP及JavaBean程序,共有3個(gè)程序。程序1:課本P217習(xí)題8.10。程序2:課本P217習(xí)題8.12。〔把課本習(xí)題8.12改為:編寫(xiě)JavaBean程序讀取習(xí)題8.10的職工表,在該表最后插入一條新記錄〔由自己設(shè)計(jì)〕,并用JSP頁(yè)面顯示插入后的表的內(nèi)容。〕程序3:課本P217習(xí)題8.13。4.運(yùn)行所編寫(xiě)的應(yīng)用程序啟動(dòng)MyEclipse,建立工程exp2,將編寫(xiě)的程序放入該工程下。運(yùn)行時(shí),執(zhí)行3個(gè)步驟。第1步,把程序部署在Tomcat效勞器上。第2步,啟動(dòng)Tomcat效勞器。第3步,在MyEclipseWebBrowser瀏覽器或外部的IE,或360瀏覽器中輸入運(yùn)行的URL,如://localhost:8080/exp1/xiti8_10.jsp可運(yùn)行程序1,即課本P217習(xí)題8.10,并查看運(yùn)行結(jié)果。四:實(shí)驗(yàn)數(shù)據(jù)記錄和處理packagedatabase;importjava.sql.*;publicclassDataBaseConn{publicstaticConnectiongetConnection() { Connectioncon=null; StringJDriver="sun.jdbc.odbc.JdbcOdbcDriver"; StringconURL="jdbc:odbc:employDB";try{ Class.forName(JDriver); con=DriverManager.getConnection(conURL);}catch(Exceptione){System.err.println(e.getMessage());}returncon; }}packagedatabase;importjava.sql.*;publicclassQueryUpdate{ Statementstmt=null; Connectioncon=null;publicQueryUpdate(){this.con=DataBaseConn.getConnection();}publicvoidexecuteUpdate(Stringsql){try {stmt=con.createStatement();stmt.executeUpdate(sql); }catch(SQLExceptionex) { System.err.println(ex.getMessage()); }}publicResultSetexecuteQuery(Stringsql){ ResultSetrs=null;try{stmt=con.createStatement(); rs=stmt.executeQuery(sql); }catch(SQLExceptionex){ System.err.println("executeQuery:"+ex.getMessage()); } returnrs;}publicvoidclose(){try{stmt.close();con.close(); }catch(SQLExceptionex) { System.err.println(ex.getMessage()); }}}1:<%@pagelanguage="java"import="java.sql.*"import="DB.*"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"":///TR/html4/loose.dtd"><html><head><meta-equiv="Content-Type"content="text/html;charset=ISO-8859-1"><title>表數(shù)據(jù)的修改</title></head><body><jsp:useBeanid="myBean"scope="page"class="database.QueryUpdate"/><h4>原始表為</h4><TABLEborder=1><TR><TD>職工號(hào)</TD><TD>姓名</TD><TD>性別</TD><TD>工資</TD><TD>職稱(chēng)</TD></TR><%Stringsql="SELECT*FROMemployb";ResultSetrs=myBean.executeQuery(sql);while(rs.next()){ out.println("<TR>"); out.println("<TD>"+rs.getString("id")+"</TD>"); out.println("<TD>"+rs.getString("name")+"</TD>"); out.println("<TD>"+rs.getString("sex")+"</TD>"); out.println("<TD>"+rs.getInt("salary")+"</TD>"); out.println("<TD>"+rs.getString("title")+"</TD>"); out.println("</TR>");}rs.close();%></TABLE><% sql="updateemploybsetsalary=salary+50"; myBean.executeQuery(sql);%><h4>更改后的表</h4><TABLEborder=1><TR><TD>職工號(hào)</TD><TD>姓名</TD><TD>性別</TD><TD>工資</TD><TD>職稱(chēng)</TD></TR><%sql="SELECT*FROMemployb";rs=myBean.executeQuery(sql);while(rs.next()){ out.println("<TR>"); out.println("<TD>"+rs.getString("id")+"</TD>"); out.println("<TD>"+rs.getString("name")+"</TD>"); out.println("<TD>"+rs.getString("sex")+"</TD>"); out.println("<TD>"+rs.getInt("salary")+"</TD>"); out.println("<TD>"+rs.getString("title")+"</TD>"); out.println("</TR>");}rs.close();myBean.close();%></TABLE></body></html>2:<%@pagelanguage="java"import="java.sql.*"import="DB.*"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"":///TR/html4/loose.dtd"><html><head><meta-equiv="Content-Type"content="text/html;charset=ISO-8859-1"><title>表數(shù)據(jù)的修改</title></head><body><jsp:useBeanid="myBean"scope="page"class="database.QueryUpdate"/><h4>原始表為</h4><TABLEborder=1><TR><TD>職工號(hào)</TD><TD>姓名</TD><TD>性別</TD><TD>工資</TD><TD>職稱(chēng)</TD></TR><%Stringsql="SELECT*FROMemployb";ResultSetrs=myBean.executeQuery(sql);while(rs.next()){ out.println("<TR>"); out.println("<TD>"+rs.getString("id")+"</TD>"); out.println("<TD>"+rs.getString("name")+"</TD>"); out.println("<TD>"+rs.getString("sex")+"</TD>"); out.println("<TD>"+rs.getInt("salary")+"</TD>"); out.println("<TD>"+rs.getString("title")+"</TD>"); out.println("</TR>");}rs.close();%></TABLE><% sql="insertintoemploybvalues="+"'0001','王剛','男','2000','工程師'"; myBean.executeUpdate(sql);%><h4>更改后的表</h4><TABLEborder=1><TR><TD>職工號(hào)</TD><TD>姓名</TD><TD>性別</TD><TD>工資</TD><TD>職稱(chēng)</TD></TR><%sql="SELECT*FROMemployb";rs=myBean.executeQuery(sql);while(rs.next()){ out.println("<TR>"); out.println("<TD>"+rs.getString("id")+"</TD>"); out.println("<TD>"+rs.getString("name")+"</TD>"); out.println("<TD>"+rs.getString("sex")+"</TD>"); out.println("<TD>"+rs.getInt("salary")+"</TD>"); out.println("<TD>"+rs.getString("title")+"</TD>"); out.println("</TR>");}rs.close();myBean.close();%></TABLE></body></html>3:<%@pagelanguage="java"import="java.sql.*"import="DB.*"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"":///TR/html4/loose.dtd"><html><head><meta-equiv="Content-Type"content="text/html;charset=ISO-8859-1"><title>表數(shù)據(jù)的修改</title></head><body><jsp:useBeanid="myBean"scope="page"class="database.QueryUpdate"/><h4>原始表為</h4><TABLEborder=1><TR><TD>職工號(hào)</TD><TD>姓名</TD><TD>性別</TD><TD>工資</TD><TD>職稱(chēng)</TD></TR><%Stringsql="SELECT*FROMemployb";ResultSetrs=myBean.executeQuery(sql);while(rs.next()){ out.println("<TR>"); out.println("<TD>"+rs.getString("id")+"</TD>"); out.println("<TD>"+rs.getString("name")+"</TD>"); out.println("<TD>"+rs.getString("sex")+"</TD>"); out.println("<TD>"+rs.getInt("salary")+"</TD>"); out.println("<TD>"+rs.getString("title")+"</TD>"); out.println("</TR>");}rs.close();%></TABLE><% sql="deletefromemploybwhereid='1001'orid='1005'"; myBean.executeQuery(sql);%><h4>更改后的表</h4><TABLEborder=1><TR><TD>職工號(hào)</TD><TD>姓名</TD><TD>性別</TD><TD>工資</TD><TD>職稱(chēng)</TD></TR><%sql="SELECT*FROMemployb";rs=myBean.executeQuery(sql);while(rs.next()){ out.println("<TR>"); out.println("<TD>"+rs.getString("id")+"</TD>"); out.println("<TD>"+rs.getString("name")+"</TD>"); out.println("<TD>"+rs.getString("sex")+"</TD>"); out.println("<TD>"+rs.getInt("salary")+"</TD>"); out.println("<TD>"+rs.getString("title")+"</TD>"); out.println("</TR>");}rs.close();myBean.close();%></TABLE></body></html>五:實(shí)驗(yàn)結(jié)果與分析六:討論、心得實(shí)驗(yàn)4Servlet一、實(shí)驗(yàn)?zāi)康牧私釹ervlet的工作原理,學(xué)會(huì)使用和配置Servlet程序。二、實(shí)驗(yàn)要求1.獨(dú)立完成實(shí)驗(yàn)2.書(shū)寫(xiě)實(shí)驗(yàn)報(bào)告書(shū)三、實(shí)驗(yàn)內(nèi)容1、編寫(xiě)Servlet程序,共有3個(gè)程序。 程序1:課本P234第9章第5節(jié)Servlet模式的留言板案例。程序2:課本P247習(xí)題9.9。程序3:課本P247習(xí)題9.10。4.運(yùn)行所編寫(xiě)的應(yīng)用程序啟動(dòng)MyEclipse,建立工程exp3,將編寫(xiě)的程序放入該工程下。運(yùn)行時(shí),執(zhí)行3個(gè)步驟。第1步,把程序部署在Tomcat效勞器上。第2步,啟動(dòng)Tomcat效勞器。第3步,在MyEclipseWebBrowser瀏覽器或外部的IE,或360瀏覽器中輸入運(yùn)行的URL,并查看運(yùn)行結(jié)果。四:實(shí)驗(yàn)數(shù)據(jù)記錄和處理1:packageex3;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet..Servlet;importjavax.servlet..ServletRequest;importjavax.servlet..ServletResponse;@WebServlet("/c99")publicclassc99extendsServlet{ publicc99(){ super(); } publicvoiddestroy(){ super.destroy(); } publicvoiddoGet(ServletRequestreq,ServletResponseres) throwsServletException,IOException { res.setContentType("text/html;charset=UTF-8"); PrintWriterout=res.getWriter(); out.println("<html><head></head><body>"); out.println("<h3>我的名字</h3>"); out.println("張清華"); out.println("</body></html>"); } publicvoiddoPost(ServletRequestreq,ServletResponseres) throwsServletException,IOException { doPost(req,res); }}2:<!DOCTYPEhtml><html><head><metacharset="UTF-8"><title>在表單中調(diào)用Servlet</title></head><body><algin=center>在表單中調(diào)用Servlet</algin><hr><formmethod="post"action="c90"><p>被加數(shù):<inputtype="text"name="plus1"></p><p>加數(shù):<inputtype="text"name="plus2"></p><inputtype="submit"value="計(jì)算"><inputtype="reset"value="去除"></form></body></html>packageex3;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet..Servlet;importjavax.servlet..ServletRequest;importjavax.servlet..ServletResponse;@WebServlet("/c90")publicclassc90extendsServlet{ publicvoiddoGet(ServletRequestreq,ServletResponseres) throwsServletException,IOException{ res.setContentType("text/html"); doPost(req,res); } publicvoiddoPost(ServletRequestreq,ServletResponseres) throwsServletException,IOException{ res.setContentType("text/html;charset=UTF-8"); PrintWriterout=res.getWriter(); Integera=Integer.parseInt(req.getParameter("plus1")); Integerb=Integer.parseInt(req.getParameter("plus2")); Integerc=a+b; out.println("<html><head></head><body>"); out.println("<h3>輸出結(jié)果為:</h3>"); out.println(c+"<br>"); out.println("</body></html>"); }}3:packagemessage;importjavax.servlet.*;importjavax.servlet..*;importjava.sql.*;importjava.io.*;{ publicAddMessageServlet(){ StringJDriver="sun.jdbc.odbc,JdbcOdbcDriver"; StringconURL="jdbc:odbc:message"; try{ Class.forName(JDriver); con=DriverManager.getConnection(conURL); } catch(Exceptione) {System.err.println(e.getMessage());} }privateConnectioncon;protectedvoiddoGet(ServletRequestrequest,ServletResponseresponse)throwsServletException,IOException{ byteb1[]=request.getParameter("name").getBytes("ISO-8859-1"); Stringna=newString(b1); byteb2[]=request.getParameter("email").getBytes("ISO-8859-1");Stringem=newString(b2);byteb3[]=request.getParameter("title").getBytes("ISO-8859-1");Stringti=newString(b3);byteb4[]=request.getParameter("content").getBytes("ISO-8859-1");Stringco=newString(b4);if(na==null)na="";if(ti==null)ti="";if(co==null)co="";if(em==null)em="";try{ PreparedStatementstm=con.prepareStatement("insertintoMessageTablevalue(?,?,?,?)"); stm.setString(1,ti); stm.setString(2,na); if(em.length()==0)stm.setString(3,null); elsestm.setString(3,em); stm.setString(4,co); try{stm.executeQuery();} catch(Exceptione){} RequestDispatcherrequestDispatcher=request.getRequestDispatcher("ViewMessages_servlet"); requestDispatcher.forward(request,response);}catch(Exceptione){e.printStackTrace();}}protectedvoiddoPost(ServletRequestrequest,ServletResponseresponse)throwsServletException,IOException{doGet(request,response);}}packagemessage;publicclassMessageDataBean{privateStringname,email,title,content;publicStringgetName(){returnname;}publicvoidsetName(Stringname){=name;}publicStringgetEmail(){returnemail;}publicvoidsetEmail(Stringemail){this.email=email;}publicStringgetTitle(){returntitle;}publicvoidsetTitle(Stringtitle){this.title=title;}publicStringgetContent(){returncontent;}publicvoidsetContent(Stringcontent){this.content=content;}}packagemessage;importjava.io.*;importjava.sql.*;importjava.util.ArrayList;importjava.util.Collection;importjavax.servlet.*;importjavax.servlet..*;publicclassViewMessages_servletextendsjavax.servlet..Servletimplementsjavax.servlet.Servlet{ publicViewMessages_servlet(){ StringJDriver="sun.jdbc.odbc.JdbcOdbcDriver"; StringconURL="jdbc:odbc:message"; try{ Class.forName(JDriver); con=DriverManager.getConnection(conURL); }catch(Exceptione){ System.err.println(e.getMessage()); } } privateConnectioncon; publicvoiddoGet(ServletRequestrequest,ServletResponseresponse) throwsServletException,IOException{Collectionret=newArrayList();try{ Statementstm=con.createStatement(); ResultSetresult=stm.executeQuery("selectcount(*)fromMessageTable"); intmessage_count=0; if(result.next()){ message_count=result.getInt(1); result.close(); } if(message_count>0){ result=stm.executeQuery("select*fromMessageTable"); while(result.next()){ Stringtitle=result.getString("title"); Stringname=result.getString("name"); Stringemail=result.getString("email"); Stringcontent=result.getString("content"); MessageDataBeanmessage=newMessageDataBean(); message.setTitle(title); message.setName(name); message.setEmail(email); message.setContent(content); ret.add(message); } result.close(); stm.close(); } request.setAttribute("messages",ret); RequestDispatcherrequestDispatcher=request.getRequestDispatcher("../viewMessages.jsp"); requestDispatcher.forward(request,response); }catch(Exceptione){e.printStackTrace();} }publicvoiddoPost(ServletRequestrequest,ServletResponseresponse) throwsServletException,IOException{doGet(request,response); }}<!DOCTYPEhtml><html><head><title>留言板</title></head><body><center>留言板</center><formaction="servlet/AddMessageServlet"><tableborder=1align="center"><tr><td>姓名:</td><td><inputtype="text"name="name"size=25></td></tr><tr><td>E-mail:</td><td><inputtype="text"name="email"size=25></td></tr><tr><td>主題</td><td><inputtype="text"name="title"size=25></td></tr><tr><td>留言:</td><td><textareaname="content"rows=7cols25></textarea></td><tr><tdcolspan=3><tablealign="center"width="100%"cellspacing="0"cellpading="0"></td><tr><tdalgin="center"><inputtype="submit"value="提交留言"></td><tdalign="center"><ahref="servlet/ViewMessages_servlet"><fortsize=2>查看留言</fort></a></td><tdalign="center"><inputtype="reset"value="重新填寫(xiě)"></td></tr></table></td></tr></table></form></body></html><%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%><%@pageimport="message.MessageDataBean"%><html><head><title>顯示留言</title></head><body><palign="center">所有留言</p><tablealign="center"width="80%"border=1><%intmessage_count=0;Collectionmessage=(Collection)request.getAttribute("messages");Iteratorit=message.iterator();while(it.hasNext()){MessageDataBeanmg=(MessageDataBean)it.next();%><tr><tdwidth="20%">留言人:</td><tdwidth="23%"><%=mg.getName()%></td><tdwidth="58%"align="center"><%out.println("<ahref=mailto:"+mg.getEmail()+">"+mg.getEmail()+"</a>");%></td></tr><tr><tdwidth="20%">主題:</td><tdcolspan="3"><%=mg.getTitle()%></td></tr><tr><tdwidth="20%">內(nèi)容:</td><tdcolspan="3"><%=mg.getContent()%></td></tr><%message_count++;}%></table><palign="center"><ahref="Messages.html">我要留言</a></p></body></html>五:實(shí)驗(yàn)結(jié)果與分析六:討論、心得熟練使用jsp+javabean+servlet,輸出想要輸出的內(nèi)容。實(shí)驗(yàn)5JSP中的文件操作一、實(shí)驗(yàn)?zāi)康牧私釰SP文件操作的工作原理,學(xué)會(huì)文件操作。二、實(shí)驗(yàn)要求1.獨(dú)立完成實(shí)驗(yàn)2.書(shū)寫(xiě)實(shí)驗(yàn)報(bào)告書(shū)三、實(shí)驗(yàn)內(nèi)容1、編寫(xiě)程序。程序:課本P270習(xí)題10.11。提示:在D盤(pán)建立一個(gè)名為score.txt的文本文件,內(nèi)容為:1,78,82,742,82,88,983,98,89,904,68,78,775,70,86,88共4列,分別表示5個(gè)學(xué)生的學(xué)號(hào)以及數(shù)學(xué),英語(yǔ),計(jì)算機(jī)的成績(jī),要求求出5個(gè)學(xué)生數(shù)學(xué)、英語(yǔ)、計(jì)算機(jī)的最小值,最大值及平均值。局部程序如下:Filefl=newFile("d:/score.txt");RandomAccessFilefrw=newRandomAccessFile(fl,"rw");Srtinginfo=null;String[]rec=nullwhile((info=frw.readLine())!=null){ System.out.println(info); rec=info.split(",");……以上程序解釋?zhuān)鹤x入一行后,以“,〞為分隔符,把1行切割為4個(gè)局部,存入數(shù)組中。2.運(yùn)行所編寫(xiě)的應(yīng)用程序啟動(dòng)MyEclipse,建立工程exp4,將編寫(xiě)的程序放入該工程下。運(yùn)行時(shí),執(zhí)行3個(gè)步驟。第1步,把程序部署在Tomcat效勞器上。第2步,啟動(dòng)Tomcat效勞器。第3步,在MyEclipseWebBrowser瀏覽器或外部的IE,或360瀏覽器中輸入運(yùn)行的URL,并查看運(yùn)行結(jié)果。四:實(shí)驗(yàn)數(shù)據(jù)記錄和處理<%@pagelanguage="java"import="java.util.*"contentType="text/html;charset=utf-8"%><%@pageimport="java.io.*"%><html><head><title>實(shí)驗(yàn)五10.11</title></head><body> <% Filefl=newFile("d:/score.txt"); RandomAccessFilefrw=newRandomAccessFile(fl,"rw"); Stringinfo=null; String[]rec=null; while((info=frw.readLine())!=null){ System.out.println(info); rec=info.split(","); int[]r=newint[4]; r[0]=Integer.parseInt(rec[0]); r[1]=Integer.parseInt(rec[1]); r[2]=Integer.parseInt(rec[2]); r[3]=Integer.parseInt(rec[3]); doubleavg=(r[1]+r[2]+r[3])/3;%><h4>學(xué)號(hào):<%=r[0]%></h4><h4>平均成績(jī):<%=avg%></h4><h4>最大成績(jī):<%=Math.max(r[1],Math.max(r[2],r[3]))%></h4><h4>最小成績(jī):<%=Math.min(r[1],Math.min(r[2],r[3]))%></h4><hr><%}%></body></html>五:實(shí)驗(yàn)結(jié)果與分析六:討論、心得熟練掌握了JSP文件操作的工作原理,學(xué)會(huì)文件操作實(shí)驗(yàn)6XML操作一、實(shí)驗(yàn)?zāi)康恼莆誜ML的語(yǔ)法,學(xué)會(huì)XML操作。二、實(shí)驗(yàn)要求1.獨(dú)立完成實(shí)驗(yàn)2.書(shū)寫(xiě)實(shí)驗(yàn)報(bào)告書(shū)三、實(shí)驗(yàn)內(nèi)容1、編寫(xiě)程序。1〕程序:課本P290習(xí)題11.8。要求:對(duì)應(yīng)的XML文檔內(nèi)容為:<?xmlversion="1.0"encoding="GB2312"?> <?xml-stylesheettype=“text/xsl〞href=“test.xsl〞?> <addresslist> <linkman> <name>李興華</name>

<id>001</id> </linkman> </addresslist>要求:李興華和001之間間隔1行。2〕程序:課本P290習(xí)題11.10。<?xmlversion="1.0"encoding="GBK"?><addresslist> <linkman> <name>李興華</name> <email>mldnqa@163</email> </linkman> <linkman> <name>MLDN</name> <email>mldnkf@163</email> </linkman></addresslist>要求解析后輸出XML的內(nèi)容。2.運(yùn)行所編寫(xiě)的應(yīng)用程序啟動(dòng)MyEclipse,建立工程exp5,將編寫(xiě)的程序放入該工程下。運(yùn)行程序,查看運(yùn)行結(jié)果并進(jìn)行分析。四:實(shí)驗(yàn)數(shù)據(jù)記錄和處理1.源代碼:MessagesTables.html<html><head> <title>實(shí)驗(yàn)六</title></head><body> <h1align="center">實(shí)驗(yàn)六</h1> <formaction="servlet/exp5servlet"method="post"> <tableborder=1align="center"> <tr> <td>姓名:</td> <td><inputtype="text"name="name"size=25></td> </tr> <tr><td>ID:</td> <td><inputtype="text"name="id"size=25></td>

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論