您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 经营企划 > 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!=
本文标题:servlet 很不错的东西,伴随着我成长,嘿嘿
链接地址:https://www.777doc.com/doc-4356967 .html