Java Web 编程技术程序代码.docx

上传人:夺命阿水 文档编号:480164 上传时间:2023-07-19 格式:DOCX 页数:161 大小:269.44KB
返回 下载 相关 举报
Java Web 编程技术程序代码.docx_第1页
第1页 / 共161页
Java Web 编程技术程序代码.docx_第2页
第2页 / 共161页
Java Web 编程技术程序代码.docx_第3页
第3页 / 共161页
Java Web 编程技术程序代码.docx_第4页
第4页 / 共161页
Java Web 编程技术程序代码.docx_第5页
第5页 / 共161页
点击查看更多>>
资源描述

《Java Web 编程技术程序代码.docx》由会员分享,可在线阅读,更多相关《Java Web 编程技术程序代码.docx(161页珍藏版)》请在课桌文档上搜索。

1、JavaWeb编程技术沈泽刚秦玉平主编清华高校出版社20233作者E-mail地址:电话:单位:渤海高校信息科学与工程学院感谢您运用本书,下面是书中全部程序代码。如有任何问题,希望联系作者。程序1.1index.html!DOCTYPEhtmlPUBLIC,W3CDTDXHTML1.0Strict/ENNewCenturyWebBookstore新世纪网上书店vb图书分类vbvpahref=book.do?CataIog=COmPUteI计算机vH自然科学vlivb世界读书日,读一本好书,受益一生!通过开展为期一个月(4.23-5.23)读书节活动,切实引导大家多读书、乐读书、读好书,拓宽视野

2、,引导大家从中相识“真、善、美”,树立正确的人生观、价值观,提高全民人文素养。 Copyright©2023NewCenturyWebBookstore,Inc.程序1.2register.htmlRegisterPagepfont-size:1Iptjcolor:black姓名:VinPUttype=texf,name=,name,size=,15,年龄:J性别:VinPUttype=,radioname=,sexuVaIUe=male,男女VbI爱好:文学inputtype=checkboxname=,hobbyValUe=sport,体育电脑Vbl*最高学历:VSeI

3、eCtname=,studywidth=15/option邮件地址:程序1.3inputCheck.htmlfunctioncustCheck()varCustNamedocument.getElementById(custName,);varemail=document.getElementById(,emai,);varphone=document.getElementById(phone);if(custName.value=,)alert(客户名不能为空!”);returnfalse;elseif(email.value.indexf(,)=-l)alert(电子邮件中应包含字符!”)

4、;returnfalse;)elseif(phone.value.length!=8)alert(电话号码应是8位数字!”);returnfalse;)*,inputfont-size:1lpt;color:black请输入客户信息:vtr客户名:vtdEmail地址:vtd电话:vtd程序1.4HelloServletJavapackagecom.demo;importjava.io.*;importjavax.servlet.*;importjavax.servlet.*;publicclassHelloServletextendsServletpublicvoidservice(Serv

5、letRequestrequest,ServletResponseresponse)throwsServletException,IOExceptionPrintWriterout=response.getWriter();out.println(,);out.println();out.println(,);OUt.println(vh3Hello,World!vh3);out.println(,Thetimenowis:,newjava.util.Date();out.println(nM);out.println();程序1.5web.xmlhelloServletcom.demo.He

6、lloServlethelloServlethello程序1.6hello.jspASimpleJSPPageHello,World!Thetimenowis:程序1.7square.jspnumbersquared%for(inti=0;i程序2.1login.htmlLoginPageVtdaHgn=right”用户名:vtd密  码: vtd程序2.2LoginServletJavapackagecom.demo;importjava.io.*;importjavax.servlet.*;importjavax.servlet.JpublicclassLog

7、inServletextendsServletpublicvoiddoPost(ServletRequestrequest,ServletResponseresponse)throwsServletException,IOExceptionStringusername=request.getParameter(,usemameM);Stringpassword=request.getParameter(password);response.setContentType(utext/html;charset=gb2312);PrintWriterout=response.getWriter();

8、out.println(,);if(username.equals(uadmin,)65password.equals(,admin)OUt.println(登录胜利!欢迎您,,usemame);elseOUt.println(对不起!您的用户名或密码不正确.”);out.println(,);ServletRequest request,ServletException,publicvoiddoGet(ServletResponseresponse)throwsIOExceptiondoPost(request,response);程序2.3exam.htmlASimpleExamh3,pt

9、ext-indent:2em;font-size:85%请回答下面的问题:vh31.在北京召开的奥运会是第29届奥运会。正确错误p2.Windows操作系统是哪个公司的产品?Sun公司IBM公司Microsoft公司vp3.下面的程序设计语言,哪些是面对对象的?Java语言C语己C+而口4.编写SerVIet程序应继承什么类?vp交卷请点击:vbinputtype=submitVahle=交卷vb重答请点击:程序2.4ExamServletJavapackagecom.exam;importjavax.servlet.*;importjavax.servlet.*;importjava.io.

10、*;publicclassExamServletextendsServletpublicvoiddoPost(ServletRequestrequest,ServletResponseresponse)throwsIoExceptionjServletExceptionresponse.setContentType(utexthtmljcharset=GB2312u);PrintWriterout=response.getWriter();Stringquest1=request.getParameter(quest1);Stringquest2=request.getParameterVal

11、ues(,quest2);Stringquest3=request.getParameterValues(,quest3);Stringquest4=request.getParameter(,quest4,).trim();intscore=O;if(questl!=null&quest1.equals(,)score=score+25;)if(quest2!=null8&quest20.equals(3)score=score+25;)if(quest3!=null&quest3.length=2quest30.equals(“)&quest3l.equals(n3)score=score

12、+25;if(quest4!=null&(quest4.equals(,Servletu)quest4.equals(,javax.servlet.Servletu)score=score+25;out.println();OUt.printingtitle考试结果/title”);out.println(,);OUt.printing你的成果是:“+score+分”);out.println(,);程序2.5ClientlnfoServletJavapackagecom.demo;importjava.io.*;importjava.util.*;importjavax.servlet.*;

13、importjavax.servlet.publicclassClientInfoServletextendsServletpublic void doGet(ServletRequest request,ServletResponseresponse)throwsServletException,IOExceptionresponse.SetContentType(texthtml;CharSet=GB2312);PrintWriterout=response.getWriter();out.println();OUt.printl11tvtitle获得客户端信息/title);out.pr

14、intln();out.println();OUt.printInrVh4客户端信息:);out.println(n);out.println(客户主机名vtd);out.println(+request.getRemoteHost()+,);out.println(,);out.println(IP地址vtd);out.println(,+request.getRemoteAddr()+,);out.println();out.printtin(口);out.println(+request.getRemotePort()+);out.println();OUt.println(Vtrvtd

15、恳求方法vtd);out.println(,+request.getMethod()+u);out.println();OUt.printingvtr恳求协议vtd);out.println(,+request.getProtocol()+u);out.println(,);OUt.println(vtrvtd编码方式vtd);out.println(+request.getCharacterEncodingO+,);out.println();OUt.printingvtrvtd内容类型vtd);out.println(,+request.getContentType()+);out.pri

16、ntln(,);OUt.println(vtrvtd恳求URI);out.println(+request.getRequestURlQ+,);out.println(,);out.println();out.printin();)publicvoiddoPost(ServletRequestrequest,ServletResponseresponse)throwsServletException,IOException(doGet(request,response);程序2.6ShowHeadersServletJavapackagecom.demo;importjava.io.*;imp

17、ortjavax.servlet.*;importjavax.servlet.*;importjava.util.Enumeration;publicclassShowHeadersServletextendsServletpublicvoidservice(ServletRequestrequest,ServletResponseresponse)throwsServletException,IOExceptionresponse.setContentType(,texthtmlicharset=gb2312u);PrintWriterout=response.getWriter();out

18、.printin(,);outprintln(下面是服务器收到的恳求头信息);out.printin(request.getMethod()+,+request.getRequestURI()+,+request.getQueryString()+request.getProtocol()+,);Enumerationheaders=request.getHeaderNamesO;while(headers.hasMoreElements()Stringheader=(String)headers.nextElement();Stringvalue=request.getHeader(head

19、er);out.println(header+=+value+,);)out.println();程序2.7LoginServletJavapackagecom.demo;importjava.io.*;importjavax.servlet.*;importjavax.servlet.*;publicclassLoginServletextendsServletpublicvoiddoGet(ServletRequestrequest,ServletResponseresponse)throwsServletException,IOExceptionStringusername=reques

20、t.getParameter(username);Stringpassword=request.getParameter(upassword);Stringlocation=,;if(userame.equals(admin,)5password.equals(uadmin)request.setAttribute(,usemamer,username);location=/hello.jsp;)else(location=/login.html;RequestDispatcherdispatcherrequest.getRequestDispatcher(location);dispatch

21、er.forward(request,response);程序2.8hello.jspYourarewelcome!程序2.9ExcelServlet.javapackagecom.demo;importjava.io.*;importjava.util.*;importjavax.servlet.*;importjavax.servlet.?publicclassExcelServletextendsServlet(publicvoiddoGet(ServletRequestrequest,ServletResponseresponse)throwsServletException,IOEx

22、ception/指定页面在传输过程中运用的编码方式response.setHeader(Content-Encoding,ngb2312n);response.setContentType(,applicationvnd.ms-exceljcharset=gb2312,);PrintWriterout=response.getWriter();OUt.println(学号t姓名t性别t年龄t所在系”);out.println(95001t李勇t男t20t信息”);out.println(95002t刘晨t女tl9t数学”);程序2.10packagecom.demo;importjava.io

23、.*;importjavax.servlet.*;importjavax.servlet.publicclassextendsServlet(publicvoiddoGet(ServletRequestrequest,ServletResponseresponse)throwsServletException,IOExceptionresponse.SetContentType(,application/jar);/设置响应头,将响应内容以指定的文件名存储到客户机上response.setHeader(uContent-Dispositionn,Attachment;1);Filef=newF

24、Ue(nC:servlet-api.jar11);is=new(f);ServletOutputStreamos=response.getOutputStream();bytebytearray=newbyte1024;intbytesread=O;while(bytesread=is.read(bytearray)!=-1)os.write(bytearray,O,bytesread);os.flush();程序2.11ShowTimeServletJavapackagecom.demo;importjava.io.*;importjava.util.*;importjava.text.Si

25、mpleDateFormat;importjavax.servlet.*;importjavax.servlet.*;publicclassShowTimeServletextendsServletpublicvoiddoGet(ServletRequestrequest,ServletResponseresponse)throwsServletException,IOExceptionresponse.setContentType(,texthtmljcharset=gb2312u);response.setHeader(,RefreshM/5M);PrintWriterout=respon

26、se.getWriter();Datedate=newDate();SimpleDateFormatsdf=newSimpleDateFormat(,yyyy-MM-ddaHH:mm:ssu);Strings=sdf.format(date);/对日期对象格式化out.pritl(,u);out.println();out.println(每5秒钟刷新一次页面u);out.println(现在的时间是:+s+1,);out.println(,);out.println(,u);)程序2.12RedirectServletJavapackagecom.demo;importjava.io.*;i

27、mportjavax.servlet.*;importjavax.servlet.*;publicclassRedirectServletextendsServletpublicvoiddoGet(ServletRequestrequest,ServletResponseresponse)throwsIOException,ServletExceptionStringUserAgent=request.getHeader(,User-Agent);if(userAgent!=null)(userAgent.indexOf(,MSIE)!=-l)response.sendRedirect(,);return;elseresponse.sendRediect();return;程序3.1web.xml文件。WebappTestemailloginServletcom.demo.LoginServlet/servlet-classgreetingGoodMorning2loginServletlogin.dowelcome-welcome-20程序3.2web.xmlconfigDemoServletServletemailtelephone1conf

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 在线阅读 > 生活休闲


备案号:宁ICP备20000045号-1

经营许可证:宁B2-20210002

宁公网安备 64010402000986号