福建农林大学J2EE实验报告五.docx
- 文档编号:26021153
- 上传时间:2023-06-17
- 格式:DOCX
- 页数:15
- 大小:34.33KB
福建农林大学J2EE实验报告五.docx
《福建农林大学J2EE实验报告五.docx》由会员分享,可在线阅读,更多相关《福建农林大学J2EE实验报告五.docx(15页珍藏版)》请在冰豆网上搜索。
福建农林大学J2EE实验报告五
福建农林大学计算机与信息学院实验报告
系
计算机与信息
专业
计算机科学与技术
年级
2013
成绩
姓名
学号
实验室
机号
实验时间
教师签字
实验(五)过滤器和监听器
一、实验目的和要求
1.掌握如何实现一个Servlet过滤器步骤
二、实验内容和原理
1.自定义一个过滤器,用来过滤非法文字。
如果用户提交的信息中含有非法文字,则使用“*”字符替代之前的非法字符。
要求:
编写3个文件,即
用来提交用户发言信息的表单
用来过滤非法文字的过滤器
显示过滤后的用户发言的页面
三、实验环境
1.硬件环境:
2.软件环境:
JDK1.5
四、算法描述及实验步骤
1.算法描述(可以用流程图、伪代码或源程序描述)
2.实验步骤
Index.html
Handle.jsp
<%
StringsOutput=null;
sOutput=(String)request.getAttribute("output");
out.print(sOutput);
%>
Web.xml
xmlversion="1.0"encoding="UTF-8"?
>
//xmlns.jcp.org/xml/ns/javaee"xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance"xsi: schemaLocation="http: //xmlns.jcp.org/xml/ns/javaeehttp: //xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 30 newPackage.CharFilter.java packagenewpackage; importjava.io.IOException; importjava.io.PrintStream; importjava.io.PrintWriter; importjava.io.StringWriter; importjavax.servlet.Filter; importjavax.servlet.FilterChain; importjavax.servlet.FilterConfig; importjavax.servlet.ServletException; importjavax.servlet.ServletRequest; importjavax.servlet.ServletResponse; /** * *@authorAdministrator */ publicclassCharFilterimplementsFilter { privatestaticfinalbooleandebug=true; privateFilterConfigfilterConfig=null; privateString[]sKeyWords; publicCharFilter() { } privatevoiddoBeforeProcessing(ServletRequestrequest,ServletResponseresponse) throwsIOException,ServletException { if(debug) { log("CharFilter: DoBeforeProcessing"); } } privatevoiddoAfterProcessing(ServletRequestrequest,ServletResponseresponse) throwsIOException,ServletException{ if(debug){ log("CharFilter: DoAfterProcessing"); } } /** * *@paramrequestTheservletrequestweareprocessing *@paramresponseTheservletresponsewearecreating *@paramchainThefilterchainweareprocessing * *@exceptionIOExceptionifaninput/outputerroroccurs *@exceptionServletExceptionifaservleterroroccurs */ publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain) throwsIOException,ServletException { if(debug) { log("CharFilter: doFilter()"); } doBeforeProcessing(request,response); //获取关键字 sKeyWords=newString[3]; sKeyWords[0]=filterConfig.getInitParameter("KeyWord0"); if(sKeyWords[0]==null) { sKeyWords[0]=""; } sKeyWords[1]=filterConfig.getInitParameter("KeyWord1"); if(sKeyWords[1]==null) { sKeyWords[1]=""; } sKeyWords[2]=filterConfig.getInitParameter("KeyWord2"); if(sKeyWords[2]==null) { sKeyWords[2]=""; } //获取输入 request.setCharacterEncoding("GB2312"); StringsInput=request.getParameter("input"); if(sInput==null) { chain.doFilter(request,response); return; } for(inti=0;i { sInput=myReplace(sInput,sKeyWords[i],'*'); } request.setAttribute("output",sInput); //送往下一个过滤器 Throwableproblem=null; try { chain.doFilter(request,response); } catch(Throwablet) { //Ifanexceptionisthrownsomewheredownthefilterchain, //westillwanttoexecuteourafterprocessing,andthen //rethrowtheproblemafterthat. problem=t; t.printStackTrace(); } doAfterProcessing(request,response); //Iftherewasaproblem,wewanttorethrowitifitis //aknowntype,otherwiselogit. if(problem! =null) { if(probleminstanceofServletException) { throw(ServletException)problem; } if(probleminstanceofIOException) { throw(IOException)problem; } sendProcessingError(problem,response); } } publicStringmyReplace(StringsInput,StringsKeyWord,charcReplace) { if(sInput==null||sInput.isEmpty()) { System.exit(0); } if(sKeyWord==null||sKeyWord.isEmpty()) { returnsInput; } intnlength=sKeyWord.length(); StringsReplace=""; while(nlength! =0) { sReplace=sReplace+Character.toString(cReplace); nlength--; } //替换大写 returnsInput.replaceAll(sKeyWord,sReplace); } /** *Returnthefilterconfigurationobjectforthisfilter. */ publicFilterConfiggetFilterConfig() { return(this.filterConfig); } /** *Setthefilterconfigurationobjectforthisfilter. * *@paramfilterConfigThefilterconfigurationobject */ publicvoidsetFilterConfig(FilterConfigfilterConfig){ this.filterConfig=filterConfig; } /** *Destroymethodforthisfilter */ @Override publicvoiddestroy() { this.filterConfig=null; } /** *Initmethodforthisfilter *@paramfilterConfig */ @Override publicvoidinit(FilterConfigfilterConfig) { this.filterConfig=filterConfig; if(filterConfig! =null) { if(debug) { log("CharFilter: Initializingfilter"); } } } /** *ReturnaStringrepresentationofthisobject. */ @Override publicStringtoString() { if(filterConfig==null) { return("CharFilter()"); } StringBuffersb=newStringBuffer("CharFilter("); sb.append(filterConfig); sb.append(")"); return(sb.toString()); } privatevoidsendProcessingError(Throwablet,ServletResponseresponse) { StringstackTrace=getStackTrace(t); if(stackTrace! =null&&! stackTrace.equals("")) { try{ response.setContentType("text/html"); PrintStreamps=newPrintStream(response.getOutputStream()); PrintWriterpw=newPrintWriter(ps); pw.print("\n //PENDING! Localizethisfornextofficialrelease pw.print("Theresourcedidnotprocesscorrectly
\n\n");
pw.print(stackTrace);
pw.print("\n");//NOI18N
pw.close();
ps.close();
response.getOutputStream().close();
}catch(Exceptionex){
}
}else{
try{
PrintStreamps=newPrintStream(response.getOutputStream());
t.printStackTrace(ps);
ps.close();
response.getOutputStream().close();
}catch(Exceptionex){
}
}
}
publicstaticStringgetStackTrace(Throwablet){
StringstackTrace=null;
try{
StringWritersw=newStringWriter();
PrintWriterpw=newPrintWriter(sw);
t.printStackTrace(pw);
pw.close();
sw.close();
stackTrace=sw.getBuffer().toString();
}catch(Exceptionex){
}
returnstackTrace;
}
publicvoidlog(Stringmsg)
{
filterConfig.getServletContext().log(msg);
}
}
五、调试过程
1.编译过程
2.调试过程
六、实验结果
用与测试用例不同的输入数据运行算法,写出得到的结果,并分析结果是否正确。
七、总结
通过这次实验,对过滤器有了进一步的了解。
附录:
如果原来的算法中发现了错误,在附录中附上改正后的算法实现。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 福建 农林 大学 J2EE 实验 报告