JSP学习——原理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 |
package
org.apache.jsp; import
javax.servlet.*; import
javax.servlet.http.*; import
javax.servlet.jsp.*; import
java.util.*; public
final
class
index_jsp extends
org.apache.jasper.runtime.HttpJspBase implements
org.apache.jasper.runtime.JspSourceDependent { private
static
final
JspFactory _jspxFactory = JspFactory.getDefaultFactory(); private
static
java.util.List<String> _jspx_dependants; private
javax.el.ExpressionFactory _el_expressionfactory; private
org.apache.tomcat.InstanceManager _jsp_instancemanager; public
java.util.List<String> getDependants() { return
_jspx_dependants; } public
void
_jspInit() { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); } public
void
_jspDestroy() { } public
void
_jspService(HttpServletRequest request, HttpServletResponse response) throws
java.io.IOException, ServletException { PageContext pageContext = null ; HttpSession session = null ; ServletContext application = null ; ServletConfig config = null ; JspWriter out = null ; Object page = this ; JspWriter _jspx_out = null ; PageContext _jspx_page_context = null ; try
{ response.setContentType( "text/html;charset=ISO-8859-1" ); 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( ‘\r‘ ); out.write( ‘\n‘ ); String path = request.getContextPath(); String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ; out.write( "\r\n" ); out.write( "\r\n" ); out.write( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n" ); out.write( "<html>\r\n" ); out.write( " <head>\r\n" ); out.write( " <base href=\"" ); out.print(basePath); out.write( "\">\r\n" ); out.write( " \r\n" ); out.write( " <title>My JSP ‘index.jsp‘ starting page</title>\r\n" ); out.write( "\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n" ); out.write( "\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n" ); out.write( "\t<meta http-equiv=\"expires\" content=\"0\"> \r\n" ); out.write( "\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n" ); out.write( "\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n" ); out.write( "\t<!--\r\n" ); out.write( "\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n" ); out.write( "\t-->\r\n" ); out.write( " </head>\r\n" ); out.write( " \r\n" ); out.write( " <body>\r\n" ); out.write( " \r\n" ); out.write( " " ); Date date = new
Date(); out.write(date.toLocaleString()); 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 ) try
{ out.clearBuffer(); } catch
(java.io.IOException e) {} if
(_jspx_page_context != null ) _jspx_page_context.handlePageException(t); } } finally
{ _jspxFactory.releasePageContext(_jspx_page_context); } } } |
不管是JSP还是Servlet,虽然都可以用于开发动态web资源。但由于这2门技术各自的特点,在长期的软件实践中,人们逐渐把servlet作为web应用中的控制器组件来使用,而把JSP技术作为数据显示模板来使用。
其原因为,程序的数据通常要美化后再输出:
让jsp既用java代码产生动态数据,又做美化会导致页面难以维护。
让servlet既产生数据,又在里面嵌套html代码美化数据,同样也会导致程序可读性差,难以维护。
因此最好的办法就是根据这两门技术的特点,让它们各自负责各的,servlet只负责响应请求产生数据,并把数据通过转发技术带给jsp,数据的显示jsp来做。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。