users@grizzly.java.net

php on GrizzlyWebServer 100%java with JSR 223

From: Survivant 00 <survivant00_at_gmail.com>
Date: Sat, 31 Jan 2009 19:28:11 -0500

I have something that can do PHP wihtout native php installation.

I wasn't able to get Quercus servlet to work with GrizzlyWebServer, but I
have a workaround for now that will allow the users to run php with the
JSR223.

you have a create a serlvet that you will use with GrizzlyWebServer (I'll
post a complete demo on my blog later this weekend)




    protected void doPut (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

        System.out.println("doPut");

        Object php2javaResult = null;

        ScriptContext context = phpEngine.getContext();

        try {
            context.setWriter(new StringWriter());

            //put the parameters and attributes into the engine
            Enumeration attributes = req.getAttributeNames();

            while (attributes.hasMoreElements()) {
                String key = (String) attributes.nextElement();

                // put the parameters and attributes into the engine
                phpEngine.put(key, req.getAttribute(key));
            }

            Map<String, Object> params = req.getParameterMap();

            for (Iterator iterator = params.keySet().iterator();
iterator.hasNext();) {
                String key = (String) iterator.next();

                // put the parameters and attributes into the engine
                phpEngine.put(key, params.get(key));
            }

            // need a better way to find the requested file
            String uri = req.getRequestURI();

            if(uri.startsWith("/")){
                uri = uri.substring(1);
            }

            php2javaResult = phpEngine.eval(new FileReader(uri), context);
            //php2javaResult = phpEngine.eval(new FileReader("phpinfo.php"),
context);
            StringWriter writer = (StringWriter) context.getWriter();
            res.getWriter().println(writer.toString());
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    protected void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
        doPut(req, res);
    }


if I enter the URL :
http://localhost/phpinfo.php?coucou&id=12

the file phpinfo will be executed with the params in the URL.


PS. if I need to put attributes in the response just tell me.. I did though
about that.

PS2 : if you need a complete solution... Glassfish with Quercus is one good
solution.

PS3 : I used Quercus librairies for this example.

2009/1/29 Richard Corsale <igf1_at_yahoo.com>

> Great tutorial man!! I was wondering about this very subject the other day.
>
> I still can't get my PHP scripts to run on Grizzly... I don't know why I
> wrote them in SQL?? :)
>
> --Richard
>
>
> ------------------------------
> *From:* Survivant 00 <survivant00_at_gmail.com>
> *To:* users_at_grizzly.dev.java.net
> *Sent:* Thursday, January 29, 2009 1:53:27 PM
> *Subject:* Demo : How to host jsp files with GrizzlyWebServer
>
> I did a little demo how to use JSP and GrizzlyWebServer.
>
> http://weblogs.java.net/blog/survivant/archive/2009/01/jsp_on_grizzlyw.html
>
> you won't do a complete web server with that.. but I'm sure you can still
> have fun :)
>
>
>