九大内置对象:
常用内置对象:蓝色背景
jsp的java代码解析内置对象
jspServlet.java路径
(如何找到该java文件:
1、进入tomcat的项目存放地apache-tomcat…(我的是在原项目目录下的.metadata中)
2、在该项目中搜索work,一路点进去work\Catalina\localhost\mvc_two\org\apache\jsp
3、在jsp文件夹下的就是jsp.java和jsp.servlet)
这里提取了核心的代码,也就是_jspService()方法
方法内部:
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
只实现了pageContext(依赖工厂模式)
其他的内置对象,都是靠pageContext来获取的(其封存了所有内置对象)
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
final java.lang.String _jspx_method = request.getMethod();
if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD");
return;
}
final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.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("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
out.write("<title>login</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("<form action=\"loginServlet\" method=\"post\">\r\n");
out.write("<input type=\"text\" name=\"name\"/>\r\n");
out.write("<input type=\"text\" name=\"password\"/>\r\n");
out.write("<input type=\"submit\" value=\"登录\"/>\r\n");
out.write("</form>\r\n");
out.write("</body>\r\n");
out.write("</html>");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try {
if (response.isCommitted()) {
out.flush();
} else {
out.clearBuffer();
}
} catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
解析:
pageContext:一般情况,运行一次pageContext会被销毁,因为其在_jspService()中有效(局部变量),所以pageContext是当前页面有效
session省略不讲。
jsp中设置生效
<%@page session="true" %>
request省略
application:从demo中可得 application = pageContext.getServletContext();说明application使得本质是ServletContext对象,作用范围为整个项目内
response:作为形参(响应头),可以重定向
out:带有缓冲区的输出,可以调用方法在jsp实现输出,但用的很少,毕竟jsp可以转译为java文件,要输出直接写html更方便
page: 从demo中可得 final java.lang.Object page = this; 说明page指代的为当前页面
config省略
exception:储存了异常信息,但要设置属性开启了才有效
在jsp的首部加上
<%@page errorPage="error.jsp" %>
<%@page isErrorPage="true" %>
jsp.java显示:
if (exception != null) {
response.setStatus(javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}