File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes PHP and the fly likes How to run PHP file on Tomcat server? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » PHP
Bookmark "How to run PHP file on Tomcat server?" Watch "How to run PHP file on Tomcat server?" New topic
Author

How to run PHP file on Tomcat server?

kumar sapan
Greenhorn

Joined: Dec 13, 2013
Posts: 10
Hi,

I am new to PHP.
I am using Eclipse Juno IDE, Tomcat server, mysql database on Mac OSX.
I created a dynamic web project and created some web pages. In one of the pages, I created login form... To display the error message on the same page by comparing the username and password with the database, I need to use PHP.. but I am unable to use PHP in my dynamic web project. Can anyone help me how can I run the PHP files on Tomcat server or suggest can I achieve this using any other javascript like AJAX.

Please help me.. I am really struggling with this.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

I don't know... there are thousands if not millions of websites around the world which make people log in, and not all of them are written in PHP. So your statement that you need to use PHP isn't all that convincing. Your later suggestion that you could use AJAX is a better idea, if you are forced to use Tomcat. But if you're more comfortable with PHP you could discard the Tomcat idea and write your website all in PHP. After all there are plenty of websites around the world which are written in PHP too.
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 509
    
    6
If what you have now is pure HTML pages, and if you intend to implement your complete web app (including database access ) with PHP as server side language, then Tomcat isn't the right server; what you need instead is apache httpd (which is commonly called simply "apache server"). You can optionally install either the PHPclipse plugin or download the PDT distribution of eclipse; they are targeted at PHP developers and facilitate deployment of PHP webapps on apache server. For the error display, you can use a javascript framework like jquery to send ajax requests to server and reply to those requests from PHP scripts.

On the other hand, if you have already implemented your pages and database access using JSP/servlets and already deployed on tomcat, then you don't need PHP at all. Just use a javascript framework like jquery to send ajax requests to server and reply to those requests from a servlet.

PHP can't execute on tomcat; or more accurately, it can probably be coaxed to run with lots of deployment gymnastics and patience, but nobody does so because it's not the right tool for the job. Apache httpd is the right server for PHP.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2419
    
    7

Tomcat is servlet/JSP container not a PHP container. If you need to run PHP then Apache web server is needed.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41874
    
  63
... and specifically something like the mod_php module for enabling PHP support in Apache.


Ping & DNS - my free Android networking tools app
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11460
    
  94

It is possible to run PHP applications in Tomcat. For example, you could use the open source PHP/Java Bridge to do this.

But unless you have a need for Java applications to be running at the same time, you are probably better off running an Apache HTTPD server. Even if you need both, I would normally recommend using the Apache HTTPD server for static content, PHP files, access controls - things that it is great at, and that Tomcat is not really designed for.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

I'm still questioning the need for PHP at all.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
kumar sapan
Greenhorn

Joined: Dec 13, 2013
Posts: 10
Paul Clapham wrote:I don't know... there are thousands if not millions of websites around the world which make people log in, and not all of them are written in PHP. So your statement that you need to use PHP isn't all that convincing. Your later suggestion that you could use AJAX is a better idea, if you are forced to use Tomcat. But if you're more comfortable with PHP you could discard the Tomcat idea and write your website all in PHP. After all there are plenty of websites around the world which are written in PHP too.



Yes, I agree with you. But my question is while using html... Or JSP which is the best option for server side checking. Now, I am using servlets. But in servlets we need to add new page to display the errors. I want to display the errors in the same page... I try to use AJAX. But the AJAX is internally using php. (May be I am wrong) but when I search on google. I found that.) I don't know whether I can use only AJAX to achieve this. If it is possible, can you suggest me which is the site to learn AJAX.
Thank you for the reply.
kumar sapan
Greenhorn

Joined: Dec 13, 2013
Posts: 10
Karthik Shiraly wrote:If what you have now is pure HTML pages, and if you intend to implement your complete web app (including database access ) with PHP as server side language, then Tomcat isn't the right server; what you need instead is apache httpd (which is commonly called simply "apache server"). You can optionally install either the PHPclipse plugin or download the PDT distribution of eclipse; they are targeted at PHP developers and facilitate deployment of PHP webapps on apache server. For the error display, you can use a javascript framework like jquery to send ajax requests to server and reply to those requests from PHP scripts.

On the other hand, if you have already implemented your pages and database access using JSP/servlets and already deployed on tomcat, then you don't need PHP at all. Just use a javascript framework like jquery to send ajax requests to server and reply to those requests from a servlet.

PHP can't execute on tomcat; or more accurately, it can probably be coaxed to run with lots of deployment gymnastics and patience, but nobody does so because it's not the right tool for the job. Apache httpd is the right server for PHP.



Thank you very much for your explanation. I already implemented my pages and database access using JSP and servlets. But when I search on google with AJAX, it is again internally using PHP file. So, I thought PHP is mandatory for server side checking. And also one more thing... We can use Java code inside the JSP, right. But why it is not recommended... (I somewhere read that using java inside the JSP is not recommended.)
Can you suggest me whether W3SCHOOLS best to start with jQuery. Can you suggest me any other website also.
Thank you once again for your answer. I completely understand now.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

Ajax does not require PHP. Ajax makes a request to the server, and it can call any type of resource: PHP, servlets, or anything else. You do not need to use PHP in order to use Ajax.
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 509
    
    6
JSP is HTML + java code. This java code can be included either as scriptlets with raw java code, or as custom JSP tags which include java code indirectly.
Which one to use is a subjective preference. Some people like including java code directly as scriptlets; others prefer custom tags.

I prefer customs tags and strongly dislike scriptlets for the following reasons:
1. Better Readability and "Codeability": To my eyes, this snippet with custom tags


is easier to type, read,and understand, than this scriptlet version:

Look at how the scriptlet has so much more verbose code, extra keystrokes and scope for mismatched braces!

2. Compiler error checking support: If there is a syntax or semantic problem in the above JSP scriptlet, we usually don't get to know it until the page has been deployed on server,
because most default configurations go for on-demand runtime JSP compilation, instead of build time compilation.
On the other hand, a custom tag library is like any other piece of pure java code and needs to be compiled first before deploying. So it's easy to find out
most syntactic or semantic faults.

I started to frame more reasons, but then guessed that some angel here would have already written a comprehensive FAQ, and indeed they have! Go through it.



Can you suggest me whether W3SCHOOLS best to start with jQuery. Can you suggest me any other website also.

In order to appreciate why frameworks like jquery are useful, you first need to understand what exactly are the problems when using plain javascript.
If you are not familiar with javascript, then my suggestion is learn plain javascript first and implement ajax and basic DOM manipulation using it for your current app (learn-by-doing). There are ajax and DOM manipulation examples on w3schools.
Also check out this site's own javascript FAQ.
Once you have understood the problems with that approach, learn jquery to appreciate how it makes life easier. The FAQ above has links to good resources.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

Karthik Shiraly wrote:JSP is HTML + java code.

Not since 2002. JSP is a template to create a text response, which is usually HTML (bit can be any text). Java code is no longer used in JSP, but rather the JSTL and EL.
kumar sapan
Greenhorn

Joined: Dec 13, 2013
Posts: 10
Karthik Shiraly wrote:JSP is HTML + java code. This java code can be included either as scriptlets with raw java code, or as custom JSP tags which include java code indirectly.
Which one to use is a subjective preference. Some people like including java code directly as scriptlets; others prefer custom tags.

I prefer customs tags and strongly dislike scriptlets for the following reasons:
1. Better Readability and "Codeability": To my eyes, this snippet with custom tags


is easier to type, read,and understand, than this scriptlet version:

Look at how the scriptlet has so much more verbose code, extra keystrokes and scope for mismatched braces!

2. Compiler error checking support: If there is a syntax or semantic problem in the above JSP scriptlet, we usually don't get to know it until the page has been deployed on server,
because most default configurations go for on-demand runtime JSP compilation, instead of build time compilation.
On the other hand, a custom tag library is like any other piece of pure java code and needs to be compiled first before deploying. So it's easy to find out
most syntactic or semantic faults.

I started to frame more reasons, but then guessed that some angel here would have already written a comprehensive FAQ, and indeed they have! Go through it.



Can you suggest me whether W3SCHOOLS best to start with jQuery. Can you suggest me any other website also.

In order to appreciate why frameworks like jquery are useful, you first need to understand what exactly are the problems when using plain javascript.
If you are not familiar with javascript, then my suggestion is learn plain javascript first and implement ajax and basic DOM manipulation using it for your current app (learn-by-doing). There are ajax and DOM manipulation examples on w3schools.
Also check out this site's own javascript FAQ.
Once you have understood the problems with that approach, learn jquery to appreciate how it makes life easier. The FAQ above has links to good resources.



Thank you for your suggestion. This is really helpful. I will do that.
Thank you.
kumar sapan
Greenhorn

Joined: Dec 13, 2013
Posts: 10
Bear Bibeault wrote:
Karthik Shiraly wrote:JSP is HTML + java code.

Not since 2002. JSP is a template to create a text response, which is usually HTML (bit can be any text). Java code is no longer used in JSP, but rather the JSTL and EL.


Thank you. Even I thought that we can use java inside the JSP. When I read that this is not good way to approach then I did not know the reason. Now I got it.
Nick Charles
Ranch Hand

Joined: Oct 09, 2011
Posts: 71

To run PHP in Tomcat, take a look at Quercus: http://www.caucho.com/resin-3.1/doc/quercus.xtp
You could also try using phpservlet: http://phpservlet.sourceforge.net/
I've used both with JBoss AS 4.2.x and 5.x; since those versions of JBoss AS use a variant of Tomcat you should also be able also use Quercus and phpservlet on Tomcat.
Personally, once I started using Quercus I never bothered with phpservlet any more.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to run PHP file on Tomcat server?