文章目录
JSP
什么是JSP?
Java Server Pages:Java服务器端页面,也和Servlet一样,用于动态web技术!
以前是将jsp写在servlet文件中,如:
out.write("<html>\n");
out.write("<body>\n");
out.write("<h2>Hello World!</h2>\n");
out.write("</body>\n");
out.write("</html>\n");
现如今有了JSP,其最大特点:
写JSP就像写HTML
区别:
html只给用户提供静态数据
JSP页面可以嵌入Java代码,为用户提供动态数据
JSP原理
1.中tomcat的工作空间
C:\Users\dell\.IntelliJIdea2019.3\system
2.找到当前项目中的work目录,发现页面成为了Java程序
C:\Users\dell\.IntelliJIdea2019.3\system\tomcat\Tomcat_8_5_47_study_2\work\Catalina\localhost\ROOT\org\apache\jsp
3.查看源码**.java**
浏览器向服务器发送请求,不管访问什么资源,其实都在访问servlet
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent,
org.apache.jasper.runtime.JspSourceImports
发现继承和实现的类并没有HttpServlet,但是查看父类HttpJspBase(此jasper.jar)可知,此类继承了HttpServlet
public abstract class HttpJspBase extends HttpServlet implements HttpJspPage
因此,jsp本质上就是一个servlet
//初始化
public void _jspInit() {
}
//销毁
public void _jspDestroy() {
}
//service(doGet/doPost)
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
...}
在service中的过程
1.判断请求
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;
}
2.内置对象(9大内置对象)
final javax.servlet.jsp.PageContext pageContext;//页面上下文
javax.servlet.http.HttpSession session = null;//session
final javax.servlet.ServletContext application;//application
final javax.servlet.ServletConfig config;//config
javax.servlet.jsp.JspWriter out = null;//out
final java.lang.Object page = this;//page,当前页
http.HttpServletRequest request//请求
HttpServletResponse response//响应
exception//异常
3.输出页面前增加
response.setContentType("text/html");//设置响应文本
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;
4.以上对象可以直接在jsp中使用
为什么内置对象可以直接使用?
写一个hello.jsp为例
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>hello</title>
</head>
<body>
hello everyone!
</body>
</html>
写之前:
写之后:
查看hello_jsp.java变化之处在于
out.write("\r\n");
out.write("\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write(" <title>hello</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("hello everyone!\r\n");
out.write("</body>\r\n");
out.write("</html>\r\n");
在源码中可以看出,是将jsp中写的代码经转换写入了hello_jsp.java中,由之前可知,在_jsp.java中有8大内置对象,因此在jsp中写可以直接使用内置对象。
在jsp编译中,Java代码原封不动输出,html代码由out.write()输出
hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>hello</title>
</head>
<body>
hello everyone!
<%
String name = "www";
%>
<%=name%>
</body>
</html>
hello_jsp.java
out.write("\r\n");
out.write("\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write(" <title>hello</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("hello everyone!\r\n");
String name = "www";
out.write('\r');
out.write('\n');
out.print(name);
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>\r\n");
如有不对的地方欢迎指出,大家共同进步!