Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Servlets Code Examples

Reading from an abstract resource

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
Reading from an abstract resource package com.ack.web.servlet; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * This class demonstrates how to view static resources * within a web application. It is a URL based scheme * for reading resources within the web application. This * makes it a more portable solution than the usual * getPathTranslated() technique that deals exclusively * with files. * * This class caters for web applications deployed as WAR * files and those that are distributed across machines. * * Note that this is not a production level implementation * and really opens up your web application to anyone. It * is only meant as a demonstration of how to use * getServletContext().getResource() for reading resources * within a web application. * * Use As Follows: * http://<server>:<port>/<web-app>/servlet/<servlet path>/file_to_view * * For example to view my index.jsp page in my web apps root directory * * http://localhost:8080/ack/vf/index.jsp * * where /vf/* is mapped to the com.ack.web.servlet.ReadingFromAnAbstractResource */ public class ReadingFromAnAbstractResource extends HttpServlet { public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException { ServletOutputStream os = res.getOutputStream(); res.setContentType( "text/plain" ); // get the resource String resourcePath = req.getPathInfo(); if( resourcePath == null ) { res.sendError( HttpServletResponse.SC_NOT_FOUND ); return; } System.out.println( "about to read resource -> " + resourcePath ); // get resource as a url URL resourceUrl = getServletContext().getResource( resourcePath ); if( resourceUrl == null ) { res.sendError( HttpServletResponse.SC_NOT_FOUND ); return; } // open connection to resource URLConnection con = null; try { con = resourceUrl.openConnection(); con.connect(); } catch( IOException ioe ) { res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "unable to read resource: " + resourcePath ); } // set the actual content type in; must check that its non-null String contentType = con.getContentType(); if( contentType != null ) { res.setContentType( contentType ); } // send resource contents back down output stream try { InputStream is = con.getInputStream(); byte[] buf = new byte[8096]; int bytesRead; while( ( bytesRead = is.read( buf ) ) != -1 ) { os.write( buf, 0, bytesRead ); } } catch( IOException ioe ) { res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "unable to send resource to client: " + resourcePath ); } } }