TastA
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp</title>
</head>
<body>
<!-- JSP=java server pages -->
<!-- html=静态网页 -->
<!-- JSP=动态网页(网页的模板引擎) --html -->
<!-- JSP通俗解释:html的基础之上多了一些java的东西 ,学的就是多出来的那些规则-->
<!-- JSP本质上就是一个servlet -->
<!-- http:/127.0.0.1:8080
<form action="/demo211229/test/test220104/TestB.jsp" method="post">
账号<input type="text" name="code"><br>
密码<input type="password" name="pass"><br>
<input type="radio" name="sex" value="1">男
<input type="radio" name="sex" value="2">女<br>
<input type="checkbox" name="hobby" value="a">跑步
<input type="checkbox" name="hobby" value="b">游泳
<input type="checkbox" name="hobby" value="c">羽毛球<br>
<select name="addr">
<option value="bejing">北京</option>
<option value="shanghai">上海</option>
<option value="guangzhou">广州</option>
</select><br>
<input type="hidden" name="action" value="hide">
<input type="submit">
<input type="reset">
</form>
</body>
</html>
TestB
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String code = request.getParameter("code");
String pass=request.getParameter("pass");
System.out.print(code+"\t"+pass);
out.print(code+"\t"+pass);
%>
</body>
</html>
TestBase
<%@page import="org.apache.jasper.tagplugins.jstl.core.Out"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- http://127.0.0.1:8080/demo211229/test/test220104/TestBase.jsp -->
<!-- JSP本质上就是一个servlet -->
<!-- jsp--servlet--html -->
<%
int a1=1;
System.out.print(a1);
out.print(a1);
String s2="asdasd";
%>
<hr>
<% out.print(a1);%>
<jsp:scriptlet>
out.print(a1);
</jsp:scriptlet>
<hr>
<%!
String s2="aaa";
void func(){}
%>
<jsp:declaration>
</jsp:declaration>
<hr>
<%=
"asdasd"+a1
%>
<br>
<jsp:expression>"asdasd"+a1</jsp:expression>
<hr>
<%-- jsp注释(写法+效果+区别):在jsp解析成html时就被注释掉 --%>
<%-- <%= "asdasd"+a1%> --%>
<!-- html注释 -->
<!-- 流程控制语句 -->
<%if(a1==1){%>
<div>aaa</div>
<%}else{ %>
<div>bbb</div>
<%} %>
</body>
</html>
```
# 指令directive
```java
<%@ page
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"
session="true"
import="test122131.PersonModel,test122131.TestJson,java.util.*"
%>
<!-- java.util.*--将java.util包下直接摆放的文件全部导入进来,但不包含他子包下的文件 -->
<!-- http://127.0.0.1:8080/demo211229/test/test220104/Test3directive.jsp -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>指令directive</title>
</head>
<body>
<%
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTf-8");
PersonModel p1=new PersonModel();
%>
指令directive=page+include+taglib
<!-- 格式= -->
<%-- <%@ directive attr1="val1" attr2="val2"%> --%>
<!-- taglib:标签库 ,在jstl中使用:jsp standard taglib-->
<%-- <%@ taglib uri="" prefix="" %> --%>
<%-- <jsp:directive.taglib uri="" prefix="">--标准写法 --%>
<hr>
<!-- include包含:file=请求转发的路径 -->
<%@ include file="/test/test220104/TestA.jsp" %>
<%-- <jsp:directive.include file="/test/test220104/TestA.jsp"> 标准路径 --%>
asdasdasdasd
</body>
</html>
动作action
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp动作行为action</title>
</head>
<body>
<!-- http://127.0.0.1:8080/demo211229/test/test220104/Test4Action.jsp -->
<!-- 指令包含include -->
<!-- 动作包含include -->
action格式
<%-- <jsp.action attr1="value" attr2="value2" /> --%>
<%-- <%@ include file="/test/test220104/TestA.jsp" %> --%>
<jsp:include page="/test/test220104/TestA.jsp" />
<%-- <%request.getRequestDispatcher("/test/test220104/TestA.jsp").include(request,response); %> java代码中的include --%>
<!-- 区别 -->
1-语法不同,属性
2-过程不同,解析
--指令包含(静态包含)
被包含的文件被原封不动的放入到包含页面中使用该指令的位置处
然后jsp编译时会和成一个文件进行编译
所以在一个jsp页面中使用include指令来包含另一个jsp页面,
最终编译后的文件只有一个
--动作包含(动态包含)
当执行该动作后,jsp会将请求发到被包含的页面,
并将执行结果输出到浏览器中,然后返回页面继续执行后面的代码,
因为web容器执行的是两个文件,所以jsp编译时会分为两个文件进行编译
注意:
动作include通常用于那些经常改变的文件
因为被包含的文件改动不会影响所包含的文件
因此不需要对包含文件进行重新编译
<% int a=1; %>
<!-- 页面跳转 -->
<%-- <%request.getRequestDispatcher("/test/test220104/TestA.jsp").forward(request,response); %> java代码中的forward --%>
<jsp:forward page="/test/test220104/TestA.jsp"></jsp:forward>
</body>
</html>
Test3directive
<%@ page
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"
session="true"
import="test122131.PersonModel,test122131.TestJson,java.util.*"
%>
<!-- java.util.*--将java.util包下直接摆放的文件全部导入进来,但不包含他子包下的文件 -->
<!-- http://127.0.0.1:8080/demo211229/test/test220104/Test3directive.jsp -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>指令directive</title>
</head>
<body>
<%
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTf-8");
PersonModel p1=new PersonModel();
%>
指令directive=page+include+taglib
<!-- 格式= -->
<%-- <%@ directive attr1="val1" attr2="val2"%> --%>
<!-- taglib:标签库 ,在jstl中使用:jsp standard taglib-->
<%-- <%@ taglib uri="" prefix="" %> --%>
<%-- <jsp:directive.taglib uri="" prefix="">--标准写法 --%>
<hr>
<!-- include包含:file=请求转发的路径 -->
<%@ include file="/test/test220104/TestA.jsp" %>
<%-- <jsp:directive.include file="/test/test220104/TestA.jsp"> 标准路径 --%>
asdasdasdasd
</body>
</html>
Test4Form
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- http://127.0.0.1:8080/demo211229/test/test220104/Test4Form.jsp -->
<form action="/demo211229/test/test220104/Test4Formjsp.jsp">
编号:<input type="text" name="code1">
姓名:<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
Test4Formjsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="pm3" class="test122131.PersonModel" scope="request"></jsp:useBean>
<jsp:setProperty name="pm3" property="*" />
<!-- *作用:将参数中的参数名与Javabean中的属性名(property)进行对应 -->
<!-- 如果相同,则将参数值赋值给对应的属性上作为属性值 -->
<!-- 如果参数名和属性名不同,则用下面的方式 -->
<jsp:setProperty name="pm3" property="code" param="code1"/>
<jsp:setProperty name="pm3" property="name" param="name"/>
<jsp:getProperty name="pm3" property="code"/>
<jsp:getProperty name="pm3" property="name"/>
</body>
</html>
Test4useBean
<%@page import="test122131.PersonModel"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>useBean</title>
</head>
<body>
<!-- http://127.0.0.1:8080/demo211229/test/test220104/Test4useBean.jsp -->
<!-- 动作 -->
<!-- useBean -->
<!-- 创建(获得)对象并放在域中 -->
<!-- 若域中无该对象,则创建并存放在域中 -->
<!-- 若域中有该对象,则从域中获得该对象 -->
<jsp:useBean id="pm0" class="test122131.PersonModel" scope="request"></jsp:useBean>
<!-- 用java代码实现如下 -->
<%-- <% --%>
<!--
<%-- %> --%>
<jsp:setProperty name="pm0" property="code" value="A1001"/>
<%-- <% --%>
<!-- Object pm00=request.getAttribute("pm0"); -->
<!-- ((PersonModel)pm00).setCode("A1001"); -->
<%-- %> --%>
<jsp:getProperty property="code" name="pm0"/>
<%-- <% --%>
<!-- out.print(((PersonModel)pm0).getCode()); -->
<!-- %> -->
</body>
</html>
Test5scope
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp内置对象(隐式/隐含)-9个</title>
</head>
<body>
pageContext request session application --- 四个web域(范围)对象(类似)
out response config page exception
<%
String name="zhangsan";
request.setAttribute("aaa", name);
Object val= request.getAttribute("aaa");
String valString1=(String) val;
String valString2=val.toString();
session.setAttribute("aaa", name);
session.getAttribute("aaa");
application.setAttribute("aaa", name);
application.getAttribute("aaa");
pageContext.setAttribute("aaa", name);
pageContext.getAttribute("aaa");
%>
从上到下,由小变大
pageContext -- 当前页面的范围
request -- 一次请求的范围
session -- 一次会话的范围(两个终端建立连接之后) -- 同一个用户共有的范围(一般存用户的信息和验证码)
application -- (ServletContext类型)一个应用的范围 -- 所有用户共用的范围