JSP课堂代码笔记

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//demo211229/test/test220104/TestA.jsp -->
    	<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");//doget/dopost
    String code = request.getParameter("code");
    String pass=request.getParameter("pass");
    System.out.print(code+"\t"+pass);//java控制台输出
    out.print(code+"\t"+pass);//response.getWriter()
    %>
    
    </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);//out:jsp的内置对象(隐式对象,隐含对象)--9个
    // 	PrintWriter out = response.getWriter();
    	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>&nbsp;
    <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();//import导入
    // test122131.PersonModel p2=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();//import导入
	// test122131.PersonModel p2=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代码实现如下 -->
<%-- <% --%>
<!--  // request.setAttribute("pm00", new PersonModel()); -->
<%-- %> --%>

<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 code=request.getParameter("code");
// 请求当中的参数获取
// Map <String,Object> map=new HashMap<>();
// map.put("aaa",new Object());
// Object val=map.get("aaa");
String name="zhangsan";
request.setAttribute("aaa", name);
Object val= request.getAttribute("aaa");
// request域中获取属性
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类型)一个应用的范围 -- 所有用户共用的范围
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值