servlet 很不错的东西,伴随着我成长,嘿嘿

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

44ServletJasonHunter1996ServletJavaJavaWebJavaWebWebServletServletServletApacheStrutskeletonServletServletHttpSessionSingleThreadModelSaveAs?HTTPServlet45ServletWebServletServletAPIJavaWebServletAPIServletout.println()ServletWebout.println()JavaServerPagesJSPWebMacroVelocityWebJavaServletProgrammingO'Reilly3-1WebServletServlet11ServletWebhttp;//://java.sun.com/blueprints/enterpriseSunJ2EEMVCWeb3ModelViewControllerMVCBluePrintsCommandActionActionActionActionJ2EEBluePrintsApacheStrutsApacheStrutsApacheStruts*;importjava.util.prefs.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassWastedConversionsextendsHttpServlet{//Stringname=content.txt;publicvoiddoGet(HttpServletRequestreq,HttpServletResponseres)throwsServletException,IOException{Stringfile=getServletContext().getRealPath(name);res.setContentType(text/plain);PrintWriterout=res.getWriter();returnFile(file,out);}Servlet51publicstaticvoidreturnFile(Stringfilename,Writerout)throwsFileNotFoundException,IOException{Readerin=null;try{in=newBufferedReader(newFileReader(filename));char[]buf=newchar[4*1024];//4KintcharsRead;while((charsRead=in.read(buf))!=-1){out.write(buf,0,charsRead);}}finally{if(in!=null)in.close();}}}3-2ServletServlet3-2importjava.io.*;importjava.util.prefs.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassNoConversionsextendsHttpServlet{Stringname=content.txt;//publicvoiddoGet(HttpServletRequestreq,HttpServletResponseres)throwsServletException,IOException{Stringfile=getServletContext().getRealPath(name);res.setContentType(text/plain);OutputStreamout=res.getOutputStream();returnFile(file,out);}publicstaticvoidreturnFile(Stringfilename,OutputStreamout)throwsFileNotFoundException,IOException{InputStreamin=null;try{in=newBufferedInputStream(newFileInputStream(filename));52byte[]buf=newbyte[4*1024];//4KintbytesRead;while((bytesRead=in.read(buf))!=-1){out.write(buf,0,bytesRead);}}finally{if(in!=null)in.close();}}}2MBServletTomcat3.x20%Tomcat4.x50%Servlet3-2CPU3-2URLResultSetgetAsciiStream()getCharacterStream()ASCIIASCIIUCS-2ServletString.getBytes()StringServlet533-3Servlet-OutputStreamwrite(byte[])println(String)3-3ValueObjectProxy.javaimportjava.io.*;importjava.sql.*;importjava.util.Date;importjavax.servlet.*;importjavax.servlet.http.*;publicclassAsciiResultextendsHttpServlet{publicvoiddoGet(HttpServletRequestreq,HttpServletResponseres)throwsServletException,IOException{res.setContentType(text/html);ServletOutputStreamout=res.getOutputStream();//ServletOutputStreamprintln()//println()//Content-Type//out.write.(str.getBytes(Shift_JIS))out.println(Contentcurrentasof);out.println(newDate().toString());//ResultSettry{InputStreamascii=resultSet.getAsciiStream(1);returnStream(ascii,out);}catch(SQLExceptione){thrownewServletException(e);}}publicstaticvoidreturnStream(InputStreamin,OutputStreamout)throwsFileNotFoundException,IOException{byte[]buf=newbyte[4*1024];//4KintbytesRead;54while((bytesRead=in.read(buf))!=-1){out.write(buf,0,bytesRead);}}}ASCIIASCIIServletAPI1.0ServletAPI2.3ServletServletServletJ2EEJNDIWebWebWEB-INF/classesWARServletJNDI3-4ResourceResourcefile:URLFileURLfile:URL*;importjava.net.*;importjava.util.*;/****CLASSPATH*Resource.class.getResource(/+name)*Resourcefile:URLfile*URL*/publicclassResourceimplementsSerializable{privateStringname;privateFilefile;privateURLurl;publicResource(Stringname)throwsIOException{this.name=name;SecurityExceptionexception=null;try{//CLASSPATHfiletrue//SecurityExceptionif(tryClasspath(name)){return;}}catch(SecurityExceptione){exception=e;//}try{//classloadergetResource()//fileURLurlif(tryLoader(name)){return;}}catch(SecurityExceptione){exception=e;//}//Stringmsg=;56if(exception!=null){msg=:+exception;}thrownewIOException(Resource'+name+'couldnotbefoundin+theCLASSPATH(+System.getProperty(java.class.path)+),norcoulditbelocatedbytheclassloaderresponsibleforthe+webapplication(WEB-INF/classes)+msg);}/****/publicStringgetName(){returnname;}/****/publicInputStreamgetInputStream()throwsIOException{if(file!=null){returnnewBufferedInputStream(newFileInputStream(file));}elseif(url!=

1 / 30
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功