深剖jsp和servlet的区别

本文纠正了一些关于JSP和Servlet常见误解,如两者的关系、性能差异、生命周期管理等,并探讨了它们在现代Web开发中的角色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前几天写了一篇“浅析jsp和servlet的区别”,这几天一直想写一下他们两者的深入区别,但今天上网查了一下,这方面的资料已经很多了,就不再赘述。为了完成念想,此篇仍命名“深剖jsp和servlet的区别”,只不过把随意浏览资料中看到的错误观点纠正一下。

1、jsp和servlet没有任何关系。

错 jsp本质上就是servlet,下面是一个jsp生成的.java代码:

package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import java.io.*; import java.util.*; import java.sql.*; public final class error_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { private static java.util.List _jspx_dependants; public Object getDependants() { return _jspx_dependants; } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { JspFactory _jspxFactory = null; PageContext pageContext = null; HttpSession session = null; Throwable exception = org.apache.jasper.runtime.JspRuntimeLibrary.getThrowable(request); if (exception != null) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; JspWriter _jspx_out = null; PageContext _jspx_page_context = null; try { _jspxFactory = JspFactory.getDefaultFactory(); response.setContentType("text/html"); pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true); _jspx_page_context = pageContext; application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); out = pageContext.getOut(); _jspx_out = out; out.write("<!-- Standard iCarnegie library error page. -->/r/n"); out.write("/r/n"); out.write("<!-- Page scoped bean for a common header and footer. -->/r/n"); library.CommonTags tags = null; synchronized (_jspx_page_context) { tags = (library.CommonTags) _jspx_page_context.getAttribute("tags", PageContext.PAGE_SCOPE); if (tags == null){ tags = new library.CommonTags(); _jspx_page_context.setAttribute("tags", tags, PageContext.PAGE_SCOPE); } } out.write("/r/n"); out.write("/r/n"); // Get the exception stored in the session. exception = (Throwable) session.getAttribute("jspException"); if (exception == null) { response.sendRedirect("index.jsp"); return; } // Remove the error from the session. session.removeAttribute("jspException"); out.write("/r/n"); out.write("/r/n"); out.write("<!DOCTYPE html PUBLIC /"-//W3C//DTD html 4.0 transitional//EN/">/r/n"); out.write("/r/n"); out.write("<html>/r/n"); out.write(" <head>/r/n"); out.write(" <meta name=/"Description/" content=/r/n"); out.write(" /"iCarnegie.Library: a demonstration online database project./">/r/n"); out.write("/r/n"); out.write(" <title>iCarnegie.Library: Error Page</title>/r/n"); out.write(" <link rel=/"stylesheet/" type=/"text/css/" href="/r/n" mce_href="/r/n""); out.write(" /"styles.css/">/r/n"); out.write(" </head>/r/n"); out.write("/r/n"); out.write(" <body>/r/n"); out.write(" <!-- Display the common header using a JavaBean. --> /r/n"); out.write(" "); out.print(tags.getHeader("")); out.write("/r/n"); out.write("/r/n"); out.write(" <h3>Oops! An Unexpected Error or Exception has Occurred!</h3>/r/n"); out.write(" <h3>Exception:</h3>/r/n"); out.write(" <!-- Display the exception name. -->/r/n"); out.write(" "); out.print(exception); out.write("<br>/r/n"); out.write("/r/n"); out.write(" <br>/r/n"); out.write("/r/n"); out.write(" <!-- Display the stack trace for debugging. -->/r/n"); out.write(" <h3>Stack Trace:</h3>/r/n"); out.write(" "); StackTraceElement[] ste = exception.getStackTrace(); for (int i = 0; i < ste.length -1; i++) { out.println(ste[i] + " at<br>"); } out.println(ste[ste.length - 1] + "<br>"); out.write("<br>/r/n"); out.write(" <!-- Display the copyright information from the custom JSP tags. -->/r/n"); out.write(" "); out.print(tags.getFooter()); out.write("/r/n"); out.write(" </body> /r/n"); out.write("</html>/r/n"); } catch (Throwable t) { if (!(t instanceof SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) out.clearBuffer(); if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); } } finally { if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context); } } }

可以看出,本质上仍然是servlet

2、因为jsp先生成.java文件,所以速度比servlet慢。

不完全对 jsp的确会先生成.java文件,然后编译成.class文件,但一般当你把你的项目部署到服务器上,在你的jsp不改变的前提下,一般只会第一次访问编译,以后都不会再编译,也就是说只有第一次访问比servlet慢,以后就基本同样的速度。如果有大量的jsp,可以选择预编译。

3、servlet在一个生命周期内只会实例化一次。

不是绝对的 这要根据你web.xml中的配置文件,如果同一个servlet对应多个name,那就不止一次了,几个name就实例化几次。

4、servlet和struts的action比较类似

你猜的很对 struts中的action就是对servlet的封装,不是类似。

5、只用servlet还要写很多html代码,太麻烦,用servlet和jsp也要在jsp中写java代码,只用jsp岂不更简单。

这明显不对 的确jsp就是因为需要在servlet中写大量的html代码才推出的。但只用jsp不是不可以,但坏处是:一、丢失了java语言的最大的优点-面向对象,在jsp中的java代码都是面向过程,所有的jsp中你看不到一个类。二、给后期维护带来太大的麻烦。软件开发不仅要实现功能,更要设计有弹性的代码。理想状态下jsp中不出现java代码,全部是标签和脚本语言。当然这是理想状态下。

。。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值