Jsp基础语法

一、JSP概述

  JavaServer Pages(JSP) 是一种技术,能够开发支持动态内容的网页,可以帮助开发人员在 HTML 页面中利用特殊的 JSP 标签插入 Java 代码,其中大部分标签是以 <% 开始,以 %> 结束的。

二、JSP体系结构

  Web 服务器需要一个 JSP 引擎,即处理 JSP 页面的容器。JSP 容器负责为 JSP 页面拦截请求。本教程使用了 Apache,Apache 已经内置了 JSP 容器来支持 JSP 页面开发。

  1. web 服务器如何使用 JSP 创建 web 页面?

  • 作为正常的页面,你的浏览器发送一个 HTTP 请求到 web 服务器。

  • web 服务器承认一个 JSP 页面的 HTTP 请求,并将其转发给一个 JSP 引擎。这是通过使用 URL 或 JSP 页面实现的,该 JSP 页面是以.jsp结尾而不是以 .Html 结尾的。

  • JSP 引擎从磁盘加载 JSP 页面并将其转换为一个 servlet 的内容。这种转换是非常简单的,所有模板文本转换为 println()语句,所有 JSP 元素转换为 Java 代码实现页面的相应的动态行为。

  • JSP 引擎编译 servlet 到一个可执行的类中,并将原始请求转发给一个 servlet 引擎。

  • 调用 servlet 引擎的 web 服务器的一部分加载 Servlet 类并执行它。执行期间,Servlet 产生一个 HTML 格式的输出,servlet 引擎将该输出传递到 HTTP 响应内的 web 服务器中。

  • web 服务器将 HTTP 响应以静态 HTML 内容的形式转发到你的浏览器中。

  • 最后 web 浏览器处理 HTTP 响应中的动态生成的 HTML 页面,就好像它是一个静态页面。  

三、JSP生命周期

  JSP 生命周期可以被定义为从创建到销毁的整个过程,这类似于一个 servlet 的生命周期与一个额外的步骤,该步骤将一个 JSP 编译成 servlet。

  • 编译

  • 初始化

  • 执行

  • 清理

四、JSP声明

<%! int i = 0; %> 

  

五、JSP表达式

<%= expression %>

  

六、JSP注释

<%-- This is JSP comment --%>  

七、JSP三大指令

  1. 页面指令page

    页面指令用于为属于当前 JSP 页面的容器提供指示。你可以在 JSP 页面的任何地方编写页面指令代码。按照惯例,通常在 JSP 页面的顶部编写页面指令代码。

    格式:<%@ page ... %>

 1 <%@ page import="com.eagle.test.*" %> 

  2. 包含指令include

    包含指令用于在转换阶段包含一个文件。这个指令告诉容器在转换阶段将其他外部文件的内容与当前 JSP 合并。你可以在你的 JSP 页面中的任何位置编写 include 指令。

               格式:<%@ include file="relative url" >

       3. taglib指令

    JSP API 允许用户定义自定义的 JSP 标签,看起来像 HTML 或 XML 标签,且标签库是一组用户定义的标签,能够实现自定义的行为。

               格式:<%@ taglib uri="uri" prefix="prefixOfTag" >

八、JSP十大操作

  

jsp:include 当请求页面时,包含一个文件
jsp:useBean 发现或实例化一个 JavaBean
jsp:setProperty JavaBean 的属性集
jsp:getProperty 将 JavaBean 的属性嵌入到输出中
jsp:forward 将请求转发给一个新页面
jsp:plugin 生成浏览器-特定代码,为 Java 插件创建 OBJECT 或 EMBED 标签
jsp:element 动态的定义 XML 元素
jsp:attribute 定义了动态定义的 XML 元素的属性
jsp:body 定义了动态定义 XML 元素的 body
jsp:text 用于在 JSP 页面和文档中编写模板

 

九、JSP九大隐式对象

  

request 这是与请求关联的 HttpServletRequest 对象。
response 这是与客户端响应关联的 HttpServletResponse 对象。
out 这是用于向客户端发送输出的 PrintWriter 对象。
session 这是与请求关联的 HttpSession 对象。
application 这是与应用程序上下文关联的 ServletContext 对象。
config 这是与页面关联的 servletConfig 对象。
pageContext 这个封装特使用特定服务器的特性,如更高的性能 jspwriter。
page 这是 this 的一个简单的同义词,是用来调用由转换的 servlet 类定义的方法。
Exception Exception 对象允许指定的 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>Jsp四大作用域之request</title>
</head>
<body>
	<%
		request.setAttribute("name", "ying");
		request.setAttribute("age", 12);
	%>
	<%-- 服务器内部跳转  url不变,和客户端跳转(重定向)有区别  --%>
	<jsp:forward page="requestTarget.jsp"></jsp:forward>
</body>
</html>


<%@page import="java.util.Enumeration"%>
<%@ 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>request接收页面</title>
</head>
<body>
	<%
	
		String name = (String)request.getAttribute("name");
		int age = (Integer)request.getAttribute("age");
		// 获取头部信息
		Enumeration enu = request.getHeaderNames();
		while(enu.hasMoreElements()){
			String headerName = (String)enu.nextElement();
			String headerVlaue = request.getHeader(headerName);
	%>
		<h4><%=headerName %> <%=headerVlaue %></h4>
	<%	
		}
	
	%>
	<front>姓名:<%=name %></front>
	<front>年龄:<%=age %></front>
</body>
</html>

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!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>response自动刷新应用</title>
</head>
<body>
	<%
		response.setHeader("refresh", "1");
		Date myDate = new Date();
	%>
	当前时间:<%=myDate.toLocaleString() %>
</body>
</html>


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!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>response重定向,客户端跳转</title>
</head>
<body>
	<%
		response.sendRedirect("index.html");
		
	%>
	
</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>操作Cookie</title>
<script type="text/javascript">
	function resetValue(){
		document.getElementById("userName").value = "";
		document.getElementById("pwd").value = "";
	}
</script>

<%
	String userName = null;
	String pwd = null;
	// 获取cookie 
	Cookie[] cookies = request.getCookies();
	for(int i=0;cookies!=null && i<cookies.length;i++){
		if(cookies[i].getName().equals("userNameAndPwd")){
			// 截取字符串
			userName = cookies[i].getValue().split("-")[0];
			pwd = cookies[i].getValue().split("-")[1];
		}
	}
	
	if(userName==null){
		userName="";
	}
	if(pwd==null){
		pwd="";
	}
%>
</head>
<body>
<form action="userLogin.jsp" method="post">
	<table>
		<tr>
			<td>用户名:</td>
			<td><input type="text" id="userName" name="userName" value="<%=userName%>"/></td>
		</tr>
		<tr>
			<td>密码:</td>
			<td><input type="password" id="pwd" name="pwd" value="<%=pwd %>" /></td>
		</tr>
		<tr>
			<td>记住密码:</td>
			<td><input type="checkbox" id="remember" name="remember" value="remember-me"/></td>
		</tr>
		<tr>
			<td><input type="submit" value="登录"/></td>
			<td><input type="button" value="重置" onclick="resetValue()"/></td>
		</tr>
	</table>
</form>
</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>登录提交处理</title>
</head>
<body>
	<%
	String userName=request.getParameter("userName");  // 获取用户名
	String pwd=request.getParameter("pwd");  // 获取密码
	String remember=request.getParameter("remember");  // 获取记住密码
	
	if("remember-me".equals(remember)){
		// 设置Cookie start
		Cookie userNameAndPwd = new Cookie("userNameAndPwd",userName+"-"+pwd);
		userNameAndPwd.setMaxAge(1*60*60*24*7); // cookie记录一个星期
		response.addCookie(userNameAndPwd);
		// 设置Cookie end
		System.out.println("Cookie设置成功");
	}
	response.sendRedirect("response03.jsp");
		
	%>
</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>Jsp四大作用域之application</title>
</head>
<body>
	<%
	application.setAttribute("name", "ying");
	application.setAttribute("age", 12);
	%>
	
	<h4>application值设置完毕</h4>
</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>application接收页面</title>
</head>
<body>
	<%
	// 取值
	String name=(String)application.getAttribute("name");
	int age=(Integer)application.getAttribute("age");
	%>
	<font>姓名:<%=name %></font>
	<font>年龄:<%=age %></font>
</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>Jsp内置对象--config</title>
</head>
<body>
<%
	String jdbcName=config.getInitParameter("jdbcName");
	String dbUrl=config.getInitParameter("dbUrl");
%>
<h1>驱动名称:<%=jdbcName %></h1>
<h1>连接地址:<%=dbUrl %></h1>
</body>
</html>

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%-- errorPage 指定抛异常页面 --%>
<%@ page errorPage="error.jsp"%>
<!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>jsp内置变量之exception</title>
</head>
<body>
	<%
		int a=1;
		int b=0;
		out.println(a/b);
	%>
</body>
</html>

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%-- isErrorPage 抛异常 --%>
<%@ page 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>
<%
	if(exception!=null){
		out.println("程序错误信息:");
		out.println(exception.getMessage());
	}
%>
</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>out:向客户端输出数据和管理应用服务器上的输出缓冲区</title>
</head>
<body>
	<%
		out.println("<h1>");
		out.println("Hello Jsp Servlet");
		out.println("</h1>");
	%>
	<%
		int totalbuffer=out.getBufferSize();  // 获取总共缓冲区的大小
		int available=out.getRemaining(); // 获取未使用的缓冲区的大小
		int user=totalbuffer-available;  // 获取使用的缓冲区大小
		out.println("总共缓冲区的大小:"+totalbuffer);
		out.println("未使用的缓冲区的大小:"+available);
		out.println("使用的缓冲区大小:"+user);
	%>
</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>Jsp九大内置对象之--pageContext</title>
</head>
<body>
<%
	pageContext.setAttribute("name0", "pageInfo");
	
	
	out.println("使用pageContext<br/>");
	out.println("page中的属性值:"+pageContext.getAttribute("name0")+"<br/>");

%>
</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>Jsp四大作用域之page</title>
</head>
<body>
	<%
		pageContext.setAttribute("name", "ying");
		pageContext.setAttribute("age", 12);
	%>
	<%
		String name = (String)pageContext.getAttribute("name");
		int age = (Integer)pageContext.getAttribute("age");
	%>
	<front>姓名:<%=name %></front>
	<front>年龄:<%=age %></front>
</body>
</html>

  

十、JSTL标签库

  1. 核心标签库

    

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>JSTL 核心标签库</title>
</head>
<body>
	<!-- c:if 用来条件判断; -->
	<jsp:useBean id="people" class="com.learn.model.People" scope="page"></jsp:useBean>
	<!--  -->
	<c:set property="id" target="${people }" value="007"></c:set>
	<c:set property="name" target="${people }" value="王二小"></c:set>
	<c:set property="age" target="${people }" value="16"></c:set>
	
	<c:if test="${people.name=='王二小' }" var="r" scope="page">
		<h2>是王二小</h2>
	</c:if>
	<c:if test="${!r}">
        <h2>不是王二小</h2>
    </c:if>
      
	<c:if test="${people.age<18 }">
		<h2>是未成年</h2>
	</c:if>
</body>
</html>



<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.*"%>
<%@ page import="com.learn.model.Peoples"%>
<!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>JSTL 核心标签库</title>
</head>
<body>
	<!-- c:forEach 用来遍历数组或者集合; -->
	<%
		String dogs[]={"小黑","小黄","小白","小小"};
	    pageContext.setAttribute("dogs",dogs);
	%>
	<c:forEach var="dog" items="${dogs }">
		${dog }  <!-- 小黑 小黄 小白 小小 -->
	</c:forEach>
	<hr/>
	<c:forEach var="dog" items="${dogs }" step="2">
		${dog } <!-- 小黑 小白 -->
	</c:forEach>
	<hr/>
	<c:forEach var="dog" items="${dogs }" begin="1" end="2">
		${dog } <!-- 小黄 小白 -->
	</c:forEach>
	<hr/>
	
	<%
		List<Peoples> pList=new ArrayList<Peoples>();
	    pList.add(new Peoples(1,"张三",10));
	    pList.add(new Peoples(2,"李四",20));
	    pList.add(new Peoples(3,"王五",30));
	    pageContext.setAttribute("pList",pList);
	%>
	<table>
		<tr>
			<th>编号</th>
			<th>姓名</th>
			<th>年龄</th>
		</tr>
		<c:forEach var="p" items="${pList }">
			<tr>
				<td>${p.id }</td>
				<td>${p.name }</td>
				<td>${p.age }</td>
			</tr>
		</c:forEach>
	</table>
</body>
</html>



<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.*"%>
<%@ page import="com.learn.model.People"%>
<!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>JSTL 核心标签库</title>
</head>
<body>
	<!-- c:fortokens 分隔输出; -->
	<%
		String str1="www.java1234.com";
		String str2="张三,李四,王五";
	    pageContext.setAttribute("str1",str1);
	    pageContext.setAttribute("str2",str2);
	%>
	<c:forTokens items="${str1 }" delims="." var="s1">
		${s1 }  <!-- www java1234 com -->
	</c:forTokens>
	<hr/>
	<c:forTokens items="${str2 }" delims="," var="s2">
		${s2 } <!-- 张三 李四 王五 -->
	</c:forTokens>
</body>
</html>

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>JSTL 核心标签库</title>
</head>
<body>
	<!-- c:choose、c:when、c:otherwise 用来多条件判断; -->
	<jsp:useBean id="people" class="com.learn.model.People" scope="page"></jsp:useBean>
	
	<c:set property="id" target="${people }" value="007"></c:set>
	<c:set property="name" target="${people }" value="王二小"></c:set>
	<c:set property="age" target="${people }" value="19"></c:set>
	
	<c:choose>
		<c:when test="${people.age<18 }">
			<h2>小于18</h2>
		</c:when>
		<c:when test="${people.age==18 }">
			<h2>等于18</h2>
		</c:when>
		<c:otherwise>
			<h2>大于18</h2>
		</c:otherwise>
	</c:choose>
</body>
</html>


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>JSTL 核心标签库</title>
</head>
<body>
	<!-- c:catch 用来处理程序中产生的异常; -->
	<c:catch var="errMsg">
	<%
		int a=1/0;
	%>
	</c:catch>
	<h2>异常信息:${errMsg }</h2>
	
</body>
</html>



<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>JSTL 核心标签库</title>
</head>
<body>
	<!-- c:import 导入页面; -->
	<c:import url="c_if.jsp"></c:import>
	
</body>
</html>


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>JSTL 核心标签库</title>
</head>
<body>
	<!-- 
		以后开发jsp的原则: 尽量在jsp页面中少写甚至不写java代码。
		使用EL表达式替换掉jsp表达式,使用标签(jsp动作标签、jsp自定义标签、jsp标准标签库)替换掉jsp脚本
		JSTL简介:
			JSTL(Java Standard Tag Library) –Java标准标签库。 
			SUN公司制定的一套标准标签库的规范。 
			JSTL标签库,是由一些Java类组成的。

		JSTL组成:
			JSTL –Core 核心标签库。  【重点--必须熟练掌握】 若使用核心标签库,必须
					1.先在lib/下导入jstl.jar和standard.jar
					2.在页面中引入
			JSTL – I18N - 国际化标签库。Internationalization- I18N 【可以熟悉下】
			JSTL – SQL – 数据库操作标签(有悖于MVC设计模式),现在都不用这个。 
			JSTL - Functions – 函数库。 
			JSTL - XML ,对XML的操作(同SQL标签-有悖于MVC设计模式),现在都不用这个。
	 -->
	 <%
	 	pageContext.setAttribute("people", "ying");
	 %>
	 <c:out value="jstl你好--c:out 内容输出标签"></c:out>
	 <c:out value="${people}"></c:out>
	 <!-- 这就是核心标签库与EL表达式的区别,可以设置默认值等,功能更强大 -->
	 <c:out value="${people1}" default="某人"></c:out>
</body>
</html>



<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>JSTL 核心标签库</title>
</head>
<body>
	<!-- c:redirect 客户端跳转(重定向) -->
	<c:redirect url="target.jsp">
		<c:param name="name" value="ying"></c:param>
		<c:param name="age" value="28"></c:param>
	</c:redirect>
	
</body>
</html>



<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>JSTL 核心标签库</title>
</head>
<body>
	<!-- c:remove 用来删除指定范围中的属性; -->
	<c:set var="people" value="张三" scope="request"></c:set>
	<h2><c:out value="${people}" default="没人啊"></c:out></h2>
	<c:remove var="people" scope="request"/>
	<h2><c:out value="${people}" default="没人啊"></c:out></h2>
	
	
</body>
</html>



<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>JSTL 核心标签库</title>
</head>
<body>
	<!-- c:set 用来设置 4 中属性范围值的标签; -->
	<c:set var="people" value="hehe" scope="request"></c:set>
	<h2><c:out value="${people}"></c:out></h2>
	
	<jsp:useBean id="people2" class="com.learn.model.People" scope="page"></jsp:useBean>
	
	<c:set property="id" target="${people2 }" value="007"></c:set>
	<c:set property="name" target="${people2 }" value="王二小"></c:set>
	<c:set property="age" target="${people2 }" value="26"></c:set>
	
	<h2>编号:${people2.id }</h2>
	<h2>姓名:${people2.name }</h2>
	<h2>年龄:${people2.age }</h2>
	
</body>
</html>


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>JSTL 核心标签库</title>
</head>
<body>
	<!-- c:url 生成一个 url 地址; -->
	<c:url value="http://www.eaglexb.com" var="url">
		<c:param name="name" value="eagle"></c:param>
		<c:param name="age" value="26"></c:param>
	</c:url>
	<a href="${url }">我的网站</a>
	
</body>
</html>


// 举例接收我的网站页面参数
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>JSTL 核心标签库</title>
</head>
<body>
	<h2>姓名:${param.name }</h2>
	<h2>年龄:${param.age }</h2>
</body>
</html>  

     2. 自定义标签

  

// 在WEB-INF下新建文件custom_tag.tld,内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">
    <tlib-version>1.0</tlib-version>
    <!-- short-name貌似没有实际用 
    	自定义标签系列使用body-content的作用:可参考:https://blog.csdn.net/mocharz/article/details/53112651
	    body-content的值有下面4种:
		<xsd:enumeration value="tagdependent"/>
	    <xsd:enumeration value="JSP"/>
	    <xsd:enumeration value="empty"/>
	    <xsd:enumeration value="scriptless"/>
    -->
    <short-name>javaCustomTag123334344</short-name>
    
    <tag>
    	<name>helloWorld</name>
    	<tag-class>
    		com.learn.tag.HelloWorldTag
    	</tag-class>
    	<body-content>empty</body-content>
    </tag>
    
    <tag>
    	<name>oneTags</name>
    	<tag-class>
    		com.learn.tag.OneTag
    	</tag-class>
    	<body-content>empty</body-content>
    	<attribute>
    		<name>name</name>
    		<required>true</required>
    		<rtexprvalue>true</rtexprvalue>
    		<!-- 自定义标签属性(Run-time Expression Value) -->
    	</attribute>
    </tag>
    
    <tag>
    	<name>iterate</name>
    	<tag-class>
    		com.learn.tag.IterateTag
    	</tag-class>
    	<body-content>JSP</body-content>
    	<attribute>
    		<name>var</name>
    		<required>true</required>
    		<rtexprvalue>true</rtexprvalue>
    	</attribute>
    	<attribute>
    		<name>items</name>
    		<required>true</required>
    		<rtexprvalue>true</rtexprvalue>
    	</attribute>
    </tag>
    
    <tag>
    	<name>iterateSimple</name>
    	<tag-class>
    		com.learn.tag.IterateSimpleTag
    	</tag-class>
    	<body-content>scriptless</body-content>
    	<attribute>
    		<name>var</name>
    		<required>true</required>
    		<rtexprvalue>true</rtexprvalue>
    	</attribute>
    	<attribute>
    		<name>items</name>
    		<required>true</required>
    		<rtexprvalue>true</rtexprvalue>
    	</attribute>
    </tag>
    
    
</taglib>


下面创建几个自定义标签对应的jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="ying" uri="/WEB-INF/custom_tag.tld" %>
<!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>问候Jsp 自定义标签--最基础的标签</title>
</head>
<body>
	<!-- 
		EL表达式、JSP标签、JSTL标签、自定义标签的区别与使用,参考下面链接
		https://blog.csdn.net/guanhang89/article/details/51277657
	 -->
	<!-- 自定义标签创建步骤如下:
		1. 编写一个普通的java类,继承SimpleTagSupport类或TagSupport类,叫标签处理器类
		2. 在web项目的WEB-INF目录下建立xxx.tld文件,这个tld叫标签库的声明文件。(参考核心标签库的tld文件)
		3. 在jsp页面的头部导入自定义标签库
		4. 在jsp中使用自定义标签
		注意: 自定义标签: 一般的中小型网站用到的机会不多,一般不用;但是一些大型的系统如果自定义标签的话可以大大提高工作效率
	 -->
	 
	 <ying:helloWorld/>

</body>
</html>



<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ taglib prefix="ying" uri="/WEB-INF/custom_tag.tld" %>
<!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>问候Jsp 自定义标签--自定义有标签体的标签</title>
</head>
<body>
	<!-- 自定义标签创建步骤如下:
		1. 编写一个普通的java类,继承SimpleTagSupport类或TagSupport类,叫标签处理器类
		2. 在web项目的WEB-INF目录下建立xxx.tld文件,这个tld叫标签库的声明文件。(参考核心标签库的tld文件)
		3. 在jsp页面的头部导入自定义标签库
		4. 在jsp中使用自定义标签
	 -->
	 <%
		List people=new ArrayList();
		people.add("王二小");
		people.add("司马光");
		people.add("呵呵");
		pageContext.setAttribute("people", people);
	%>
	 
	<ying:iterateSimple items="people" var="p">
		<h2>${p }</h2>
	</ying:iterateSimple>

</body>
</html>


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ taglib prefix="ying" uri="/WEB-INF/custom_tag.tld" %>
<!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>问候Jsp 自定义标签--自定义有标签体的标签</title>
</head>
<body>
	<!-- 自定义标签创建步骤如下:
		1. 编写一个普通的java类,继承SimpleTagSupport类或TagSupport类,叫标签处理器类
		2. 在web项目的WEB-INF目录下建立xxx.tld文件,这个tld叫标签库的声明文件。(参考核心标签库的tld文件)
		3. 在jsp页面的头部导入自定义标签库
		4. 在jsp中使用自定义标签
	 -->
	 <%
		List people=new ArrayList();
		people.add("王二小");
		people.add("司马光");
		people.add("呵呵");
		pageContext.setAttribute("people", people);
	%>
	 <ying:iterate items="people" var="p">
		<h2>${p }</h2>
	</ying:iterate>

</body>
</html>


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="ying" uri="/WEB-INF/custom_tag.tld" %>
<!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>问候Jsp 自定义标签--自定义有属性的标签</title>
</head>
<body>
	<!-- 自定义标签创建步骤如下:
		1. 编写一个普通的java类,继承SimpleTagSupport类或TagSupport类,叫标签处理器类
		2. 在web项目的WEB-INF目录下建立xxx.tld文件,这个tld叫标签库的声明文件。(参考核心标签库的tld文件)
		3. 在jsp页面的头部导入自定义标签库
		4. 在jsp中使用自定义标签
	 -->
	 
	 <ying:oneTags name="JspServlet屌炸天"/>

</body>
</html>


对应的java类:
package com.learn.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class HelloWorldTag extends TagSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	@Override
	public int doStartTag() throws JspException {
		JspWriter out = this.pageContext.getOut();
		try {
			out.println("自定义标签大爷你好!");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return TagSupport.SKIP_BODY; // 直接结束标签
	}

}




package com.learn.tag;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class IterateSimpleTag extends SimpleTagSupport{
	private String var;
	private String items;
	
	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}

	public String getItems() {
		return items;
	}

	public void setItems(String items) {
		this.items = items;
	}
	
	@Override
	public void doTag() throws JspException, IOException {
		Object value=this.getJspContext().getAttribute(items);
		if(value!=null && value instanceof List){
			Iterator iter=((List)value).iterator();
			while(iter.hasNext()){
				this.getJspContext().setAttribute(var, iter.next());
				this.getJspBody().invoke(null); // 响应页面
			}
		}
	}
}





package com.learn.tag;

import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class IterateTag extends TagSupport{

	/**
	 * 自定义有标签体的标签
	 */
	private static final long serialVersionUID = 1L;

	private String var;
	private String items;
	private Iterator iter;
	
	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}

	public String getItems() {
		return items;
	}

	public void setItems(String items) {
		this.items = items;
	}

	public Iterator getIter() {
		return iter;
	}

	public void setIter(Iterator iter) {
		this.iter = iter;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public int doStartTag() throws JspException {
		
		Object value=this.pageContext.getAttribute(items);
		if(value!=null && value instanceof List){
			this.iter=((List)value).iterator();
			if(iter.hasNext()){
				//this.pageContext.setAttribute(var, iter.next());
				return TagSupport.EVAL_BODY_INCLUDE; // 执行标签体
			}else{
				return TagSupport.SKIP_BODY; // 退出标签执行
			}
		}else{
			return TagSupport.SKIP_BODY; // 退出标签执行
		}
	}
	
	@Override
	public int doAfterBody() throws JspException {
		if(iter.hasNext()){
			this.pageContext.setAttribute(var, iter.next());
			return TagSupport.EVAL_BODY_AGAIN; // 再执行一次标签体
		}else{
			return TagSupport.SKIP_BODY; // 退出标签执行
		}
	}
	

}




package com.learn.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class OneTag extends TagSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String name;
	
	@Override
	public int doStartTag() throws JspException {
		
		JspWriter out=this.pageContext.getOut();
		try {
			out.println(name+":自定义有属性的标签大爷你好!");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return TagSupport.SKIP_BODY; // 直接结束标签
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

  

十一、el表达式

 

<%@ 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>EL表达式</title>
</head>
<body>
	<!-- EL(Expression Language) 是为了使JSP写起来更加简单.
	语法结构
		${expression}
	注意:page指令的 isELIgnored属性 表示是否禁用EL语言,TRUE表示禁止.FALSE表示不禁止.JSP2.0中默认的启用EL语言
	
	EL 表达式访问 4 种范围属性
	表达式语言(Expression Language,EL)
	寻找值的顺序:page->request->session->application
	 -->
	<%
		pageContext.setAttribute("info1","page范围的值");
		request.setAttribute("info2","request范围的值");
		session.setAttribute("info3","session范围的值");
		application.setAttribute("info4","application范围的值");
	%>
	<!-- 以后用下面el表达式这种方式 简化jsp输出 -->
	<h2>${info1 }</h2>
	<h2>${info2 }</h2>
	<h2>${info3 }</h2>
	<h2>${info4 }</h2>
	<%-- <h2>${info1 }</h2>
	<h2>${info1 }</h2>
	<h2>${info1 }</h2> --%>
</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>
	<form action="el-two.jsp" method="post">
		<input type="text" name="name"/>
		<input type="submit" value="提交el-two.jsp"/>
	</form>
	<a href="el-two.jsp?age=12">提交el-two.jsp</a>
	<form action="el-two.jsp" method="post">
		<input type="checkbox" name="hobby" value="java语言"/>java语言
		<input type="checkbox" name="hobby" value="C#语言"/>C#语言
		<input type="checkbox" name="hobby" value="php语言"/>php语言
		<input type="submit" value="提交el-two.jsp"/>
	</form>
</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>EL表达式param内置对象---接收传递到本页面的参数</title>
</head>
<body>
	<!-- 
		Param:单个参数
		paramValues:一组参数
		接收传递到本页面的参数
	 -->
	<%
		request.setCharacterEncoding("utf-8");
	%>
	<h1>姓名:${param.name }</h1>
	<h1>年龄:${param.age }</h1>
	<h1>爱好一:${paramValues.hobby[0] }</h1>
	<h1>爱好二:${paramValues.hobby[1] }</h1>
	<h1>爱好三:${paramValues.hobby[2] }</h1>		
</body>
</html>

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.learn.model.People" %>
<!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>EL表达式</title>
</head>
<body>
	<!-- EL 表达式对象操作 -->
	<%
		People zhangsan=new People();
		zhangsan.setId(1);
		zhangsan.setName("张三");
		zhangsan.setAge(20);
		request.setAttribute("zhangsan",zhangsan);
	%>
	<h1>ID:${zhangsan.id }</h1>
	<h1>姓名:${zhangsan.name }</h1>
	<h1>年龄:${zhangsan.age }</h1>
	
	<h1>ID:${zhangsan['id'] }</h1>
	<h1>姓名:${zhangsan['name'] }</h1>
	<h1>年龄:${zhangsan['age'] }</h1>
</body>
</html>

  

<%@page import="java.util.*"%>
<%@ 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>EL表达式</title>
</head>
<body>
	<!-- EL 表达式集合操作 -->
	<%
		List all = new LinkedList();
		all.add(0,"元素1");
		all.add(1,"元素2");
		all.add(2,"元素3");
		request.setAttribute("alls",all);
	%>
	<h1>${alls[0] }</h1>
	<h1>${alls[1] }</h1>
	<h1>${alls[2] }</h1>
</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>EL表达式</title>
</head>
<body>
	<!-- EL 表达式运算符操作 -->
	<%
		request.setAttribute("num1",10);
		request.setAttribute("num2",3);
		request.setAttribute("flag1",true);
		request.setAttribute("flag2",false);
	%>
	<h1>算数运算符:</h1>
	<h2>num1=${num1 },num2=${num2 }</h2>
	<h2>num1+num2=${num1+num2 }</h2>
	<h2>num1-num2=${num1-num2 }</h2>
	<h2>num1*num2=${num1*num2 }</h2>
	<h2>num1/num2=${num1/num2 }</h2>
	<h2>num1%num2=${num1%num2 }</h2>
	<h2>num1*(num1-num2)=${num1*(num1-num2) }</h2>
	<h1>关系运算符:</h1>
	<h2>flag1=${flag1 },flag2=${flag2 }</h2>
	<h1>逻辑运算符:</h1>
	<h2>与操作flag1 && flage2:${flag1 && flage2 }</h2>
	<h2>或操作flag1 || flage2:${flag1 || flage2 }</h2>
	<h2>非操作!flag1:${!flag1}</h2>
	<h1>三目运算符:</h1>
	<h2>三目操作 num1>num2:${num1>num2?"yes":"no" }</h2>
	<h1>empty关键字:</h1>
	<h2>判断空操作:${empty num1 }</h2>
	<h2>判断空操作:${empty a1 }</h2>
</body>
</html>

  

十二、JavaBean

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.learn.model.Student" %>
<!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>常规类调用--未使用javabean</title>
</head>
<body>
	<%
		Student stu = new Student();
		stu.setUsername("王小二");
		stu.setAge(28);
	%>
<h1>姓名:<%=stu.getUsername() %></h1>
<h1>年龄:<%=stu.getAge() %></h1>
</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>使用javabean--减少重复代码</title>
</head>
<body>
	<%-- jsp:useBean 创建 javabean  创建语法如下: --%>
	<%-- <jsp:useBean id="实例化对象名称" scope="保存范围" class="类完整名称"/>
Scope,一共有 page,request,session 和 application4 个属性范围,默认是 page; --%>
	
	<jsp:useBean id="stu" scope="page" class="com.learn.model.Student"/>

	<%
		stu.setUsername("赵云");
		stu.setAge(22);
	%>
<h1>姓名:<%=stu.getUsername() %></h1>
<h1>年龄:<%=stu.getAge() %></h1>
</body>
</html>

=======================================================
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.learn.model.Student" %>
<!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>常规获取参数并实例化--未使用javabean</title>
</head>
<body>
	
	<%
		//request.setCharacterEncoding("utf-8");
		//String name = request.getParameter("name");
		// 出现name乱码的现象:
		// 原因:Http请求传输时将url以ISO-8859-1编码,服务器收到字节流后默认会以ISO-8859-1编码来解码成字符流(造成中文乱码)
        // 解决办法:我们需要把request.getParameter(“参数名”)获取到的字符串先用ISO-8859-1编码成字节流,然后再将其用utf-8解码成字符流
		String name = new String(request.getParameter("name").getBytes("iso-8859-1"), "utf-8");  
		//out.println(str);
		String age  = request.getParameter("age");
		Student stu = new Student();
		stu.setUsername(name);
		stu.setAge(Integer.parseInt(age)); // 将字符串转化成整型
	%>
<h1>姓名:<%=stu.getUsername() %></h1>
<h1>年龄:<%=stu.getAge() %></h1>
</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>获取参数并实例化--使用javabean</title>
</head>
<body>
	<%-- jsp:setProperty 设置 javabean 属性值 --%>
	<%-- <jsp:setProperty property="属性名称" name="实例化对象的名称" value="属性值" param="参数名称"/>
Property=”*” 自动匹配所有 --%>
	<jsp:useBean id="stu" scope="page" class="com.learn.model.Student"></jsp:useBean>
	<jsp:setProperty property="*" name="stu"/>
<h1>姓名:<%=stu.getUsername() %></h1>
<h1>年龄:<%=stu.getAge() %></h1>
</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>获取参数并实例化--使用javabean</title>
</head>
<body>
	<%-- jsp:setProperty 设置 javabean 属性值 --%>
	<%-- <jsp:setProperty property="属性名称" name="实例化对象的名称" value="属性值" param="参数名称"/>
Property=”*” 自动匹配所有 --%>
	<jsp:useBean id="stu" scope="page" class="com.learn.model.Student"></jsp:useBean>
	<jsp:setProperty property="username" name="stu" value="ying"/>
	<jsp:setProperty property="age" name="stu"/>
<h1>姓名:<%=stu.getUsername() %></h1>
<h1>年龄:<%=stu.getAge() %></h1>
</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>获取参数并实例化--使用javabean</title>
</head>
<body>
	<%-- jsp:setProperty 设置 javabean 属性值 --%>
	<%-- <jsp:setProperty property="属性名称" name="实例化对象的名称" value="属性值" param="参数名称"/>
Property=”*” 自动匹配所有 --%>
	<jsp:useBean id="stu" scope="page" class="com.learn.model.Student"></jsp:useBean>
	<jsp:setProperty property="username" name="stu" param="username"/>
	<jsp:setProperty property="age" name="stu" value="25"/>
<h1>姓名:<%=stu.getUsername() %></h1>
<h1>年龄:<%=stu.getAge() %></h1>
</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>jsp:getProperty 获取 javabean 属性值</title>
</head>
<body>
	<%-- <jsp:getProperty property="属性名称" name="实例化对象的名称"/> --%>
	<jsp:useBean id="stu" scope="page" class="com.learn.model.Student"></jsp:useBean>
	<%
	stu.setUsername("王二小2");
	stu.setAge(12);
	%>
<h1>姓名:<jsp:getProperty property="username" name="stu"/></h1>
<h1>年龄:<jsp:getProperty property="age" name="stu"/></h1>
</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>javabean的保存范围之request</title>
</head>
<body>
	<jsp:useBean id="stu" scope="request" class="com.learn.model.Student"></jsp:useBean>
	<jsp:setProperty property="username" name="stu" value="ying"/>
	<jsp:setProperty property="age" name="stu" value="22"/>
	
	<jsp:forward page="target01.jsp"></jsp:forward>
</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>javabean--request 服务器内部跳转</title>
</head>
<body>
	<jsp:useBean id="stu" scope="request" class="com.learn.model.Student"></jsp:useBean>
	<h1>姓名:<jsp:getProperty property="username" name="stu"/></h1>
	<h1>年龄:<jsp:getProperty property="age" name="stu"/></h1>
</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>javabean的保存范围之session</title>
</head>
<body>
	<jsp:useBean id="stu" scope="session" class="com.learn.model.Student"></jsp:useBean>
	<jsp:setProperty property="username" name="stu" value="ying"/>
	<jsp:setProperty property="age" name="stu" value="22"/>
	
	<h1>Session数据设置完毕!</h1>
</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>javabean的保存范围之application</title>
</head>
<body>
	<jsp:useBean id="stu" scope="application" class="com.learn.model.Student"></jsp:useBean>
	<jsp:setProperty property="username" name="stu" value="ying"/>
	<jsp:setProperty property="age" name="stu" value="22"/>
	
	<h1>application数据设置完毕!</h1>
</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>javabean--删除</title>
</head>
<body>
<%--  javabean--删除:
	Page 范围:pageContext.removeAttribute(“javaBean Name”);
	request 范围:request.removeAttribute(“javaBean Name”);
	session 范围:session.removeAttribute(“javaBean Name”);
	application 范围:application.removeAttribute(“javaBean Name”);
 --%>
<%
	session.removeAttribute("stu");
%>
<h1>javabean已删除!</h1>
</body>
</html>

  

 

 

 

 

 

 

               

 

posted on 2016-11-10 17:44  eaglezb  阅读(174)  评论(0编辑  收藏  举报

导航