ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

0313 jsp

2021-03-13 16:33:41  阅读:180  来源: 互联网

标签:Insert 0313 here jsp 页面 servlet out


在jsp中可以写java代码

三种格式

(1)<%java代码%>

(2)<%=java变量或表达式%>

(3)<%!java代码%>

代码展示

新建demo01.jsp

<body>
	<%
	int y=0;
	System.out.println(y);
	session.setAttribute("name", "张三");
	
	%>
	<%=y %>
	<%! String str="hello"; %>
	<%=str %>
	<%=session.getAttribute("name") %>
</body>

  

 

 

 jsp本身就是一个servlet,jsp在第一次被访问时会被Web容器翻译成servlet

流程:第一次访问---->helloServlet.jsp---->helloServlet_jsp.java---->编译运行,被翻译后的servlet在Tomcat的work目录中可以找到

jsp指令

(1)page指令 --- 属性最多的指令

常用属性

language:jsp脚本中可以嵌套进来的语言

pageEncoding:当前jsp文件的本身编码---内部可以包含contentType

contentType:例:response.setContentType(text/html;charset=UTF-8) 解决响应乱码

session:是否jsp在翻译时自动创建session(默认是true,自动创建session对象,如果不想创建可以写 session=“false”)

import:导入java的包

errorPage:当当前页面出错后跳转到哪个页面

isErrorPage:当前页面是一个处理错误的页面

代码展示

demo01.jsp

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" errorPage="error.jsp" session="true"%>
<!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>
	<%
	ArrayList<String> list=new ArrayList<String>();
	int y=1/0;
	System.out.println(y);
	session.setAttribute("name", "张三");
	
	%>
	<%=y %>
	<%! String str="hello"; %>
	<%=str %>
	<%=session.getAttribute("name") %>
</body>
</html>

  上述代码中故意写了一个错误1/0,因为设置了报错会调到页面error.jsp,那么在error.jsp中要声明isErrorPage

error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
<!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>
这是处理错误页面
</body>
</html>

  访问demo01.jsp

 

 

 那我们经常见到404和500错误页面,我们也可以设置一个跳转到自己的错误信息页面,就在xml文件中加标签

例:

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

(2)include指令

 

 

 就上图这样的结构,建demo02.jsp,通过引入头部jsp和尾部jsp引入

header.jsp

<%@ 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>
头部
</body>
</html>

  

footer.jsp

<%@ 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>
尾部
</body>
</html>

  demo02.jsp

<%@ 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>
<%@include file="/header.jsp" %>
正文
<%@include file="/footer.jsp" %>
</body>
</html>

  

 

 

 (3)taglib指令(后续将学习)

jsp内置/隐式对象(9个)jsp被翻译成servlet之后,service方法中有9个对象定义并初始化完毕,我们在jsp脚本中可以直接使用这9个对象

(1)out          javax.servlet.jsp.JspWriter 用于页面输出

(2)request    javax.servlet.http.HttpServletRequest 得到用户请求信息

(3)response  javax.servlet.http.HttpServletResponse 服务器向客户端的回应信息

(4)config      javax.servlet.ServletConfig 服务器配置,可以取得初始化参数

(5)session    javax.servlet.http.HttpSession 用来保存用户的信息

(6)application javax.servlet.ServletContext 所有用户的共享信息

(7)page       java.lang.Object 指当前页面转换后的Servlet类的实例(在普通类中的this)

(8)pageContext javax.servlet.jsp.PageContext JSP的页面容器

(9)exception java.lang.Throwable 表示JSP页面所发生的异常,在错误页中才起作用

正常jsp有前八个内置对象,只有像上述中的error.jsp中会有第九个异常对象

重点分析 out对象和 pageContext对象

1、out对象

out对象的类型是JspWriter

out作用就是向客户端输出内容----out.write()

out.jsp

<%@ 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>
aaaaaaaa
<%="bbbbbbbb" %>
<%
response.getWriter().write("cccccccc");
out.write("dddddddd");
%>
</body>
</html>

  

 

 

 输出的内容顺序是跟我们预想的顺序不一样

是因为 aaaaa,bbbbb,ddddd 都进到了out缓冲区。而ccccc进到了response缓冲区,进到out缓冲区的内容要再进到response缓冲区然后再输出。

图解

 

 

 如果不想这样,那可以将out缓冲区关闭 out缓冲区默认8kb 可以设置成0 代表关闭out缓冲区 内容直接写到respons缓冲 器

代码展示

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" buffer="0kb"%>
<!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>
aaaaaaaa
<%="bbbbbbbb" %>
<%
response.getWriter().write("cccccccc");
out.write("dddddddd");
%>
</body>
</html>

  

 

 

2、pageContext对象(域对象)

作用域只在本jsp中,所以不能实现数据传递,不能在这个jsp中存入数据,在另一个jsp中取出

但是 该对象可以往别的域中存储数据

pageContext可以向指定的其他域中存取数据

setAttribute(String name,Object obj,int scope)

getAttribute(String name,int scope)

removeAttrbute(String name,int scope)

findAttribute(String name)

---依次从pageContext域,request域,session域,application域中获   取属性,在某个域中获取后将不在向后寻找

代码展示

<%@ 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>
<%
	//reguest域存值
	pageContext.setAttribute("name", "李四", pageContext.REQUEST_SCOPE);
	//session域中存值
	pageContext.setAttribute("name", "王五", pageContext.SESSION_SCOPE);
	//aplisation
	pageContext.setAttribute("name", "赵六", pageContext.APPLICATION_SCOPE);
%>
<%=pageContext.getAttribute("name",pageContext.REQUEST_SCOPE) %>
<%=pageContext.getAttribute("name",pageContext.SESSION_SCOPE) %>
<%=pageContext.getAttribute("name",pageContext.APPLICATION_SCOPE) %>
<%=pageContext.findAttribute("name") %>
</body>
</html>

  

 

标签:Insert,0313,here,jsp,页面,servlet,out
来源: https://www.cnblogs.com/-gongxue/p/14529329.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有