I'm sure this is horribly insufficient, so I would recommend reading the servlet spec. It's not all that bad a read as far as specifications go and you can learn a lot about how tomcat operates.
If you have a servlet mapping all *.jpg to some servlet "myServlet" in web.xml, web.xml also has another section where the servlets are individually declared. In there you'll find the class that backs the servlet named "myServlet". All that means is the request for the resource goes to that specific servlet class to be handled. What the servlet does with it is open-ended. In the case of the default servlet, it basically calls sc.getResourceAsStream( request.getServletPath() + request.getPathInfo() ) and returns the contents of the file to the client. That's over simplified -- the real default servlet does some security checks to be sure it's not returning something it shouldn't and set's the mime type in the response, but you get the idea. sc.getResourceAsStream is aware of the webapp's location in the filesystem and takes care of finding the resource in the webapp's folder. sc.getRealPath will only return a full path to a file if the webapp is NOT compressed in a web archive file (.war). From within a .war file, it always returns null and isn't recommended except in rare cases where you might want to write access. This is all in the spec. Servlet 2.4 spec is available at http://jcp.org/aboutJava/communityprocess/final/jsr154/index.html --David Williams, Allen wrote: >I'm new at this, so bear with me here for a moment... > >The servlet mapping seems to me to tell tomcat "anytime you have a >request for a URI with .jpg extension, deliver the request to this >servlet", but that doesn't give any information about where in the >"real" file system said jpeg is stored, does it? So, when you call >sc.getRealPath(), how does the servlet context know where to go? >Doesn't there have to be a mapping or alias somewhere (server.xml, >web.xml,...?) that resolves, or translates "ThisTypeofFileName.ext" into >"/real/path/in/OS/ThatTypeOfFileName.oxt"? > > >-----Original Message----- >From: John Pedersen [mailto:[EMAIL PROTECTED] >Sent: Wednesday, February 21, 2007 4:37 AM >To: Tomcat Users List >Subject: Re: where to store user-generated files? > >Looks like roll your own then! > >A few thoughts on the matter - maybe someone could add to them? > >It should be easy to map requests for images to a servlet, which can >then find the appropriate image file wherever it might be ( within or >outside the server ). Like this in the web.xml file: > ><servlet-mapping> > <servlet-name>servletName</servlet-name> > <url-pattern>*.jpg</url-pattern> ></servlet-mapping> > >? > >But how is the image then added to the reponse? Another servlet ( >behind the scenes - I am actually using the Spring framework ) is >handling the request/response. Can the request/response be passed to >the image-providing servlet for the images within a page to be written >to the reponse in this kind of way: > > // This method is called by the servlet container to process a GET >request. > public void doGet(HttpServletRequest req, HttpServletResponse >resp) throws IOException { > // Get the absolute path of the image > ServletContext sc = getServletContext(); > String filename = sc.getRealPath("image.gif"); > > // Get the MIME type of the image > String mimeType = sc.getMimeType(filename); > if (mimeType == null) { > sc.log("Could not get MIME type of "+filename); > >resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); > return; > } > > // Set content type > resp.setContentType(mimeType); > > // Set content size > File file = new File(filename); > resp.setContentLength((int)file.length()); > > // Open the file and output streams > FileInputStream in = new FileInputStream(file); > OutputStream out = resp.getOutputStream(); > > // Copy the contents of the file to the output stream > byte[] buf = new byte[1024]; > int count = 0; > while ((count = in.read(buf)) >= 0) { > out.write(buf, 0, count); > } > in.close(); > out.close(); > } > >( from http://www.exampledepot.com/egs/javax.servlet/GetImage.html ) > >I'm off for a walk to mull it over - any suggestions while I am out >and before I get to experimenting will be most welcome. > >Thanks, > >John > >--------------------------------------------------------------------- >To start a new topic, e-mail: users@tomcat.apache.org >To unsubscribe, e-mail: [EMAIL PROTECTED] >For additional commands, e-mail: [EMAIL PROTECTED] > > >--------------------------------------------------------------------- >To start a new topic, e-mail: users@tomcat.apache.org >To unsubscribe, e-mail: [EMAIL PROTECTED] >For additional commands, e-mail: [EMAIL PROTECTED] > > > --------------------------------------------------------------------- To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]