Servlet Is JSP Is: HTML in Java Java in HTML

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

A very basic difference:

Servlet is html in java

JSP is java in html

Other diff are:

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

Servlets run faster compared to JSP

JSP can be compiled into Java Servlets

Its easier to code in JSP than in Java Servlets

In MVC, jsp act as a view and servlet act as a controller.

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 servlet is a Java class. It's written like normal Java.

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();

An example of a JSP mixing HTML and Java code:


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!doctype html>
<html>
<body>
Hello, world! Your IP address is <%= request.getRemoteAddr(); %>!
<%
if (request.getParameter("test") != null) {
%>
<p><strong>Something happened!</strong></p>
<%
}
%>
</body>
</html>

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 output displayed in the user's web browser would be:


Counting to three:
This number is 1.
This number is 2.
This number is 3.
OK.
Specifically, a servlet is a Java class that implements the javax.servlet.Servlet interface. The
Servlet interface defines the methods that all servlets must implement. One ring to rule them all!
The web application and its constituent components are managed and executed inside the web
container, also called a servlet container, which provides additional features to the web application
such as security. When the web server gets a request for specific functionality that a particular web
component (such as a servlet or a JSP page) can provide, the web server forwards the request to
the servlet container in which the web component resides. All requests for the dynamic content
(that is, all requests to the web component that is responsible for generating the dynamic content)
are mediated by the servlet container, as shown in Figure 2-2.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy