标签:
在上篇Servlet登录实例中,用到了一个html进行访问,一个Servel处理和输出,Servlet输出显示的时候采用下面的拼接字符串的形式显示。在拼接的html标签中,css或js同样一个个的嵌入,这样的坏处是显示和处理没有分离。而且修改html输出的时候很麻烦。
StringBuffer sbHtml = new StringBuffer();
sbHtml.append("<html>");
sbHtml.append("<head>");
......
response.getWriter().println("sbHtml.toString()");
JSP:可以先简单理解为html标签中间嵌入和java代码。下面以HelloWorld为例:
jsp脚本:嵌入的java代码表现形式。:<%! 定义成员属性和方法的,此种方式使用很少 %>
<% 定义局部变量,在jsp_service方法中使用,使用广泛 %>
<%= 后面必须是字符串变量或者可以被转换成字符串的表达式 %>
注释:<%--… …--%>,<% //… … %>,<% /*… …*/ %>
<span style="font-size:14px;">
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<html>
<head>
<title> HelloWorld </title>
</head>
<body>
<%--这个注释只是在jsp页面中注释,生成的代码中没有 --%>
<%// 单行注释,下面定义的为成员变量 %>
<%!
int i = 10;
public void method1() {
}
%>
<% /*
多行注释
下面定义的为局部变量,放在jsp_service方法中使用
*/%>
<%
out.println("HelloWorld");
String username = "zhangsan";
%>
<input type="text" name="username" value="<% out.print(username); %>">
<input type="text" name="username" value="<%=username %>">
<input type="text" name="username" value="<%=i %>">
<input type="text" name="age" value="<%=10+1 %>">
</body>
</html></span>jsp的执行过程
1.客户端: http://localhost:8080/test_jsp/HelloWorld.jsp访问。
2.Tomcat服务器: conf文件中web.xml文件,执行JspServlet这个类
<span style="font-size:14px;">
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
........
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping></span>3.JspServlet: 继承与HttpServlet覆盖Service()方法:对于第一访问的jsp会生出一个Servlet类(HelloWorld_jsp.java和HelloWorld_jsp.class),不是第一次的访问已经生成的对应的Servlet类
4. HelloWorld_jsp: 继承与HttpJspBase(HttpJspBase类中Service()调用_jspService()方法,_jspService()方法在HttpJspBase类中没有实现,有子类实现)
5.HelloWorld_jsp中_jspService()方法来执行java代码输出html标签。
JspServlet.java和HttpJspBase.java代码Tomcat源码中有我们不做重点了解,知道jsp最后转换成了Servlet代码来执行。
对应的时序图:
生成的HelloWorld_jsp.java
<span style="font-size:14px;">
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public final class HelloWorld_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
int i = 10;
public void method1() {
}
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;
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; charset=GB18030");
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");
out.write("<html>\r\n");
out.write("\t<head>\r\n");
out.write("\t\t<title> HelloWorld </title>\r\n");
out.write("\t</head>\r\n");
out.write("\t<body>\r\n");
out.write("\t ");
out.write("\r\n");
out.write("\t \r\n");
out.write("\t\t");
// 鍗曡娉ㄩ噴锛屼笅闈㈠畾涔夌殑涓烘垚鍛樺彉閲?
out.write("\r\n");
out.write("\t\t");
out.write("\r\n");
out.write("\t\t\r\n");
out.write("\t\t\r\n");
out.write("\t\t");
/*
澶氳娉ㄩ噴
涓嬮潰瀹氫箟鐨勪负灞?閮ㄥ彉閲忥紝鏀惧湪jsp_service鏂规硶涓娇鐢?
*/
out.write("\r\n");
out.write("\t\t");
out.println("HelloWorld");
String username = "zhangsan";
out.write("\t\r\n");
out.write("\t\t\r\n");
out.write("\t\t<input type=\"text\" name=\"username\" value=\"");
out.print(username);
out.write("\">\t\t\r\n");
out.write("\t\t<input type=\"text\" name=\"username\" value=\"");
out.print(username );
out.write("\">\r\n");
out.write("\t\t<input type=\"text\" name=\"username\" value=\"");
out.print(i );
out.write("\">\r\n");
out.write("\t\t<input type=\"text\" name=\"age\" value=\"");
out.print(10+1 );
out.write("\">\r\n");
out.write("\t\t\t\t\r\n");
out.write("\t</body>\r\n");
out.write("</html>");
} 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);
}
}
}
</span>jsp指令:
在jsp的开头指令代码指明了jsp和Tomcat之间的沟通方式,指明返回客户端显示的样式等设置
<span style="font-size:14px;">
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
</span>全部:
<span style="font-size:14px;"> <%@page language=“script language”| extends=“className”| import=“importList”| buffer=“none|kb size”| --none:不缓冲,默认8k session=“true|false”| --是否可以使用session,默认true autoFlush=“true|false” --缓冲器是否自动清除,默认true isThreadSafe=“true|false”| --默认false(永远不要设成true) info=“infoText”| --任何字符 errorPage=“errorPageUrl”| isErrorPage=“true|false”| contentType=“contentTyepInfo”| pageEncoding=“gb2312” %> </span>
JSP的内置对象(9个)
out、request、response、pageContext、session、application、config、exception、Page
这几个对象不需要声明引入对应的包就可以直接使用。例如 out.println("HelloWorld");但嵌入的java代码就需要添加对应的引用如:
<span style="font-size:14px;"> <%@ page import="java.util.*"%> <%@ page import="java.text.*"%></span>
版权声明:本文不足之处在所难免,敬请各位批评指正,留下宝贵意见。
标签:
原文地址:http://blog.csdn.net/u010928364/article/details/48048383