标签:全局 led ase simple sch 源文件 技术 页面 utf-8
Servlet的用作:用java语言开发动态资源的技术;
Jsp的作用:用java语言(+html)开发动态的资源,其实jsp就是servlet演化而来的。
我们先来做一个实验,首先在项目的webroot下创建一个hellojsp页面,期其代码如下:
<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘hello.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<% SimpleDateFormat date=new SimpleDateFormat();
String curDate=date.format(new Date());
out.write("现在时间是:"+curDate);
%>
</body>
</html>
我们会发现在tomcat的目录的work下会新建一个文件

会发现原来的hello.jsp页面被重新翻译成了hello_jsp.java的java源文件和被编译的hello_jsp.class的字节码文件,这个时候我们打开其java源文件,发现其中类的关系如下:

这个hello_jsp类继承自HttpJspBase,我们继续根据其前面org.apache.jasper.runtime的信息去tomcat的包下面去查找相关的内容:

继承自HttpServlet的类,说明了Jsp也是一个Servlet程序,因为Servlet有生命周期,即Jsp也有生命周期。可以看看在hello_jsp。java的部分代码

这个时候我们可以类比Servlet总结一下Jsp的生命周期
(1)、翻译:将jsp文件翻译成一个java源文件 (第一次访问时完成)
(2)、编译:将java源文件编译成一个字节码文件(第一次访问时完成)
(3)、构造方法,服务器构造和调用hello_jsp类对象(第一次访问时完成)
(4)、Init方法 [_jspInit()] (第一次访问时完成)
(5)、service方法[_jspService]
(6)、destory方法。[_jspDestroy]

既然jsp就是一个servlet的程序,那么我们就可以在jsp中使用servlet的方法,同时jsp也在一定的基础上扩展了serlet的功能,即jsp的技术并不是全部
servlet技术。
1、 Jsp表达式
作用: 向浏览器输出变量的值或表达式计算的结果
2*3=<%=(2*3) %>
2、 Jsp的脚本
把脚本中java代码原封不动拷贝到_jspService方法中执行。
<% Random random=new Random();
float f=random.nextFloat();
%>
<%=f %>
3、 Jsp的声明
语法:<%! 变量或方法 %>
作用: 声明jsp的变量或方法
注意:1)变量翻译成成员变量,方法翻译成成员方法。
<%--声明成员变量(全局变量) --%>
<%! String name="gqxing";%>
4、 Jsp的注释
<%-- jsp注释 --%>
我们来看看现在hello_jsp.java文件(就是方便了我们写html代码)
String name="gqxing";
private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
private static java.util.List _jspx_dependants;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.AnnotationProcessor _jsp_annotationprocessor;
public Object getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
}
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=UTF-8");
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\n");
out.write("\r\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 ‘hello.jsp‘ starting page</title>\r\n");
out.write(" \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(" </head>\r\n");
out.write(" <body>\r\n");
out.write(" ");
SimpleDateFormat date=new SimpleDateFormat();
String curDate=date.format(new Date());
out.write("现在时间是:"+curDate);
out.write("\r\n");
out.write(" </br>\r\n");
out.write(" 2*3=");
out.print((2*3) );
out.write("\r\n");
out.write(" </br>\r\n");
out.write(" \r\n");
out.write(" ");
Random random=new Random();
float f=random.nextFloat();
out.write("\r\n");
out.write(" ");
out.print(f );
out.write("\r\n");
out.write(" ");
out.write("\r\n");
out.write(" </body>\r\n");
out.write("</html>\r\n");
标签:全局 led ase simple sch 源文件 技术 页面 utf-8
原文地址:http://www.cnblogs.com/helloworldcode/p/6043339.html