JSP程序設(shè)計實驗報告-2_第1頁
JSP程序設(shè)計實驗報告-2_第2頁
JSP程序設(shè)計實驗報告-2_第3頁
JSP程序設(shè)計實驗報告-2_第4頁
JSP程序設(shè)計實驗報告-2_第5頁
已閱讀5頁,還剩27頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

JSP程序設(shè)計實驗指導(dǎo)PAGEPAGE1實驗報告6月8日交,源代碼不必全寫,給的源代碼僅供參考課程設(shè)計6月8日交(至少7頁)課程設(shè)計應(yīng)該:

1.系統(tǒng)總體設(shè)計(功能設(shè)計、數(shù)據(jù)庫設(shè)計)2.界面設(shè)計(要有圖)3.系統(tǒng)實現(xiàn)(部分源代碼)

4.運行測試

附交源程序

實驗一JSP編程環(huán)境3月9日實驗?zāi)康拇罱╓eb編程環(huán)境,能正確安裝配置java運行環(huán)境、WEB服務(wù)器掌握正確運行jsp文件的方法實驗內(nèi)容安裝并配置java運行環(huán)境JDK和JRE安裝Web服務(wù)器tomcat,配置Tomcat服務(wù)器實驗步驟JDK、JRE安裝結(jié)果:Classpath和path、Java_Home設(shè)置path為java的bin目錄,Classpath為jre中rt.jar目錄,Java_Home為及java安裝目錄安裝并配置Tomcat安裝Tomcat服務(wù)器后并啟動之。即:

啟動Tomcat服務(wù)器目錄中bin\startup.bat文件。在“開始菜單--運行”中輸入http://localhost:8080/點擊確定。第一個JSP文件在Tomcat服務(wù)器目錄webapps\ROOT下:新建一個文本文件,改名為first.jsp.右擊此文件,選擇打開方式為記事本。3)編輯內(nèi)容如下:

<%@pagecontentType="text/html;charset=GB2312"%><HTML><BODYBGCOLOR=cyan><FONTSize=4><P>JSP頁面的測試<%intx=100,y=500,z;z=x+y;%><P>x+y的和為:<%=z%></FONT></BODY></HTML>4)在地址欄或者運行菜單輸入http://localhost:8080/first.jsp回車,然后觀察運行結(jié)果要求,理解常用html標(biāo)簽的含義:<html><body><p><br><hr><table><tr><td><img><a><form><input><font><ol><ul><li>了解以下屬性含義:alignbgcolorsize根據(jù)各人情況,最好能了解DIV+css布局方式實驗二、jsp頁面和jsp標(biāo)記3月16實驗?zāi)康?、掌握J(rèn)SP的頁面的基本結(jié)構(gòu),讓學(xué)生掌握怎樣在JSP頁面中使用成員變量,怎樣使用Java程序片、Java表達(dá)式。2、掌握J(rèn)SP的指令標(biāo)記和動作標(biāo)記,讓學(xué)生掌握怎樣在JSP頁面中使用include標(biāo)記動態(tài)加載文件;使用forward實現(xiàn)頁面的轉(zhuǎn)向。實驗內(nèi)容1、編寫兩個JSP頁面,分別為inputName.jsp和people.jsp。(1)inputName.jsp的具體要求該頁面有一個表單,用戶通過該表單輸入自己的姓名并提交給people.jsp頁面。(2)people.jsp頁面在程序片中獲取inputName.jsp頁面提交的姓名,然后調(diào)用judge()創(chuàng)建person對象、調(diào)用addPerson方法將用戶的姓名添加到成員變量person末尾。如果inputName.jsp頁面沒有提交姓名,或姓名含有的字符個數(shù)大于10,就使用<jsp:forwardpage="要轉(zhuǎn)向的頁面"/>標(biāo)記將用戶轉(zhuǎn)到inputName.jsp頁面。2、編寫四個JSP頁面:one.jsp、two.jsp、three.jsp和error.jsp。one.jsp、two.jsp和three.jsp頁面都含有一個導(dǎo)航條,以便讓用戶方便地單擊超鏈接訪問這三個頁面,要求這三個頁面通過使用include動作標(biāo)記動態(tài)加載導(dǎo)航條文件head.txt。導(dǎo)航條文件head.txt的內(nèi)容如下所示:head.txt<%@pagecontentType="text/html;charset=GB2312"%><tablecellSpacing="1"cellPadding="1"width="60%"align="center"

border="0"><trvalign="bottom"><td><Ahref="one.jsp"><fontsize=3>one.jsp頁面</font></A></td><td><Ahref="two.jsp"><fontsize=3>two.jsp頁面</font></A></td><td><Ahref="three.jsp"><fontsize=3>three.jsp頁面</font></A></td></tr></Font></table>(1)one.jsp的具體要求要求one.jsp頁面有一個表單,用戶使用該表單可以輸入一個1~100之間的整數(shù),并提交給該頁面;如果輸入的整數(shù)在50~100之間(不包括50)就轉(zhuǎn)向three.jsp,如果在1~50之間就轉(zhuǎn)向two.jsp;如果輸入不符合要求就轉(zhuǎn)向error.jsp。要求forward標(biāo)記在實現(xiàn)頁面轉(zhuǎn)向時,使用param子標(biāo)記將整數(shù)傳遞到轉(zhuǎn)向的two.jsp或three.jsp頁面,將有關(guān)輸入錯誤傳遞到轉(zhuǎn)向的error.jsp頁面。(2)two.jsp、three.jsp和error.jsp的具體要求要求two.jsp和three.jsp能輸出one.jsp傳遞過來的值,并顯示一幅圖像,該圖像的寬和高剛好是one.jsp頁面?zhèn)鬟f過來的值。error.jsp頁面能顯示有關(guān)的錯誤信息和一幅圖像。one.jsptwo.jspthree.jsperror.jsp圖1-9顯示錯誤信息5.參考代碼inputName.jsp<%@pagecontentType="text/html;charset=GB2312"%><HTML><BODYbgcolor=cyan><FONTsize=3><FORMaction="people.jsp"method=getname=form>請輸入姓名:<INPUTtype="text"name="name"><BR><INPUTTYPE="submit"value="送出"name=submit></FORM></BODY></HTML>people.jsp<%@pagecontentType="text/html;charset=GB2312"%><HTML><BODYBGCOLOR=yellow><FONTSize=3><%!intcount;StringBufferperson;publicvoidjudge(){if(count==0)person=newStringBuffer();//StringBuffer類型對象的特點是可以修改所表達(dá)的字符串內(nèi)容,append(s)可以在其后追加//內(nèi)容s}publicvoidaddPerson(Stringp){if(count==0){person.append(p);}else{person.append(","+p);}count++;}%><%Stringname=request.getParameter("name");bytebb[]=name.getBytes("iso-8859-1");name=newString(bb);if(name.length()==0||name.length()>10){%><jsp:forwardpage="inputName.jsp"/><%}judge();addPerson(name);%><BR>目前共有<%=count%>人瀏覽了該頁面,他們的名字是:<BR><%=person%></FONT></BODY></HTML>one.jsp<%@pagecontentType="text/html;charset=GB2312"%><HEAD><jsp:includepage="head.txt"/></HEAD><HTML><BODYbgcolor=yellow><FORMaction=""method=getname=form>請輸入1至100之間的整數(shù):<INPUTtype="text"name="number"><BR><INPUTTYPE="submit"value="送出"name=submit></FORM><%Stringnum=request.getParameter("number");if(num==null){num="0";}try{intn=Integer.parseInt(num);if(n>=1&&n<=50){%><jsp:forwardpage="two.jsp"><jsp:paramname="number"value="<%=n%>"/></jsp:forward><%}elseif(n>50&&n<=100){%><jsp:forwardpage="three.jsp"><jsp:paramname="number"value="<%=n%>"/></jsp:forward><%}}catch(Exceptione){%><jsp:forwardpage="error.jsp"><jsp:paramname="mess"value="<%=e.toString()%>"/></jsp:forward><%}%></BODY></HTML>two.jsp<%@pagecontentType="text/html;charset=GB2312"%><HEAD><jsp:includepage="head.txt"/></HEAD><HTML><BODYbgcolor=yellow><P><Fontsize=2color=blue>Thisistwo.jsp.</Font><Fontsize=3><%Strings=request.getParameter("number");out.println("<BR>傳遞過來的值是"+s);%><BR><imgsrc="a.jpg"width="<%=s%>"height="<%=s%>"></img></FONT></BODY></HTML>three.jsp<%@pagecontentType="text/html;charset=GB2312"%><HEAD><jsp:includepage="head.txt"/></HEAD><HTML><BODYbgcolor=yellow><P><Fontsize=2color=red>Thisisthree.jsp.</Font><Fontsize=3><%Strings=request.getParameter("number");out.println("<BR>傳遞過來的值是"+s);%><BR><imgsrc="b.jpg"width="<%=s%>"height="<%=s%>"></img></FONT></BODY></HTML>error.jsp<%@pagecontentType="text/html;charset=GB2312"%><HEAD><jsp:includepage="head.txt"/></HEAD><HTML><BODYbgcolor=yellow><P><Fontsize=5color=red>Thisiserror.jsp.</Font><Fontsize=2><%Strings=request.getParameter("mess");out.println("<BR>傳遞過來的錯誤信息"+s);%><BR><imgsrc="c.jpg"width="120"height="120"></img></FONT></BODY></HTML>實驗三內(nèi)置對象3月30實驗?zāi)康恼_理解request、session、application三個對象的作用域能正確處理漢字亂碼問題實驗內(nèi)容編寫一個會員系統(tǒng),首頁home.jsp,必須是會員登陸之后才能瀏覽的3個頁面:tupian.jsp、dianying.jsp、wenxue.jsp請同學(xué)們在首頁中增加訪客計數(shù)器和點擊率實驗步驟

home.jsp<%@pagecontentType="text/html;charset=GB2312"%><ahref="tupian.jsp">會員圖片區(qū)</a><ahref="dianying.jsp">會員電影區(qū)</a><ahref="wenxue.jsp">會員文學(xué)區(qū)</a><formaction=""method=post>用戶名:<inputtype=textname=name/><br>密  碼:<inputtype=passwordname=pass/><br><inputtype="submit"value="提交"/></form><%Stringname=request.getParameter("name");if(name!=null){if(name.length()!=0){session.setAttribute("guest",name);out.println(name+"已經(jīng)登陸");}}%>tupian.jsp<%@pagecontentType="text/html;charset=GB2312"%><%Stringstr=(String)session.getAttribute("guest");if(str==null)response.sendRedirect("home.jsp");elseout.print("<palign=right>"+str+"已登陸</p>");%><p>會員活動圖片區(qū)dianying.jsp<%@pagecontentType="text/html;charset=GB2312"%><%Stringstr=(String)session.getAttribute("guest");if(str==null)response.sendRedirect("home.jsp");elseout.print("<palign=right>"+str+"已登陸</p>");%><p>會員活動電影區(qū)wenxue.jsp<%@pagecontentType="text/html;charset=GB2312"%><%Stringstr=(String)session.getAttribute("guest");if(str==null)response.sendRedirect("home.jsp");elseout.print("<palign=right>"+str+"已登陸</p>");%><p>會員活動文學(xué)區(qū)2.訪客計數(shù)器和點擊率<%!intpersoncount=0;%><%Stringstr=(String)session.getAttribute("count");if(str==null){personcount++;Stringcount=String.valueOf(personcount);session.setAttribute("count",count);}%><P>您是第<%=(String)session.getAttribute("count")%>個訪問本網(wǎng)站的客戶。<%!intpointcount=0;//被所有客戶共享的pointcountsynchronizedvoidsetCount()//synchronized修飾的方法{pointcount++;}%><%setCount();out.println("本頁面被點擊次數(shù)"+pointcount);%>實驗四、jsp與javaBean實驗?zāi)康?、學(xué)會如何正確使用javaBean實驗內(nèi)容1、在一個頁面使用javaBean顯示當(dāng)前月的月歷實驗步驟編寫CalendarBean.java,編譯生成一個class文件

(新建一個文本文件,改擴展名為.bat文件,編輯文件內(nèi)容為javacCalendarBean.java,然后雙擊這個文件即可生成CalendarBean.class文件)將這個class文件拷貝到WEB-INF\classes\tom\jiafei目錄下(沒有相應(yīng)的目錄就建立相應(yīng)的目錄)重新啟動tomcat服務(wù)器運行jsp文件,顯示日歷

CalendarBean.javapackagetom.jiafei;importjava.util.*;publicclassCalendarBean{Stringcalendar=null;intyear=-1,month=-1;publicvoidsetYear(intyear){this.year=year;}publicintgetYear(){returnyear;}publicvoidsetMonth(intmonth){this.month=month;}publicintgetMonth(){returnmonth;}publicStringgetCalendar(){StringBufferbuffer=newStringBuffer();Calendarrili=Calendar.getInstance();rili.set(year,month-1,1);//將日歷翻到y(tǒng)ear年month月1日,注意0表示一月,//依次類推,11表示12月。//獲取1日是星期幾(get方法返回的值是1表示星期日,返回的值是7表示星期六):int星期幾=rili.get(Calendar.DAY_OF_WEEK)-1;intday=0;if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){day=31;}if(month==4||month==6||month==9||month==11){day=30;}if(month==2){if(((year%4==0)&&(year%100!=0))||(year%400==0)){day=29;}else{day=28;}}Stringa[]=newString[42];//存放號碼的一維數(shù)組for(inti=0;i<星期幾;i++){a[i]="**";}for(inti=星期幾,n=1;i<星期幾+day;i++){a[i]=String.valueOf(n);n++;}for(inti=星期幾+day,n=1;i<42;i++){a[i]="**";}//用表格顯示數(shù)組:buffer.append("<tableborder=1>");buffer.append("<tr>");Stringweekday[]={"日","一",二","三","四","五","六"};for(intk=0;k<7;k++){buffer.append("<td>"+weekday[k]+"</td>");}buffer.append("</tr>");for(intk=0;k<42;k=k+7){buffer.append("<tr>");for(intj=k;j<Math.min(7+k,42);j++){buffer.append("<td>"+a[j]+"</td>");}buffer.append("</tr>");}buffer.append("</table");calendar=newString(buffer);if(year!=-1){returncalendar;}else{return"選擇一個年份和月份單擊提交鍵";}}}showcalendar.jsp<%@pagecontentType="text/html;charset=GB2312"%><%@pageimport="tom.jiafei.*"%><%@pageimport="java.util.*"%><jsp:useBeanid="rili"class="tom.jiafei.CalendarBean"scope="session"/><%Datedate=newDate();intyear=date.getYear()+1900;//1900是修正值intmonth=date.getMonth()+1;rili.setYear(year);rili.setMonth(month);%><bodybgcolor=#336699><divstyle=”width=200;align=right”><jsp:getPropertyname="rili"property="calendar"/></div></body>實驗五、jsp文件操作3月30實驗?zāi)康?、掌握一種使用javaBean的文件上傳的方法,在使用過程中逐步完善這個javaBean;實驗內(nèi)容1、上傳一個文件到根目錄中的upload文件夾實驗步驟由于編譯這個javaBean時需要導(dǎo)入servlet-api.jar包,所以要在環(huán)境變量classpath后面指定這個包的位置,生成class文件之后,jsp文件運行不要這個包。編寫jsp文件運行之,觀察上傳文件的結(jié)果思考文件重名、文件路徑、文件大小等情況UpFile.javapackagetom.jiafei;importjava.io.*;importjava.util.*;importjava.util.Date;importjavax.servlet.http.*;publicclassUpFile{HttpServletRequestrequest;HttpSessionsession;StringupFileMessage="";publicvoidsetRequest(HttpServletRequestrequest){this.request=request;}publicvoidsetSession(HttpSessionsession){this.session=session;}publicStringgetUpFileMessage(){StringfileName=null;try{StringtempFileName=(String)session.getId();//客戶的session的idFilef1=newFile("./",tempFileName);FileOutputStreamo=newFileOutputStream(f1);InputStreamin=request.getInputStream();byteb[]=newbyte[10000];intn;while((n=in.read(b))!=-1){o.write(b,0,n);}o.close();in.close();RandomAccessFilerandom=newRandomAccessFile(f1,"r");intsecond=1;//讀出f1的第2行,析取出上傳文件的名字:StringsecondLine=null;while(second<=2){secondLine=random.readLine();second++;}//獲取第2行中目錄符號'\'最后出現(xiàn)的位置intposition=secondLine.indexOf("file");//客戶上傳的文件的名字是:fileName=secondLine.substring(position+10,secondLine.length()-1);//if(fileName.indexOf("\\")!=-1){position=secondLine.lastIndexOf("\\");fileName=secondLine.substring(position+1,secondLine.length()-1);}Calendarcalendar=Calendar.getInstance();calendar.setTime(newDate());longpreName=(calendar.getTimeInMillis());fileName=preName+fileName;bytecc[]=fileName.getBytes("ISO-8859-1");fileName=newString(cc);//System.out.println(fileName);session.setAttribute("Name",fileName);//供show.jsp頁面使用random.seek(0);//再定位到文件f1的開頭。//獲取第4行回車符號的位置:longforthEndPosition=0;intforth=1;while((n=random.readByte())!=-1&&(forth<=4)){if(n=='\n'){forthEndPosition=random.getFilePointer();forth++;}}//根據(jù)客戶上傳文件的名字,將該文件存入磁盤:Filef2=newFile("../webapps/ROOT/upload",fileName);RandomAccessFilerandom2=newRandomAccessFile(f2,"rw");//確定出文件f1中包含客戶上傳的文件的內(nèi)容的最后位置,即倒數(shù)第6行。random.seek(random.length());longendPosition=random.getFilePointer();longmark=endPosition;intj=1;while((mark>=0)&&(j<=6)){mark--;random.seek(mark);n=random.readByte();if(n=='\n'){endPosition=random.getFilePointer();j++;}}//將random流指向文件f1的第4行結(jié)束的位置:random.seek(forthEndPosition);longstartPoint=random.getFilePointer();//從f1讀出客戶上傳的文件存入f2(讀取從第4行結(jié)束位置和倒數(shù)第6行之間的內(nèi)容)while(startPoint<endPosition-1){n=random.readByte();random2.write(n);startPoint=random.getFilePointer();}random2.close();random.close();f1.delete();//刪除臨時文件upFileMessage=fileName;//+"SuccessfullyUpLoad";returnupFileMessage;}catch(Exceptionexp){if(fileName!=null){upFileMessage=fileName+"FailtoUpLoad";returnupFileMessage;}else{upFileMessage="";returnupFileMessage;}}}}upfile.jsp<%@pagecontentType="text/html;charset=GB2312"%><%@pageimport="tom.jiafei.UpFile"%><jsp:useBeanid="upFile"class="tom.jiafei.UpFile"scope="session"/><HTML><BODY><P>選擇要上傳的文件:<BR><FORMaction=""method="post"ENCTYPE="multipart/form-data"><INPUTtype=FILEname="boy"size="45"><BR><INPUTtype="submit"name="g"value="提交"></FORM><%upFile.setRequest(request);upFile.setSession(session);%></BODY></HTML>實驗五、數(shù)據(jù)庫操作4月13一、實驗?zāi)康?、掌握在jsp文件中如何使用數(shù)據(jù)庫實驗內(nèi)容用數(shù)據(jù)源鏈接讀取數(shù)據(jù)庫用純java驅(qū)動讀取數(shù)據(jù)庫對數(shù)據(jù)庫記錄分頁顯示實驗步驟用數(shù)據(jù)源鏈接讀取數(shù)據(jù)庫

1)創(chuàng)建數(shù)據(jù)庫,創(chuàng)建數(shù)據(jù)庫表;

2)控制面板中創(chuàng)建數(shù)據(jù)源

3)jsp文件中鏈接數(shù)據(jù)庫,并顯示表中記錄

用純java驅(qū)動讀取數(shù)據(jù)庫

1)網(wǎng)絡(luò)下載包文件,存放在WEB-INF/lib目錄下,重啟服務(wù)器

2)注意數(shù)據(jù)庫必須更新到SP4

3)jsp文件中鏈接數(shù)據(jù)庫,并顯示表中記錄

對數(shù)據(jù)庫記錄分頁顯示

1)修改或者直接使用課本中的javabean實現(xiàn)分頁顯示

2)注意數(shù)據(jù)庫必須更新到SP4參考代碼news.jsp<%Connectioncon;Statementsql;ResultSetrs;try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}catch(ClassNotFoundExceptione){out.print(e);}//try加載數(shù)據(jù)源驅(qū)動try{con=DriverManager.getConnection("jdbc:odbc:news","sa","sa");sql=con.createStatement();rs=sql.executeQuery("SELECT*FROMnewsorderbytimesdesc");inti=1;while(rs.next()&&i<=10){out.print("<br>");Stringtitle=rs.getString(1);%><ahref="newsview.jsp?title=<%=title%>"><%=title%></a><br><%i++;}con.close();}catch(SQLExceptione1){out.print(e1);}//try鏈接數(shù)據(jù)庫并顯示表中的10條記錄的title字段}純java驅(qū)動鏈接數(shù)據(jù)庫只是加載驅(qū)動的語句有點差別,jsp文件沒多大差別。此種方式下比前者更先進(jìn),但機房sql沒有sp4分頁顯示newslist.jsp<%@pagecontentType="text/html;charset=GB2312"%><%@pageimport="tom.jiafei.ShowRecordByPage"%><HTML><BODY><divid="container"><divid="header"><linkrel="stylesheet"type="text/css"href="css.css"><%@includefile="head.txt"%></div><divid=siderleft></div><divid="siderright"><br><br><br><br><br><jsp:useBeanid="look"class="tom.jiafei.ShowRecordByPage"scope="session"/><jsp:setPropertyname="look"property="databaseName"value="news"/><jsp:setPropertyname="look"property="tableName"value="news"/><jsp:setPropertyname="look"property="user"value="sa"/><jsp:setPropertyname="look"property="password"value="sa"/><jsp:setPropertyname="look"property="pageSize"value="2"/><%inttotal=look.getPageAllCount();intm;Stringshowpage=request.getParameter("showpage");if(showpage==null)m=1;else{try{m=Integer.parseInt(showpage);}catch(NumberFormatExceptione){m=1;}if(m<1)m=1;elseif(m>total)m=total;}%><jsp:setPropertyname="look"property="showPage"value="<%=m%>"/><jsp:getPropertyname="look"property="presentPageResult"/><br><br><br><br><br><br><BR>當(dāng)前顯示第<jsp:getPropertyname="look"property="showPage"/>頁,共有<jsp:getPropertyname="look"property="pageAllCount"/>頁<%intpresent=look.getShowPage();for(inti=1;i<=total;i++){%><ahref="newslist.jsp?showpage=<%=i%>"><%=i%></a><%}%><ahref="newslist.jsp?showpage=<%=m+1%>">下一頁</a><ahref="newslist.jsp?showpage=<%=m-1%>">上一頁</a><FORMaction="">跳轉(zhuǎn)到<Inputtype=textname="showpage"size=2>頁<Inputtype=submitname="g"value="提交"></FORM></FORM></div></center><divid="bottom"><br/></div></BODY></div></HTML>實驗六、serverlet基礎(chǔ)4月27一、實驗?zāi)康?、理解serverlet的工作原理以及生命周期,掌握如何編寫和使用serverlet二、實驗內(nèi)容編寫serverlet源程序,編譯生成相應(yīng)的class文件,拷貝到WEB-INF/star/moon/下2、部署serverlet文件,在根目錄的web.xml中添加serverlet應(yīng)用3、在jsp中使用serverlet三、實驗步驟1.編寫serverlet源程序

Computer.java

packagestar.moon;importjava.io.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassComputerextendsHttpServlet{publicvoidinit(ServletConfigconfig)throwsServletException{super.init(config);}publicvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException{response.setContentType("text/html;charset=GB2312");PrintWriterout=response.getWriter();out.println("<html><body>");Stringnumber=request.getParameter("number");//獲取客戶提交的信息。doublen=0;try{n=Double.parseDouble(number);out.print("<BR>"+number+"的平方根是:");out.print("<BR>"+Math.sqrt(n));}catch(NumberFormatExceptione){out.print("<H1>請輸入數(shù)字字符!</H1>");}out.println("</body></html>");}}2.web.xml中添加應(yīng)用

<servlet> <servlet-name>computer</servlet-name> <servlet-class>star.moon.Computer</servlet-class> </servlet> <servlet-mapping> <servlet-name>computer</servlet-name> <url-pattern>/getResult</url-pattern> </servlet-mapping>3.在jsp中使用serverlet

<%@pagecontentType="text/html;Charset=GB2312"%><HTML><BODY><Fontsize=2><P>輸入一個數(shù),servlet求這個數(shù)的平方根:<FORMaction="getResult"method=post><InputType=textname=number><InputType=submitvalue="提交"></FORM></BODY></HTML>

4.運行結(jié)果

實驗七、基于serverlet的MVC5月11日一、實驗?zāi)康?、理解MVC的核心思想2、掌握如何在serverlet中創(chuàng)建javabean實驗內(nèi)容根據(jù)jsp頁面輸入輸出需求構(gòu)建javabean2、創(chuàng)建部署serverlet文件,并創(chuàng)建javabean對象3、在jsp中給出輸入,發(fā)送請求給serverlet,然后jsp中顯示javabean屬性值實驗步驟1.視圖1,jsp的輸入,給出三角形的三個邊長,計算能否夠建三角形,能在計算面積

inputNumber.jsp<%@pagecontentType="text/html;charset=GB2312"%><%@pageimport="flower.grass.ComputerBean"%><HTML><BODY><Fontsize=2><FORMaction="helpComputer"method=postname=form><table><tr><td>輸入兩個數(shù):</td><td><Inputtype=textname="numberOne"value=0size=6></td><td><Inputtype=textname="numberTwo"value=0size=6></td></tr><tr><td>選擇運算符號:</td><td><Selectname="operator"><Optionvalue="+">+(加)<Optionvalue="-">-(減)<Optionvalue="*">*(乘)<Optionvalue="/">/(除)</Select></td><td><INPUTTYPE="submit"value="提交選擇"name="submit"></td></tr></table></FORM></BODY></HTML>2.構(gòu)建模型javabean,此類應(yīng)該有三個邊長,一個面積,一個布爾變量判斷能否構(gòu)建三角形

ComputerBean.javapackageflower.grass;publicclassComputerBean{doublenumberOne,numberTwo,result;Stringoperator="+";publicvoidsetNumberOne(doublen){numberOne=n;}publicdoublegetNumberOne(){returnnumberOne;}publicvoidsetNumberTwo(doublen){numberTwo=n;}publicdoublegetNumberTwo(){returnnumberTwo;}publicvoidsetOperator(Strings){operator=s.trim();;}publicStringgetOperator(){returnoperator;}publicvoidsetResult(doubler){result=r;}publicdoublegetResult(){returnresult;}}3.在控制器serverlet中創(chuàng)建javabean對象并計算

HandleComputer.javapackagerain.snow;importflower.grass.*;importjava.io.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassHandleComputerextendsHttpServlet{publicvoidinit(ServletConfigconfig)throwsServletException{super.init(config);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ComputerBeandataBean=null;HttpSessionsession=request.getSession(true);try{dataBean=(ComputerBean)session.getAttribute("ok");if(dataBean==null){dataBean=newComputerBean();//創(chuàng)建Javabean對象session.setAttribute("ok",dataBean);//將dataBean存儲到session對象中}}catch(Exceptionexp){dataBean=newComputerBean();//創(chuàng)建Javabean對象session.setAttribute("ok",dataBean);//將dataBean存儲到session對象中}doublenumberOne=Double.parseDouble(request.getParameter("numberOne"));doublenumberTwo=Double.parseDouble(request.getParameter("numberTwo"));Stringoperator=request.getParameter("operator");doubleresult=0;if(operator.equals("+")){result=numberOne+numberTwo;}elseif(operator.equals("-")){result=numberOne-numberTwo;}elseif(operator.equals("*")){result=numberOne*numberTwo;}elseif(operator.equals("/")){result=numberOne/numberTwo;}dataBean.setNumberOne(numberOne);//將數(shù)據(jù)存儲在dataBean中dataBean.setNumberTwo(numberTwo);//將數(shù)據(jù)存儲在dataBean中dataBean.setOperator(operator);//將數(shù)據(jù)存儲在dataBean中dataBean.setResult(result);//將數(shù)據(jù)存儲在dataBean中RequestDispatcherdispatcher=request.getRequestDispatcher("/showResult.jsp");dispatcher.forward(request,response);//請求showResult.jsp顯示dataBean中的數(shù)據(jù)}publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}}4.在jsp頁面中調(diào)用顯示javabean屬性(結(jié)果)

showResult.jsp<%@pagecontentType="text/html;charset=GB2312"%><%@pageimport="flower.grass.ComputerBean"%><HTML><BODY><Fontsize=3><jsp:useBeanid="ok"type="flower.grass.ComputerBean"scope="session"/>運算結(jié)果:<jsp:getPropertyname="ok"property="numberOne"/><jsp:getPropertyname="ok"property="operator"/><jsp:getPropertyname="ok"property="numberTwo"/>=<jsp:getPropertyname="ok"property="result"/><FORMaction="helpComputer"method=postname=form><table><tr><td>輸入兩個數(shù):</td><td><Inputtype=textname="numberOne"value="<jsp:getPropertyname="ok"property="result"/>"size=10></td><td><Inputtype=textname="numberTwo"value=0size=10></td></tr><tr><td>選擇運算符號:</td><td><Selectname="operator"><Optionvalue="+">+(加)<Optionvalue="-">-(減)<Optionvalue="*">*(乘)<Optionvalue="/">/(除)</Select></td><td><INPUTTYPE="submit"value="提交選擇"name="submit"></td></tr></table></BODY></HTML>實驗八、一個用戶登錄系統(tǒng)實例5月25實驗?zāi)康?、掌握一個完整的用戶系統(tǒng)的實現(xiàn)實驗內(nèi)容1、創(chuàng)建用戶登錄、注冊頁面2、創(chuàng)建用戶登錄驗證頁面、用戶注冊驗證頁面3、創(chuàng)建用戶登錄成功和用戶退出頁面實驗步驟1.用戶登錄

<palign=center><formaction="denglu.jsp">用戶名:<inputtype=textname=usernameclass="input1"/><br>密  碼:<inputtype=passwordname=passwordclass="input1"/><br><br><inputtype=submitvalue=登陸>  <ahref=login.jsp>新用戶注冊</a></form></p>2.用戶注冊

<palign=center><formaction="loginconfirm.jsp">  用戶名:<inputtype=textname=usernameclass="input1"/><br>  密  碼:<inputtype=passwordname=passwordclass="input1"/><br>電子郵件:<inputtype=textname=emailclass="input1"/><br><br><inputtype=resetvalue=重置><inputtype=submitvalue=注冊></form></p>3.用戶注冊驗證

<palign=center><%Stringusername=request.getParameter("username");Stringpassword=request.getParameter(

溫馨提示

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

最新文檔

評論

0/150

提交評論