Java中的JSP和Servlet之间的区别– JSP与Servlet
In this tutorial I will tell you about difference between JSP and Servlet in Java.
在本教程中,我将向您介绍Java中的JSP和Servlet之间的区别。
JSP and Servlet are two technologies in Java which are used to create dynamic web pages. Still there are various differences between them which I am sharing below.
JSP和Servlet是Java中用于创建动态网页的两项技术。 两者之间仍然存在各种差异,我在下面分享。
Java中的JSP和Servlet之间的区别– JSP与Servlet (Difference between JSP and Servlet in Java – JSP vs Servlet)
S.No. | JSP | Servlet |
1. | JSP is webpage scripting language that is used to create dynamic web pages in Java. | Servlets are Java programs that are already complied and are used to create dynamic web pages in Java. |
2. | In JSP, Java code is written inside HTML code. | In Servlet, HTML code is written inside Java code. |
3. | In MVC architecture, JSP act as a view. | In MVC architecture, Servlet act as a controller. |
4. | In JSP we can create custom tags which can directly call Java beans. | Servlet do not provide facility to create custom tags. |
5. | It is easier to write code in JSP. | It is not easier to write code in Servlet. |
6. | JSP run slower as compared to Servlet because JSP is first converted into Servlet and then executed. | Servlet run faster than JSP. |
7. | JSP is preferred when less data processing is required. | Servlet is preferred when more data processing and manipulation is required. |
序号 | JSP | Servlet |
1。 | JSP是网页脚本语言,用于在Java中创建动态网页。 | Servlet是已经编译的Java程序,用于在Java中创建动态网页。 |
2。 | 在JSP中,Java代码是用HTML代码编写的。 | 在Servlet中,HTML代码是用Java代码编写的。 |
3。 | 在MVC体系结构中,JSP充当视图。 | 在MVC架构中,Servlet充当控制器。 |
4。 | 在JSP中,我们可以创建可以直接调用Java bean的自定义标签。 | Servlet不提供创建自定义标签的功能。 |
5, | 用JSP编写代码更容易。 | 在Servlet中编写代码并不容易。 |
6。 | 与JSP相比,JSP的运行速度较慢,因为JSP首先转换为Servlet,然后执行。 | Servlet的运行速度比JSP快。 |
7 | 当需要较少的数据处理时,首选JSP。 | 当需要更多数据处理和操纵时,首选Servlet。 |
Below I have shared one example. It will help you to clearly understand difference between JSP and Servlet.
下面我分享了一个例子。 它将帮助您清楚地了解JSP和Servlet之间的区别。
JSP Example
JSP示例
1
2
3
4
5
6
7
8
|
<html>
<head><title>Hello World</title></head>
<body>
<%
out.println("Hello World");
%>
</body>
</html>
|
1
2
3
4
5
6
7
8
|
<html>
<head> <title> Hello World < / title > < / head >
<body>
<%
out . println ( "Hello World" ) ;
%>
< / body >
< / html >
|
Servlet Example
Servlet示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class DemoServlet extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println("<html><body>");
pw.println("Hello World");
pw.println("</body></html>");
pw.close();
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import javax . servlet . http . * ;
import javax . servlet . * ;
import java . io . * ;
public class DemoServlet extends HttpServlet {
public void doGet ( HttpServletRequest req , HttpServletResponse res ) throws ServletException , IOException
{
res . setContentType ( "text/html" ) ;
PrintWriter pw = res . getWriter ( ) ;
pw . println ( "<html><body>" ) ;
pw . println ( "Hello World" ) ;
pw . println ( "</body></html>" ) ;
pw . close ( ) ;
}
}
|
You can comment below if you have any doubts regarding above tutorial for difference between JSP and Servlet in Java.
如果您对以上教程对Java中的JSP和Servlet之间的区别有任何疑问,可以在下面发表评论。
翻译自: https://www.thecrazyprogrammer.com/2016/01/difference-between-jsp-and-servlet.html