JWhich as a servlet 

Joined:
04/09/2007
Posts:
710

October 14, 2008 23:02:29    Last update: October 14, 2008 23:03:50
Wrap JWhich in a servlet:
  1. Implement the servlet:
    package com.example;
    
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class JWhichServlet extends HttpServlet {
        public void service(HttpServletRequest req,
                            HttpServletResponse resp)
                    throws ServletException, IOException {
            PrintWriter pw = new PrintWriter(resp.getOutputStream());
            int idx = req.getRequestURI().lastIndexOf("/");
            if (idx < 0) {
                pw.printf("Nothing to do for: %s", req.getRequestURI());
            }
            else {
                java.net.URL classUrl = null;
                String className = req.getRequestURI().substring(idx + 1);
                if (className != null) {
                    // try to find a normal file, such as log4j.properties
                    String clsName = "/" + className;
                    classUrl = this.getClass().getResource(clsName);
    
                    if (classUrl == null) {
                        // try to find a class
                        clsName = clsName.replace('.', '/') + ".class";
                        classUrl = this.getClass().getResource(clsName);
                    }
                }
    
                if (classUrl == null) {
                    pw.printf("'%s' not found", className);
                }
                else {
                    pw.printf("'%s' found in: %s",
                              className,
                              classUrl.getFile());
                }
            }
            pw.close();
        }
    }
    


  2. Add this to web.xml:
    <servlet>
        <servlet-name>jwhich</servlet-name>
        <servlet-class>com.example.JWhich</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>jwhich</servlet-name>
        <url-pattern>/jwhich/*</url-pattern>
    </servlet-mapping>
    

  3. Request the path for a class or properties file:
    http://mydomain.com/jwhich/com.example.SomeClass
    http://mydomain.com/jwhich/log4j.properties
    

Share |
| Comment  | Tags
 
Easy email testing with http://www.ximailstop.com