PHP equivalent of Java Servlet - java

I am trying to understand what's the correspondent of servlets and applets in PHP but I don't have much experience in JAVA.
I want to know what is the equivalent of Java Servlets and JSP in PHP ?

In Java the Servlet delivers the web page to the client. In PHP there are multiple web server which delivers the same functionality:
Apache2
NginX
lighttpd
The PHP script is similar to JSP. It was designed to be used as template.
However overtime PHP was used more and more also not only for templates. In the last year there are appearing new approaches where PHP is used to write an entire web server. E.g.
appserver.io
reactphp

I think the equivalent of Java Servlets and JSP is PHP.
Taken from Wikipedia - https://en.wikipedia.org/wiki/Java_Servlet:
"The servlet is a Java programming language class used to extend the capabilities of a server. Although servlets can respond to any types of requests, they are commonly used to extend the applications hosted by web servers, so they can be thought of as Java applets that run on servers instead of in web browsers.[1] These kinds of servlets are the Java counterpart to other dynamic Web content technologies such as PHP and ASP.NET."

Related

General session handling in java

I work with multiple languages like PHP, C, JAVA etc.
I am trying to fully understand how session is implemented in java.
Question : In php I can create a session and use the session in any class even utility classes. In Java why session is meant to be handled normally only in servlet classes? why not in any basic POJO classes without actually passing session or context object ? (I know other classes can, using complex techniques, but why not easily as servlet or as in other languages ).
I do know that the correct way to do things is use session in Servlet to retrieve object/values and pass object/values to other utility classes and get results.
Please let me know if there is in fact an easy way to get session in basic POJO classes.
I had flagged to delete this question as I found the answer in another SO link Retrieving Web Session from a POJO Outside the Web Container . Since it has not been deleted till now , I am answering my own question.
What are servlet and why we use them ?
A Java servlet is a Java program that extends the capabilities of a server.
Although servlets can respond to any types of requests, they most commonly implement applications hosted on Web servers.
Such Web servlets are the Java counterpart to other dynamic Web content technologies such as PHP and ASP.NET.
More information about java servlet at the link https://en.wikipedia.org/wiki/Java_servlet
Servlet vs POJO/normal Java classes ( why session access only in servlet ?)
A servlet class too is a java class. Servlet is a java class which comply servlet specification, so that it can be run in a server. Java class not required to comply Java Servlet API
A servlet container (e.g. Tomcat) is designed to be able to recognise servlet classes and, if configured correctly, treat them in a particular way. There's nothing magical about this - you can always write your own application that can treat any type of class in a specific way - but there's a standard that all servlet containers follow that means you can write a servlet class and know how it will be used.
Java was designed to be a platform independent language to create software to be embedded in various consumer
electronic devices. Soon it was apparent that the Java can be used in client/server programming environment because of its inherent portability. Hence servlet and applet were introduced in java. Java applets helped create dynamic, self-executing program on client side. Java servlet is a small program that executes on the server. Just as applets dynamically extend the functionality of a web browser, servlets dynamically extend the functionality of a web server.
Since servlet part of java is used in server side programming , this has access to session. POJO classes are also used in server side programming but code which uses session is usually written in servlet.
POJO Vs PHP classes(server side)
Java is used in many development environments like desktop,mobile,embedded and Web. PHP is a server-side scripting language designed for web development.It can also be used for general purpose programming but I am limiting the discussion to class used in php in server side programming(ie .php pages). So it is better to compare php classes in php pages with servlet/JSP - server side development part of JAVA.
Again servlet puts html code inside Java code. JSP puts java code inside html code. So JSP in java is more comparable to PHP .
If we understand this then we can infer that there is no good reason to compare POJO in java to PHP classes (server side). It is like comparing applies to oranges. if you are interested in comparison between php and jsp please visit http://www.withoutbook.com/DifferenceBetweenSubjects.php?subId1=57&subId2=2&d=Difference%20between%20PHP%20and%20JSP
In modern web development we use template engines ie instead of JSP we use servlet with template engine(Freemarker,Velocity). PHP has its own template engines.
How to access session in POJO classes ? Why it is hard / Is there any easy way ?
First of all it should not be done as it would be a bad programming practice. Secondly it can be done but there is hardly any scenario where this would be necessary. Since there is no need for the same the ways to implement it are not easy.
It should not be done , If you have to there are two ways.
We can pass session and request objects from servlet to POJO classes(via methods or constructors).
Store the request in a ThreadLocal in a Filter (and clean it afterwards)
Also look at the SO links for sample code
get HttpSession|Request from simple java class not servlet class
Retrieving Web Session from a POJO Outside the Web Container
I asked this question when I was stuck at a particular scenario . I used to program in PHP and wondered why I never had the issue there and got confused. People who usually move from php to java , also have a hard time understanding the how it all works , so attaching one more part to the answer.
Why servlets require servlet containers like tomcat while php runs on a http server/webserver (Apache).?
The question is incorrect in assumptions made but the answer will highlight why .
A web server is software that helps to deliver web content (web pages) to the clients (e.g. web browser)
through the Internet using HTTP protocol. HTTP is a simple request /response protocol underpinning most web
applications on the Internet, regardless of whether they are written in Java. An HTTP request can correspond to
one of the seven HTTP methods: GET, POST, HEAD, OPTIONS, TRACE, PUT and DELETE
The server side technologies like JSP/Servlets, ASP, and PHP etc. will require their software libraries to be installed
at the server. Without these libraries a web server won’t be able to execute those server technologies and is just an HTTP Server.
An HTTP server can handle HTML and other client side technologies which don’t require any server capability.
The Apache HTTP Server is an example of an HTTP server.
Regarding Servet/JSP : Servlet can be run on a servlet container.Tomcat is normally called a servlet container.A Servlet container is basically a web server for Java Servlets and JSP pages. Tomcat has an HTTP listener inside it, but in addition to that it has a servlet/JSP engine. A servlet, in the end, is a Java class. JSP files (which are similar to PHP, and older ASP files) are generated into Java code (Servlet), which is then compiled to .class files by the server and executed by the Java virtual machine.
Regarding PHP : PHP will not work in pure Apache or any HTTP server. We are able to run php in apache because it is linked to Apache(SAPI - server API) Using mod_php5.so(or similar) modules. We do not normally start any php daemon. Apache starts the php interpreter along with itself.
If oracle wants, it can develop a module similar to PHP module which will make it easier to host Java code on an HTTP server. But using a servlet container model has many benefits compared to the PHP interpreter model even with the disadvantage of increase in complexity.
From Servlet class pass session to any helper class(Create HTTPSession as helper class propertiy).
Now in your POJO class get session from helper class that you have set previously from servlet.
Question : In php I can create a session and use the session in any
class even utility classes. In Java why session is meant to be handled
normally only in servlet classes? why not in any basic POJO classes
without actually passing session or context object ? (I know other
classes can, using complex techniques, but why not easily as servlet
or as in other languages )
Every language has its own design & structure. There is no universal session handling protocol that it should be handled the same way in all the server side languages. In java if handling session in controllers i'e servlet setting & getting attributes serves your purpose why would anyone want to get a session object in a POJO. You can save a POJO in a session in a servlet.
Besides if there are exceptional out of the norm requirements you can always write custom code.

Sending an XML file to a seperate application; JSP or Servlet? JAVA

I have been researching into what type of Java web application I will need to create to carry out this use case activity:
Oracle CRM sends http request with GET variables
System uses GET variables in Oracle SQL query
System sends back XML file with SQL results to CRM via HTTP.
From what I have read so far there is two variations of JAVA that are web applications; those are JSP and Servlet.
Assuming I am not using any frameworks like Spring. What is the best way to implement this as a JSP application or Servlet.
Or is there other Java alternatives.
FYI: I am not a JAVA programmer, this is an introductory (in the deep end of the pool) project.
Thanks
Yes, in your case you only need a Servlet. And yes JSP is only for presentation layer. Although you can have business logic in JSP too because JSP ultimately gets converted to a Servlet in the backend but that's a bad practice not to be followed. :)

Java application vs web service vs web application

I am developing a multi-platform (Android, iPhone, Windows and Blacbberry) mobile application. The application needs to communicate with our server for several tasks, such as retrieving buddy lists etc. The server interacts with data that is stored in a MySQL database. I intend to code the server element in Java, however I am confused by all the different types. So far, I think I have narrowed it down to three options:
1) I code the application using Jetty to accept http posts. I post XML to the server, handle it, interact with the DB and post a XML response back. I would save the application as a jar and leave it running on my server.
2)I develop a Java web service. REST/JSON/SOAP?
3)I develop a Java web application.
Whilst there are many questions already out there asking what the differences is, I am struggling to find a clear explanation as to what is the best approach in which situation. I have previously used the first approach but am assuming the second approach is the better option, I'm just not sure what the advantage is.
your 1-3 options are all variants of a "Web application".
Jetty is a Java based http server/servlet container. If you want to communicate between client and server using http, you are using an http server (although not necessarily Jetty).
A Web Service is part of a web application that conforms to a standard around how clients communicate with the server, and how the server offers up information to the clients.
A web application is a Java application that makes it services available over http.
So if you want to have your clients communicate with a server, and store info in a db, you are using a web-application.
I would recommend going with option 2 as it is more lightweight and can be parsed directly in you're web application. XML got more overhead and must be translated, while you can just serialize objects directly to JSON from you're Java application and then parse them in javascript at frontend

How to integreate Java FX and Spring MVC

I want to use JavaFx as a front-end in my web-application. My question is that is it possible to bind Model object with the form which is developed with Java Fx.
I kindly request you to put some light on this issue.
Please let me know If you need more clarification regarding this
The main differences between Web front-ends (like Spring MVC) and rich clients (and RIAs like JavaFX) is that for web front-ends the server-side logic runs in the same JVM as the web framework while for rich clients the server-side logic and the client are running on 2 separate JVMs, one on the server machine and one on the client machine.
Rich clients are usually downloaded/ installed completely before the user can run it, while for web front-ends each HTML page is possibly first dynamically created and then send to the user as needed.
Since the user usually already has the complete rich client from start, only the actual data (DTOs) get sent back and forth using some kind of remote service e.g Web Services.
So this means that the JavaFX client cannot access the objects of the server (e.g. attached JPA entities). You need to wrap the data up and send it to the JavaFX client using some kind of service (see the Service Facade and DTO design patterns).
The main difference between JAVAFX and any Java EE framework is same as the difference between the swing applications and Java EE apps.
You can design applications using JAVAFX to be directly used on desktop or deployed as browser applets with the help of the Java browser plugin. But, using it as a framework for designing the front end of a Java EE application is not possible.
Read this post :
https://www.java.net//node/674176

What type of java project can serve web services?

Do I have been writing a java application, application is a console application.
Now, I want my application to listen to request at a port coming from web application and process those request and affect DB data.
I am not sure how to do it in a simple java project. Can I use some java webservices, and how?
Socket programming in Java is quite nice - a Socket is an abstraction that listens for data coming in on a single port from a single web address or server and can also send out.
http://zerioh.tripod.com/ressources/sockets.html
Note that listening for data blocks program flow.
Most likely you're looking at learning Java Servlet programming. There are a lot of good resources to learn from.
However, from my experience something like Netty could serve you really well from both learning and actual application development perspectives. The site contains very reasonable example driven documentation, which takes user from non-blocking TCP/IP programming to HTTP support.
Yes , web service API (JAX-WS 2.0) is added to the Java SE 6. You can use these API to create a simple web service for the Java SE application.
See the section "Using JAX-WS 2.0 to Create a Simple Web Service" for the example .It shows how to create a standard web service end-point using JAX-WS 2.0 for the Java SE application.
Beside , this 5 minutes tutorial is very good at introducing JAX-WS .It includes the demo for implementing the web-service end-point and client and provide the eclipse project source code to download too. Please note that it misses the step to use wsgen to generate the service classes for the WebService end-point ,but it is not so difficult to figure out how to use wsgen

Categories

Resources