什么是Jsp呢?
JSP全名为Java Server Pages,其根本是一个简化的Servlet设计,它是在传统的网页HTML中插入Java程序段,从而形成JSP文件,后缀名为(*.jsp)。
Jsp只对网页中动态产生的内容采用java代码来编写,而对固定不变的静态内容采用普通的静态页面HTML方式编写
我们先来看一个最简单的JSP小例子吧:
这个例子我们不需要 在web.xml中进行配置,如下截图是在WebContent下创建一个NewFile.jsp文件
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Date date = new Date();
System.out.println(date);
%>
</body>
</html>
直接跑在server上,就相当于直接去访问一个静态的html页面即可
浏览器的地址为:http://localhost:8080/day01/NewFile.jsp
输入结果如下:
那么jsp在项目发布的什么目录呢,看看下图:
原来被编译为一个java文件NewFile_jsp.java
楼上的截图Init、Destroy、Service跟Servlet的生命周期蛮像,是不是它跟Servlet有关系呢?
我们关联了Tomcat的源码去看看继承类里面做了什么操作吧?
原来继承的是HttpServlet!!!
到此得到一个结论:Jsp其实就是一个Servlet
WEB容器接收到.jsp为扩展名的URL访问时候,比如上面的例子是
http://localhost:8080/day01/NewFile.jsp,它将把该访问交给JSP引擎去处理,然后该jsp文件翻译为一个Servlet源程序,继而将Servlet源程序,编译为Servlet的class类,然后在用web容器,调用普通的servlet一样,来进行装载执行一系列操作。
我们继续回看NewFile_jsp.java了解原理
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\n");
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=ISO-8859-1\">\r\n");
out.write("<title>Insert title here</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("\r\n");
Date date = new Date();
System.out.println(date);
out.write("\r\n");
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>");
} 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需要了解的几大对象
HttpServletRequest request,;
HttpServletResponse response
PageContext pageContext = null; 页面的上下文,可以获取8个内置对象
HttpSession session = null; 浏览器和服务器的一次会话
ServletContext application = null; 当前web应用
ServletConfig config = null;servlet配置信息
JspWriter out = null;输出到浏览器中
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
exception
我们就通过一个小例子,来简单了解下这几个内置对象吧:
首先我们需要在web.xml中,进行配置一些信息,这样可以在jsp中通过内置对象进行获取
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<context-param>
<param-name>user</param-name>
<param-value>wyf</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>123456</param-value>
</context-param>
<servlet>
<servlet-name>hellojsp</servlet-name>
<!-- 这里不是servlet-class -->
<jsp-file>/NewFile.jsp</jsp-file>
<init-param>
<param-name>test</param-name>
<param-value>testValue</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hellojsp</servlet-name>
<url-pattern>/hellojsp</url-pattern>
</servlet-mapping>
</web-app>
NewFile.jsp
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Date date = new Date();
System.out.println(date);
%>
<%
//request
String name = request.getParameter("name");
System.out.println("name:" + name);
//response
System.out.println(response instanceof HttpServletResponse);
//pageContext
ServletRequest req = pageContext.getRequest();
System.out.println("req:" + req);
//session
System.out.println(session.getId());
//application--ServletContext
System.out.println(application.getInitParameter("password"));
System.out.println(application.getInitParameter("user"));
//config
System.out.println(config.getInitParameter("test"));
//jspWriter
out.println("out:" + config.getInitParameter("test"));
//huan hang
out.println("<br>");
%>
<br>
<%
out.println("out:" + config.getInitParameter("test"));
%>
</body>
</html>
输出如下:
域对象的属性操作
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
pageContext.setAttribute("pageContextAttr", "pageContextValue");
request.setAttribute("requestAttr", "requestValue");
session.setAttribute("sessionAttr", "sessionValue");
application.setAttribute("applicationAttr", "applicationValue");
%>
pageContextAttr:<%=pageContext.getAttribute("pageContextAttr")%>
<br> requestAttr:<%=request.getAttribute("requestAttr")%>
<br> sessionAttr:<%=session.getAttribute("sessionAttr")%>
<br> applicationAttr:<%=application.getAttribute("applicationAttr")%>
</body>
</html>
http://localhost:8080/day01/NewFile.jsp
pageContextAttr:pageContextValue
requestAttr:requestValue
sessionAttr:sessionValue
applicationAttr:applicationValue
pageContext
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//使用pageContext向request域存数据
//request.setAttribute("name", "zhangsan");
//pageContext.setAttribute("name", "sunba");
//pageContext.setAttribute("name", "lisi", PageContext.REQUEST_SCOPE);
//pageContext.setAttribute("name", "wangwu", PageContext.SESSION_SCOPE);
//pageContext.setAttribute("name", "tianqi", PageContext.APPLICATION_SCOPE);
%>
<%=request.getAttribute("name") %>
<%=pageContext.getAttribute("name", PageContext.REQUEST_SCOPE)%>
<!-- findAttribute会从小到大搜索域的范围中的name -->
<!-- page域<request域<session域<application域 -->
<%=pageContext.findAttribute("name") %>
<%
pageContext.getRequest();
pageContext.getOut();
//method(pageContext);
%>
</body>
</html>