JavaWeb学习笔记5

it2023-11-10  76

8 JSP

8.1 什么是JSP

java server page:java服务器端页面,也和servlet一样,用于动态web技术 最大的特点: 写jsp就像在写html 区别: html只给用户提供静态的数据 jsp页面可以嵌入java代码,为用户提供动态数据

8.2 JSP原理

jsp是如何执行的? 代码层面没有任何问题 服务器内部工作 tomcat中有一个work目录 idea中使用tomcat会在idea的tomcat中生成一个work目录 本机电脑路径C:\Users\happytofly.IntelliJIdea2019.1\system\tomcat\Unnamed_JavaWeb_2\work\Catalina\localhost\cookiesession_war\org\apache\jsp 可以看见页面变成了java程序 浏览器向服务器发送请求,不管访问什么资源,其实都是在访问servlet jsp最终也会被转化成一个java类 jsp本质上就是一个servlet

public void _jspInit() {} public void _jspDestroy() {} public void _jspService(HttpServletRequest request, HttpServletResponse response)

1、判断请求 2、内置对象

final javax.servlet.jsp.PageContext pageContext;//页面上下文 javax.servlet.http.HttpSession session = null;//session final javax.servlet.ServletContext application;//application final javax.servlet.ServletConfig config;// javax.servlet.jsp.JspWriter out = null; final java.lang.Object page = this; HttpServletRequest request HttpServletResponse response

3、输出页面增加的代码

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;

以上的对象我们可以在jsp页面中直接使用

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("\n"); 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页面转换为java文件xx_jsp.java–》xx_jsp.class,用户真正拿到的是服务器处理完毕的class对象即servlet 在jsp页面中,只要是java代码就会原封不动的输出, 如果是html代码就会被转换成

out.write("<html>\r\n")

这样的格式输出到前端

8.3 JSP基础语法

jsp作为java技术的一种应用,它拥有一些自己扩充的语法,java中的语法都支持 jsp表达式 用于将程序的输出,输出到客户端 <%=变量名或表达式%> jsp脚本片段 <% 代码%>叫做脚本片段,其中写的内容会和jsp表达式中内容都会被编译到在Servlet的Service方法中,显然我们可以在Service方法中定义局部变量或者调用其他方法,但是不能在Service中再定义其他的方法,也就是我们可以在<%%>中定义局部变量或者调用方法,但不能定义方法。在jsp页面可以有多个脚本片段,但是多个脚本片段之间要保证结构完整。 jsp声明 <%!代码%>称作声明,其中写的内容将来会直接翻译在Servlet类中,因为我们可以在类中定义方法和属性以及全局变量,所以我们可以在<%!%>中声明方法、属性、全局变量。 jsp的注释不会在客户端显示,html的注释会显示 在web.xml中设置404错误页面

<error-page> <error-code>404</error-code> <location>/error.jsp</location> </error-page>

jsp标签 jsp:include拼接页面,本质还是三个

<jsp:include page="index1.jsp"></jsp:include> <jsp:include page="index2.jsp"></jsp:include> <jsp:include page="index3.jsp"></jsp:include>·

html链接页面标签,本质是一个

<%@include file="index1.jsp"%> <%@include file="index2.jsp"%> <%@include file="index3.jsp"%>

8.4 九大内置对象

PageContext 存东西 Request 存东西 Response Session 存东西 Application (ServletContext) 存东西 config (ServletContext) out page exception

//保存的数据只在一个页面中有效 pageContext.setAttribute("name","小明"); //保存的数据只在一次请求中有效 request.setAttribute("name","小明"); //保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器 session.setAttribute("name","小明"); //保存的数据只在服务器中有效,从打开服务器到关闭服务器 application.setAttribute("name","小明");

request:客户端向服务器发送请求,产生的数据用户看完就没用了,比如新闻,用户看完就没用 session:客户端向服务器发送请求,产生的数据,用户看完一会还有用,比如购物车 application:客户端向服务器发送请求,产生的数据一个用户用完,其他用户还能接着用,比如论坛聊天数据

最新回复(0)