Java Web 編程技術(shù)程序代碼1_第1頁
Java Web 編程技術(shù)程序代碼1_第2頁
Java Web 編程技術(shù)程序代碼1_第3頁
Java Web 編程技術(shù)程序代碼1_第4頁
Java Web 編程技術(shù)程序代碼1_第5頁
已閱讀5頁,還剩166頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

/JavaWeb編程技術(shù)沈澤剛秦玉平主編清華大學(xué)出版社20103作者E-mail地址:電話:?jiǎn)挝唬翰澈4髮W(xué)信息科學(xué)與工程學(xué)院感謝您使用本書,下面是書中全部程序代碼。如有任何問題,希望聯(lián)系作者。程序1.1index.html<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Strict//EN"""><html><head><metahttp-equiv="content-type"content="text/html;charset=gb2312"/><metahttp-equiv="content-language"content="zh-cn"/><title>NewCenturyWebBookstore</title></head><bodybgcolor="#ffffff"><tablealign="center"border="0"cellpadding="0"cellspacing="0"width="540"><tr><tdalign="center"width="20%"> <imgsrc="images/duke.gif"align="right"alt="Hereisalogo."></td><tdwidth="80%"><palign="center"><fontsize="+2"color="red">新世紀(jì)</font><fontsize="+2"color="purple">網(wǎng)上書店</font></p></td></tr><tr><tdwidth="20%"><br><palign="center"><b>圖書分類</b></p><ul><li><ahref="book.do?catalog=computer">計(jì)算機(jī)</a></li><li><ahref="book.do?catalog=industry">工業(yè)技術(shù)</a></li><li><ahref="book.do?catalog=science">自然科學(xué)</a></li><li><ahref="book.do?catalog=architect">建筑水利</a></li></ul></td><tdwidth="80%"><palign="center"><b>世界讀書日,讀一本好書,受益一生!</b></p><pstyle="text-indent:2em">通過開展為期一個(gè)月(4.23-5.23)讀書節(jié)活動(dòng),切實(shí)引導(dǎo)大家多讀書、樂讀書、讀好書,拓寬視野,引導(dǎo)大家從中認(rèn)識(shí)“真、善、美”,樹立正確的人生觀、價(jià)值觀,提高全民人文素養(yǎng)。</p></td></tr><tr><tdwidth="20%"> </td><tdwidth="80%"> <hrwidth="50%"align="right"size="1"color="blue"/><palign="right"><fontcolor="blue">Copyright©2009NewCenturyWebBookstore,Inc.</font></p></td></tr></table></body></html>_____________________________________________________________________________▃程序1.2register.html<html><head><metahttp-equiv="content-type"content="text/html;charset=gb2312"/><metahttp-equiv="content-language"content="zh-cn"/><title>RegisterPage</title><styletype="text/css">p{font-size:11</style></head><body><formaction=""method="post"><p>姓名:<inputtype="text"name="name"size="15">年齡:<inputtype="text"name="age"size="5"><br>性別:<inputtype="radio"name="sex"value="male">男<inputtype="radio"name="sex"value="female">女<br>興趣:<inputtype="checkbox"name="hobby"value="read">文學(xué)<inputtype="checkbox"name="hobby"value="sport">體育<inputtype="checkbox"name="hobby"value="computer">電腦<br>最高學(xué)歷:<selectname="study"width="15"><optionvalue="bachelor">學(xué)士</option><optionvalue="master">碩士</option><optionvalue="doctor">博士</option></select>郵件地址:<inputtype="text"name="email"size="20"><br><inputtype="submit"name="submit"value="提交"><inputtype="reset"name="reset"value="重添"></p></form></body></html>_____________________________________________________________________________▃程序1.3inputCheck.html<html><head><scriptlanguage="JavaScript"type="text/javascript">functioncustCheck(){varcustName=document.getElementById("custName");varemail=document.getElementById("email");varphone=document.getElementById("phone");if(custName.value==""){alert("客戶名不能為空!");returnfalse;}elseif(email.value.indexOf("@")==-1){alert("電子郵件中應(yīng)包含@字符!");returnfalse;}elseif(phone.value.length!=8){alert("電話號(hào)碼應(yīng)是8位數(shù)字!");returnfalse;}}</script><styletype="text/css">*,input{font-size:11</style></head><body><formaction="/helloapp/inputCustomer"method="post"onSubmit="returncustCheck()">請(qǐng)輸入客戶信息:<table><tr><td>客戶名:</td><td><inputtype="text"name="custName"id="custName"></td></tr><tr><td>Email地址:</td><td><inputtype="text"name="email"id="email"></td></tr><tr><td>電話:</td><td><inputtype="text"name="phone"id="phone"></td></tr></table><inputtype="submit"value="確定"><inputtype="reset"value="重置"></form></body></html>_____________________________________________________________________________▃程序1.4HelloServlet.javapackagecom.demo;importjava.io.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassHelloServletextendsHttpServlet{publicvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{PrintWriterout=response.getWriter();out.println("<html>");out.println("<body>");out.println("<fontcolor='#0000ff'>");out.println("<h3>Hello,World!</h3>");out.println("Thetimenowis:"+newjava.util.Date());out.println("</body>");out.println("</html>");}}_____________________________________________________________________________▃程序1.5web.xml<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns=""xmlns:xsi=""xsi:schemaLocation=""version="2.5"><servlet><servlet-name>helloServlet</servlet-name><servlet-class>com.demo.HelloServlet</servlet-class></servlet><servlet-mapping><servlet-name>helloServlet</servlet-name><url-pattern>/hello</url-pattern></servlet-mapping></web-app>_____________________________________________________________________________▃程序1.6hello.jsp<html><head><title>ASimpleJSPPage</title></head><body><fontcolor="#0000ff"><h3>Hello,World!</h3>Thetimenowis:<%=newjava.util.Date()%></font></body></html>_____________________________________________________________________________▃程序1.7square.jsp<html><body><tableborder="1"cellspacing="0"cellpadding="5"><tr><th>number</th><th>squared</th></tr><%for(inti=0;i<10;i++){%><tr><td><%=i%></td><td><%=(i*i)%></td></tr><%}%></table></body></html>_____________________________________________________________________________▃程序2.1login.html<html><head><title>LoginPage</title></head><body><formaction="login"method="post"><tablewidth="298"border="0"align="center"cellpading="2"cellspacing="1"> <tr><tdalign="right">用戶名:</td><tdalign="left"><inputtype="text"name="username"size="15"></td></tr><tr> <tdalign="right">密  碼:</td><td><inputtype="password"name="password"size="15"></td></tr><tr><tdcolspan="2"align="center"><inputtype="submit"value="登錄"> <inputtype="reset"value="取消"></td></tr></form></body></html>_____________________________________________________________________________▃程序2.2LoginServlet.javapackagecom.demo;importjava.io.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassLoginServletextendsHttpServlet{publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{Stringusername=request.getParameter("username");Stringpassword=request.getParameter("password");response.setContentType("text/html;charset=gb2312");PrintWriterout=response.getWriter();out.println("<html><body>");if(username.equals("admin")&&password.equals("admin")){out.println("登錄成功!歡迎您,"+username);}else{out.println("對(duì)不起!您的用戶名或密碼不正確.");}out.println("</body></html>");}publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}}_____________________________________________________________________________▃程序2.3exam.html<html><head><title>ASimpleExam</title><styletype="text/css">h3,p{text-indent:2em;font-size:85%}</style></head><body><h3>請(qǐng)回答下面的問題:</h3><formaction="exam.do"method="post"><p>1.在北京召開的奧運(yùn)會(huì)是第29屆奧運(yùn)會(huì)。<inputtype="radio"name="quest1"value="1">正確<inputtype="radio"name="quest1"value="2">錯(cuò)誤</p><p>2.Windows操作系統(tǒng)是哪個(gè)公司的產(chǎn)品?<inputtype="radio"name="quest2"value="1">Sun公司<inputtype="radio"name="quest2"value="2">IBM公司<inputtype="radio"name="quest2"value="3">Microsoft公司</p><p>3.下面的程序設(shè)計(jì)語言,哪些是面向?qū)ο蟮模?lt;inputtype="checkbox"name="quest3"value="1">Java語言<inputtype="checkbox"name="quest3"value="2">C語言<inputtype="checkbox"name="quest3"value="3">C++語言</p><p>4.編寫Servlet程序應(yīng)繼承什么類?<inputtype="text"name="quest4"size="30"></p><p><b>交卷請(qǐng)點(diǎn)擊:</b><inputtype="submit"value="交卷"><b>重答請(qǐng)點(diǎn)擊:</b><inputtype="reset"value="重答"></p></form></body></html>_____________________________________________________________________________▃程序2.4ExamServlet.javapackagecom.exam;importjavax.servlet.*;importjavax.servlet.http.*;importjava.io.*;publicclassExamServletextendsHttpServlet{ publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException,ServletException{ response.setContentType("text/html;charset=GB2312");PrintWriterout=response.getWriter();Stringquest1=request.getParameter("quest1");String[]quest2=request.getParameterValues("quest2");String[]quest3=request.getParameterValues("quest3");Stringquest4=request.getParameter("quest4").trim();intscore=0;if(quest1!=null&&quest1.equals("1")){score=score+25;}if(quest2!=null&&quest2[0].equals("3")){score=score+25; }if(quest3!=null&&quest3.length==2&&quest3[0].equals("1")&&quest3[1].equals("3")){score=score+25; }if(quest4!=null&&(quest4.equals("HttpServlet")||quest4.equals("javax.servlet.http.HttpServlet"))){score=score+25; }out.println("<html><head>");out.println("<title>考試結(jié)果</title>");out.println("</head><body>");out.println("你的成績是:"+score+"分");out.println("</body></html>");}}_____________________________________________________________________________▃程序2.5ClientInfoServlet.javapackagecom.demo;importjava.io.*;importjava.util.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassClientInfoServletextendsHttpServlet{ publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ response.setContentType("text/html;charset=GB2312"); PrintWriterout=response.getWriter(); out.println("<html><head>"); out.println("<title>獲取客戶端信息</title>"); out.println("</head>"); out.println("<body>"); out.println("<h4>客戶端信息:</h4>"); out.println("<tablewidth=\"350\"border=\"1\">"); out.println("<tr><tdwidth=\"175\">客戶主機(jī)名</td>"); out.println("<tdwidth=\"175\">"+request.getRemoteHost()+"</td>"); out.println("</tr>"); out.println("<tr><td>客戶IP地址</td>"); out.println("<td>"+request.getRemoteAddr()+"</td>"); out.println("</tr>"); out.println("<tr><td>端口</td>"); out.println("<td>"+request.getRemotePort()+"</td>"); out.println("</tr>"); out.println("<tr><td>請(qǐng)求方法</td>"); out.println("<td>"+request.getMethod()+"</td>"); out.println("</tr>"); out.println("<tr><td>請(qǐng)求協(xié)議</td>"); out.println("<td>"+request.getProtocol()+"</td>"); out.println("</tr>"); out.println("<tr><td>編碼方式</td>"); out.println("<td>"+request.getCharacterEncoding()+"</td>"); out.println("</tr>");out.println("<tr><td>內(nèi)容類型</td>"); out.println("<td>"+request.getContentType()+"</td>"); out.println("</tr>"); out.println("<tr><td>請(qǐng)求URI</td>"); out.println("<td>"+request.getRequestURI()+"</td>"); out.println("</tr>"); out.println("</table>"); out.println("</body></html>"); } publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ doGet(request,response); } }_____________________________________________________________________________▃程序2.6ShowHeadersServlet.javapackagecom.demo;importjava.io.*;importjavax.servlet.*;importjavax.servlet.http.*;importjava.util.Enumeration;publicclassShowHeadersServletextendsHttpServlet{publicvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setContentType("text/html;charset=gb2312");PrintWriterout=response.getWriter();out.println("<html><body>");out.println("下面是服務(wù)器收到的請(qǐng)求頭信息<p>");out.println(request.getMethod()+"" +request.getRequestURI()+"" +request.getQueryString()+"" +request.getProtocol()+"<br>");Enumerationheaders=request.getHeaderNames();while(headers.hasMoreElements()){Stringheader=(String)headers.nextElement();Stringvalue=request.getHeader(header);out.println(header+"="+value+"<br>");}out.println("</body></html>");}}_____________________________________________________________________________▃程序2.7LoginServlet.javapackagecom.demo;importjava.io.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassLoginServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{Stringusername=request.getParameter("username");Stringpassword=request.getParameter("password");Stringlocation=""; if(username.equals("admin")&&password.equals("admin")){request.setAttribute("username",username);location="/hello.jsp";}else{location="/login.html";}RequestDispatcherdispatcher=request.getRequestDispatcher(location);dispatcher.forward(request,response);}}_____________________________________________________________________________▃程序2.8hello.jsp<html><body><h4>Yourarewelcome!</h4><%=request.getAttribute("username")%></body></html>_____________________________________________________________________________▃程序2.9ExcelServlet.javapackagecom.demo;importjava.io.*;importjava.util.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassExcelServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//指定頁面在傳輸過程中使用的編碼方式response.setHeader("Content-Encoding","gb2312");response.setContentType("application/vnd.ms-excel;charset=gb2312");PrintWriterout=response.getWriter();out.println("學(xué)號(hào)\t姓名\t性別\t年齡\t所在系");out.println("95001\t李勇\t男\(zhòng)t20\t信息");out.println("95002\t劉晨\t女\t19\t數(shù)學(xué)");}}_____________________________________________________________________________▃程序2.10packagecom.demo;importjava.io.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setContentType("application/jar");//設(shè)置響應(yīng)頭,將響應(yīng)內(nèi)容以指定的文件名存儲(chǔ)到客戶機(jī)上response.setHeader("Content-Disposition","attachment;");Filef=newFile("C:\\servlet-api.jar");is=new(f);ServletOutputStreamos=response.getOutputStream();byte[]bytearray=newbyte[1024];intbytesread=0;while((bytesread=is.read(bytearray))!=-1){os.write(bytearray,0,bytesread);}os.flush();}}_____________________________________________________________________________▃程序2.11ShowTimeServlet.javapackagecom.demo;importjava.io.*;importjava.util.*;importjava.text.SimpleDateFormat;importjavax.servlet.*;importjavax.servlet.http.*;publicclassShowTimeServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setContentType("text/html;charset=gb2312");response.setHeader("Refresh","5");PrintWriterout=response.getWriter();Datedate=newDate();SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-ddaHH:mm:ss");Strings=sdf.format(date);//對(duì)日期對(duì)象格式化out.println("<html>");out.println("<body>");out.println("<h3align=\"center\">每5秒鐘刷新一次頁面</h3><p>");out.println("<h3align=\"center\">現(xiàn)在的時(shí)間是:"+s+"</h3><p>");out.println("</body>");out.println("</html>");}}_____________________________________________________________________________▃程序2.12RedirectServlet.javapackagecom.demo;importjava.io.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassRedirectServletextendsHttpServlet{ publicvoiddoGet(HttpServletRequestrequest, HttpServletResponseresponse) throwsIOException,ServletException{ StringuserAgent=request.getHeader("User-Agent"); if((userAgent!=null)&&(userAgent.indexOf("MSIE")!=-1)){ response.sendRedirect("");return; }else{ response.sendRedirect("");return; } }}_____________________________________________________________________________▃程序3.1web.xml文件。<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns=""xmlns:xsi=""xsi:schemaLocation=""version="2.5"><display-name>WebappTest</display-name><context-param><param-name>email</param-name><param-value></param-value></context-param><servlet><servlet-name>loginServlet</servlet-name><servlet-class>com.demo.LoginServlet</servlet-class><init-param><param-name>greeting</param-name><param-value>GoodMorning</param-value></init-param><load-on-startup>2</load-on-startup></servlet><servlet-mapping><servlet-name>loginServlet</servlet-name><url-pattern>/login.do</url-pattern></servlet-mapping><welcome-><welcome-</welcome-file><welcome-</welcome-file></welcome-><session-config><session-timeout>20</session-timeout></session-config></web-app>_____________________________________________________________________________▃程序3.2web.xml<?xmlversion="1.0"encoding="ISO-8859-1"?><web-app…><servlet><servlet-name>configDemoServlet</servlet-name><servlet-class>com.demo.ConfigDemoServlet</servlet-class><init-param><param-name>email</param-name><param-value></param-value></init-param><init-param><param-name>telephone</param-name><param-value></param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>configDemoServlet</servlet-name><url-pattern>/configDemo.do</url-pattern></servlet-mapping></web-app>_____________________________________________________________________________▃程序3.3ConfigDemoServlet.javapackagecom.demo;importjava.io.*;importjava.sql.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassConfigDemoServletextendsHttpServlet{StringservletName=null;ServletConfigconfig=null;Stringemail=null;Stringtelephone=null;publicvoidinit(){config=getServletConfig();servletName=config.getServletName();email=config.getInitParameter("email");telephone=config.getInitParameter("telephone");}publicvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setContentType("text/html;charset=gb2312");PrintWriterout=response.getWriter();out.println("<html><body>");out.println("Servlet名稱:"+servletName+"<br>");out.println("Email地址:"+email+"<br>"); out.println("電話:"+telephone);out.println("</body></html>");}}_____________________________________________________________________________▃程序3.4packagecom.demo;importjava.io.*;import.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassextendsHttpServlet{publicvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setContentType("application/jar");response.setHeader("Content-Disposition","attachment;");OutputStreamos=response.getOutputStream();ServletContextcontext=getServletContext();URLurl=context.getResource("/");InputStreamis=url.openStream();//上面兩行代碼可以用下面一行實(shí)現(xiàn)//InputStreamis=context.getResourceAsStream("/");byte[]bytearray=newbyte[1024];intbytesread=0;while((bytesread=is.read(bytearray))!=-1){os.write(bytearray,0,bytesread);}os.flush();is.close();}}_____________________________________________________________________________▃程序3.5CalculatorServlet.javapackagecom.demo;importjavax.servlet.*;importjavax.servlet.http.*;importjava.io.*;publicclassCalculatorServletextendsHttpServlet{privateintresult;privateintsleepTime;publicvoidinit(){ Stringsleep_time=getInitParameter("sleep.time"); sleepTime=getNumber(sleep_time);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException,ServletException{request.setCharacterEncoding("gb2312");Stringvalue1=request.getParameter("value1");intv1=getNumber(value1);Stringvalue2=request.getParameter("value2");intv2=getNumber(value2);Stringop=request.getParameter("submit");if(op.equals("相加")){ result=v1+v2;}else{ result=v1-v2;}try{ Thread.sleep(sleepTime);}catch(InterruptedExceptione){ log("Exceptionduringsleeping.");}try{ response.setContentType("text/html;charset=gb2312"); PrintWriterout=response.getWriter(); out.println("<html><body>"); out.println(v1+"與"+v2+op+"結(jié)果是"+result); out.println("</body></html>");}catch(Exceptione){ log("Errorwritingoutput.");}}privateintgetNumber(Strings){ intresult=0;try{ result=Integer.parseInt(s); }catch(NumberFormatExceptione){ log("ErrorParseing"+s); } returnresult;}}_____________________________________________________________________________▃程序3.6calculator.html<html><head><title>Calcularor</title><metahttp-equiv="content-type"content="text/html;charset=gb2312"</head><body>請(qǐng)輸入兩個(gè)整數(shù):<formaction="calculator"method="post"> <p>操作數(shù)1:<inputtype="text"name="value1"size="10"> 操作數(shù)2:<inputtype="text"name="value2"size="10"></p> <p><inputtype="submit"name="submit"value="相加"> <inputtype="submit"name="submit"value="相減"></p></form></body></html>_____________________________________________________________________________▃程序3.7login.html<html><head><title>ABankExample</title></head><body><center>請(qǐng)輸入用戶名和口令查看賬戶信息<br></center><formaction="login.do"method="get"><tablewidth="298"border="0"align="center"cellpading="2"cellspacing="1"> <tr><tdalign="right">用戶名:</td><tdalign="left"><inputtype="text"name="username"size="15"></td></tr><tr> <tdalign="right">密  碼:</td><td><inputtype="password"name="password"size="15"></td></tr><tr><tdcolspan="2"align="center"><inputtype="submit"value="登錄"> <inputtype="reset"value="取消"></td></tr></form></body></html>_____________________________________________________________________________▃程序3.8LoginServlet.javapackagecom.demo;importjava.io.*;importjavax.servlet.*;importjavax.servlet.http.*;importjava.util.HashMap;publicclassLoginServletextendsHttpServlet{ServletContextcontext;HashMap<String,String>users=newHashMap<String,String>();publicvoidinit(){context=getServletContext();users.put("ann","aaa");users.put("john","jjj");users.put("mark","mmm");}publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{Stringusername=request.getParameter("username");Stringpassword=request.getParameter("password");if(username!=null&&password!=null&&password.equals(users.get(username))){request.setAttribute("username",username);RequestDispatcherview= context.getRequestDispatcher("/account.do");view.forward(request,response);return;}else{RequestDispatcherview=context.getRequestDispatcher("/error.html");view.forward(request,response);return; }}}_____________________________________________________________________________▃程序3.9AccountServlet.javapackagecom.demo;importjava.io.*;importjavax.servlet.*;importjavax.servlet.http.*;importjava.util.HashMap;publicclassAccountServletextendsHttpServlet{ServletContextcontext;HashMap<String,String[]>data=newHashMap<String,String[]>();publicvoidinit(){context=getServletContext();data.put("ann",newString[]{"01/01/2009"01/02/2009:1300.00","01/03/2009data.put("john",newString[]{"01/01/2009"01/02/2009:2100.00","01/03/2009data.put("mark",newString[]{"01/01/2009"01/02/2009:5200.00","01/03/2009}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{Stringusername=(String)request.getAttribute("username");if(username!=null){String[]records=(String[])data.get(username);response.setContentType("text/html;charset=gb2312");PrintWriterout=response.getWriter();out.println("<html>");out.println("<body>");out.println("您好!"+username+",您的賬戶信息如下:<p>");for(inti=0;i<records.length;i++){out.println(records[i]+"<br>");}out.println("</body>");out.println("</html>");}else{RequestDispatcherview=context.getRequestDispatcher("/login.html");view.forward(request,response);}}publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}}_____________________________________________________________________________▃程序4.1ShowSessionServlet.javapackagecom.demo;importjava.io.*;importjavax.servlet.*;importjavax.servlet.http.*;importjava.util.Date;publicclassShowSessionServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setContentType("text/html;charset=gb2312");HttpSessionsession=request.getSession(true);Stringheading=null;IntegeraccessCount=(Integer)session.getAttribute("accessCount");if(accessCount==null){accessCount=newInteger(1); heading="歡迎您,首次登錄該頁面!";}else{ heading="歡迎您,再次訪問該頁面!"; accessCount=accessCount+1;}session.setAttribute("accessCount",accessCount);PrintWriterout=response.getWriter();out.println("<html><head>");out.println("<title>SessionTrackingExample</title></head>");out.println("<body><center>");out.println("<h4>"+heading+"<ahref='showsession.do'>再次訪問</a>"+"</h4>");out.println("<tableborder='0'>");out.println("<trbgcolor=\"ffad00\"><td>信息<td>值\n");Stringstate=session.isNew()?"新會(huì)話":"舊會(huì)話";out.println("<tr><td>會(huì)話狀態(tài):<td>"+state+"\n");out.println("<tr><td>會(huì)話ID:<td>"+session.getId()+"\n");out.println("<tr><td>創(chuàng)建時(shí)間:<td>");out.println(""+newDate(session.getCreationTime())+"\n");out.println("<tr><td>最近訪問時(shí)間:<td>");out.println(""+newDate(session.getLastAccessedTime())+"\n");out.println("<tr><td>最大不活動(dòng)時(shí)間:<td>"+session.getMaxInactiveInterval()+"\n");o

溫馨提示

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

評(píng)論

0/150

提交評(píng)論