文档章节

What is the difference between JSP and servlets?

xyxo
 xyxo
发布于 2013/08/15 15:25
字数 515
阅读 68
收藏 0

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
  • It’s 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.

------------------------------------------------------------------------------

JSP (JavaServer Pages)

JSP is a Java view technology running on the server machine which allows you to write template text in (the client side languages like HTML, CSS, JavaScript and so on). JSP supports the so-called taglibs which are backed by pieces of Java code with which you can control the page flow and/or output dynamically (programmatically). A well known taglib is JSTL. JSP also supports Expression Language which can be used to access backend data (actually, the attributes which are available in page, request, session and application scopes), mostly in combination with taglibs.

When a JSP is requested for the first time or when the webapp starts up, the servlet container will compile it into a class extending HttpServlet and use it during the webapp's lifetime. You can find the generated source code in the server's work directory. In for example Tomcat, it's the /work directory. On a JSP request, the servletcontainer will execute the compiled JSP class and send the generated output (usually just HTML/CSS/JS) through the webserver over network to the client side which in turn displays it in the webbrowser.

Servlets

Servlet is an Java application programming interface (API) running on the server machine which can intercept on the requests made by the client and can generate/send a response accordingly. A well known example is theHttpServlet which provides methods to hook on HTTP requests using the popular HTTP methods such asGET and POST. You can configure HttpServlets to listen on a certain HTTP URL pattern, which is configureable in web.xml, or more recently with Java EE 6, with @WebServlet annotation.

When a Servlet is requested for the first time or when the webapp starts up, the servlet container will create an instance of it and keep it in memory during webapp's lifetime. The same instance will be reused for every incoming request whose URL matches the servlet's URL pattern. You can access the request data byHttpServletRequest and handle the response by HttpServletResponse. Both objects are available as method arguments inside any of the overridden methods of HttpServlet, such as doGet() and doPost().

© 著作权归作者所有

xyxo

xyxo

粉丝 8
博文 19
码字总数 5259
作品 0
深圳
程序员
私信 提问
Difference between applicationContext.xml and spri

Spring lets you define multiple contexts in a parent-child hierarchy. The defines the beans for the "root webapp context", i.e. the context associated with the webapp. The (or w......

Finley.Hamilton
2014/10/11
32
0
nginx,resin,tomcat中的SSI配置

页面中用法: <!--#include virtual="/include/topBar.html"--> resin: web.xml <servlet> <servlet-name>SSIServlet</servlet-name> <servlet-class> com.caucho.servlets.ssi.SSIServlet <......

anx
2013/02/27
316
0
web.xml中load-on-startup的作用

如下一段配置,熟悉DWR的再熟悉不过了: dwr-invoker org.directwebremoting.servlet.DwrServlet debug true 1 dwr-invoker /dwr/* 我们注意到它里面包含了这段配置:1,那么这个配置有什么作...

长平狐
2012/11/28
83
0
web.xml中load-on-startup的含义

<servlet-mapping> <servlet-name>proResponse</servlet-name> <url-pattern>/test</url-pattern> </servlet-mapping> <servlet> <servlet-name>proResponse</servlet-name> <servlet-class>......

2012/02/21
249
0
web.xml中load-on-startup标签的含义

在servlet的配置当中,<load-on-startup>5</load-on-startup>的含义是: 标记容器是否在启动的时候就加载这个servlet。 当值为0或者大于0时,表示容器在应用启动时就加载这个servlet; 当是一...

随智阔
2014/04/02
17
0

没有更多内容

加载失败,请刷新页面

加载更多

川普给埃尔多安和内堪尼亚胡的信

任性 https://twitter.com/netanyahu/status/1186647558401253377 https://edition.cnn.com/2019/10/16/politics/trump-erdogan-letter/index.htm...

Iridium
11分钟前
4
0
golang-mysql-原生

db.go package mainimport ("database/sql""time"_ "github.com/go-sql-driver/mysql")var (db *sql.DBdsn = "root:123456@tcp(127.0.0.1:3306)/test?charset=u......

李琼涛
39分钟前
4
0
编程作业20191021092341

1编写一个程序,把用分钟表示的时间转换成用小时和分钟表示的时 间。使用#define或const创建一个表示60的符号常量或const变量。通过while 循环让用户重复输入值,直到用户输入小于或等于0的值...

1李嘉焘1
40分钟前
6
0
Netty整合Protobuffer

现在我们都知道,rpc的三要素:IO模型,线程模型,然后就是数据交互模型,即我们说的序列化和反序列化,现在我们来看一下压缩比率最大的二进制序列化方式——Protobuffer,而且该方式是可以跨...

算法之名
45分钟前
18
0
如何用C++实现栈

栈的定义 栈(stack)又名堆栈,它是一种运算受限的线性表。限定仅在表尾进行插入和删除操作的线性表。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压...

BWH_Steven
今天
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部