Servlet Is JSP Is: HTML in Java Java in HTML
Servlet Is JSP Is: HTML in Java Java in HTML
Servlet Is JSP Is: HTML in Java Java in HTML
JSP is a webpage scripting language that can generate dynamic content while Servlets are Java programs
that are already compiled which also creates dynamic web content
JSP are generally preferred when there is not much processing of data required. But servlets are best for
use when there is more processing and manipulation involved.
The advantage of JSP programming over servlets is that we can build custom tags which can directly
call Java beans. There is no such facility in servlets.
We can achieve functionality of JSP at client side by running JavaScript at client side. There are no such
methods for servlets.
A servlet is like any other java class. You put HTML into print statements like you use System.out or how
javascript uses document.write.
A JSP technically gets converted to a servlet but it looks more like PHP files where you embed the java into
HTML.
in short: servlets should be used if you have more java than HTML and JSP should be used if you have more
HTML than java
It is very common to combine servlets and JSP so that the initial request gets sent to a servlet which does some
java work and then forwards it to a JSP which actually makes the HTML output.
A Java Server Page (JSP) is a file that is often used in place of a servlet because it makes it easier to output
data. JSP is similar to PHP since you can mix output (like HTML) with Java without using lots out.println
stuff like is necessary with a servlet. A JSP file is actually a servlet; when you add it to your server, it gets
transformed into a .java file without you knowing about it. And after the transformation, it gets compiled into
.class file along with other .java files whe necessary
An example of a simple servlet:
// from http://www.caucho.com/resin-3.0/servlet/tutorial/helloworld/index.xtp
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class HelloServlet extends HttpServlet {
public void doGet (HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
PrintWriter out = res.getWriter();
out.println("Hello, world!");
out.close();
Java Servlet technology and JavaServer Pages (JSP pages) are server-side technologies
JavaServer Pages (JSP) is a technology that helps software developers create dynamically generated web
pages based on HTML, XML, or other document types. Released in 1999 by Sun Microsystems,[1] JSP is similar
to PHP and ASP, but it uses the Java programming language.
SP pages use several delimiters for scripting functions. The most basic is <% ... %>, which
encloses a JSP scriptlet. A scriptlet is a fragment of Java code that is run when the user requests
the page. Other common delimiters include <%= ... %> for expressions, where the scriptlet and
delimiters are replaced with the result of evaluating the expression, and directives, denoted with
<%@ ... %>
<p>Counting to three:</p>
<% for (int i=1; i<4; i++) { %>
<p>This number is <%= i %>.</p>
<% } %>
<p>OK.</p>
The Java EE Servlet and JSP specifications describe the service contract that a servlet container
must provide and specify how a servlet should use those services. In terms of implementation,
a servlet is a Java class that acts as a dynamic web resource.