Servlets and JSPs, just like PHP, are used to create dynamic HTML pages, but using the Java language. In the Java world, first we had Servlets. A Servlet is a Java class that implements certain interfaces. Then, for example, let's pretend that you want your servlet to output an HTML page like this:
<html>
<head></head>
<body>current time</body>
</html>
Where current time will be dynamically generated and show the current time. In order to do this, you have to write multiple out.println
statements:
out.println("<html>");
out.println("<head></head>");
out.println("<body>" + new Date() + "</body>");
out.println("</html>");
As you can see, compared to PHP, this is very verbose and not maintainable. That's why JSP was born. You can achieve the same thing in JSP using the following code:
<html>
<head></head>
<body><%= new Date() %></body>
</html>
This is much less verbose, much more readable, and much more familiar to HTML authors.
Now in order to server JSPs and Servlets, in the same way that you run PHP inside Apache + Mod PHP, you need a special kind of server called a Servlet container. A popular and open source Servlet container is Tomcat: tomcat.apache.org
Let me know if you have any other questions.