jsp基础知识解析

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

动态打印表格
<form action="print2.jsp" method="post"> <table border="0"> <tr> <td colspan="2">打印表格</td> </tr> <tr> <td>输入打印表格行数:</td> <td><input type="text" name="rows"></td> </tr> <tr> <td>输入打印表格列数:</td> <td><input type="text" name="cols"></td> </tr> <tr> <td colspan="2"> <input type="submit" value="打印"> <input type="reset" value="重置"> </td> </tr> </table> </form>
Import指令
Impor指令是page中唯一允许多次导入的指令 <%@page contentType="test/html;charset=GBK"%> <%@page import="java.sql.*"%> <%@page import="java.io.*"%>
Include指令
两种表现形式: @include指令 <jsp:include>指令 @include是静态包含,只将被包含的内容显示 出来,可以包含任何文件
<table border="1"> <% for(int i=0;i<100;i++){ %> <tr> <% for(int j=0;j<100;j++){ %> <td><%=i*j%></td> <% } %> </tr> <% } %> </table>
Page指令
现在建立一个JSP网页,要求显示中文 <h1>中国,您好!</h1> <%@page contentType="test/html;charset=GBK"%> <h1>中国,你好!</h1>
@include指令
建立3个文件: test.html <h1>test.html</h1> test.txt <h1>test.txt</h1> test.aaa <h1>test.aaa</h1>
包含以上3个内容: <h1>includedemo.jsp</h1> <%@include file="test.html"%> <%@include file="test.txt"%> <%@include file="test.aaa"%>
<%! public static int add(int a,int b){ return a+b; } %> <% out.println(add(10,20)); %>
wenku.baidu.com
第三种Scriptlet <%=%>
<%=%>称为表达式输出,可以直接使用此 形式的Script输出一个变量或一个具体的内 容。 <%! public static int add(int a,int b){ return a+b; Public String name="yangyi"; } <h1><%=add(10,20)%></h1> <h1><%=name%></h1>
哪种输出方式更好? 打印100*100的表格: <% out.println("<table>"); for(int i=0;i<100;i++){ out.println("<tr>"); for(int j=0;j<100;j++){ out.print("<td>"+(i*j)+"</td>"); } out.println("</tr>"); } out.println("</table>"); %>
Javascript验证
<script language="javaScript"> function xxx(f){ if(!(/^\d+$/.test(f.rows.value))){ alert("行数必须是数字!"); f.rows.focus(); return false; } if(!(/^\d+$/.test(f.cols.value))){ alert("列数必须是数字!"); f.cols.focus(); return false; } return true; } </script> <form action="print2.jsp" method="post" onSubmit="return xxx(this)">
jsp:include指令
此语句为动态包含,如果被包含的页面时JSP,则 先处理之后再将结果包含,而如果包含的是非*.jsp 文件,则只是把文件内容静态包含起来,功能与 @include类似 语法一: <jsp:include page="页面"/> 语法二: <jsp:include page="页面"> <jsp:param name="参数名称" value="值"/> ……… </jsp:include>
第二种Scriptlet <%!%> 在<%!%>中定义全局常量,编写方法,编写类,但一 般不会再JSP中定义一个类,绝对不能直接在里面 编写任何的语句。 <%! public static final String xxx="hello"; public static final String sss="world"; %> <% out.print("<h1>xxx="+xxx+"</h1>"); out.print("<h1>sss="+sss+"</h1>"); %>
建立test1.jsp文件,此文件可以接受参数
<h2><%=request.getParameter("ref1")%></h2> <h2><%=request.getParameter("ref2")%></h2>
建立test11.jsp文件,此文件向test1.jsp中传递参数 <h1>includetest1.jsp</h1> <jsp:include page="test1.jsp"> <jsp:param name="ref1" value="HELLO WORLD"/> <jsp:param name="ref2" value="nihao"/> </jsp:include>
9种内置对象 4种属性范围
JSP四种属性范围
属性:属性范围 pageContext: 在一个页面范围内 request: 在一个服务器请求范围内 session: 在一次会话范围内 application: 在一个应用服务器范围内
pageContext
pageContext 属性范围:是最为重要的JSP属 性之一,但是如果使用纯粹的JSP开发,该 属性显示不出用途。通常用于Struts、 WebWork框架
<%
int row=Integer.parseInt(request.getParameter("rows")); int col=Integer.parseInt(request.getParameter("cols")); %> <table border="1"> <% for(int i=0;i<row;i++){ %> <tr> <% for(int j=0;j<col;j++){ %> <td><%=i*j%></td> <% } %> </tr> <% } %> </table>
使用jsp:include指令 <% int i=10; %> <h2>test23.jsp中的i值为<%=i%></h2> <jsp:include page="test2.jsp"/>
forward
语法一: <jsp:forward page="页面"/> 语法二: <jsp:forward page="页面"> <jsp:param name="参数名称" value="值"/> ………… </jsp:forward>
课程简介
JSP技术是Java Web技术的基础。它是 基于Java Servlet以及Java平台的Web开发技 术,具有“一次编写,各处运行”等,优点 牢固掌握JSP技术,是架构高性能Web应用 的基础。 本课程旨在使学生能够较全面系统地了解 目前基于WEB开发的过程,重点掌握JSP动 态网站架构与应用开发技术。
使用语法一完成之前类似的功能 <h1>includedemo.jsp</h1> <jsp:include page="test.html"/> <jsp:include page="test.txt"/> <jsp:include page="test.aaa"/>
运行结果与以前一样,因为都是静态页面
建立test2.jsp: <% int i=1000; %> <h2>test2.jsp中的i值为<%=i%></h2> 使用两种包含语句包含如下代码:
使用@include指令 <% int i=10; %> <h2>test22.jsp中的i值为<%=i%></h2> <%@include file="test2.jsp"%>
服务器端验证 <% int row=0; int col=0; try{
row=Integer.parseInt(request.getParameter("rows ")); col=Integer.parseInt(request.getParameter("cols") ); }catch(Exception e){} %>
Scriptlet
在JSP中Scriptlet形式有三种: <%%> <%!%> <%=%>
第一种Scriptlet <%%>
在<%%>中可以定义变量,编写语句。 编写数字累加的操作,从1-100累加 <% int sum=0; for(int i=0;i<=100;i++){ sum+=i; } out.print("<h1>sum="+sum+"</h1>"); %>
教学重点难点
教学重点: Jsp内置对象的应用及四种属性范 围、 JavaBean、Servlet、JDBC 教学难点:网站综合设计
JSP基础知识
JSP是在java上的一种运用,但有其自 己的扩充语法,java中的一切语句可以在 JSP中使用。 在JSP中注释有2大类: 显式注释:HTML代码中注释<!-- --> 隐式注释:Java中代码注释:// /*..*/ JSP自己的注释:<%-- --%>
建立forward4.jsp: <%@page contentType="text/html;charset=GBK"%> <h2>跳转之后的页面!</h2> <h2><%=request.getParameter("ref1")%></h 2> <h2><%=request.getParameter("ref2")%></h 2>
request
request属性范围:将属性保存在一次请求范围 之内 前提必须使用服务器跳<jsp:forward/> 应用 MVC设计模式、Struts、Webwork
session
session属性范围:是要设置上去,则不管是 什么跳转,都可以取得属性,与session有关 的任何打开的页面都可以取得session 应用于验证用户是否登陆。
也可以向跳转页面传递参数 建立forward3.jsp: <%@page contentType="text/html;charset=GBK"%> <jsp:forward page="forward4.jsp"> <jsp:param name="ref1" value="hello"/> <jsp:param name="ref2" value="world"/> </jsp:forward>
语法一: 建立forward1.jsp <jsp:forward page="forward2.jsp"/> 建立forward2.jsp <%@page contentType="text/html;charset=GBK"%> <h1>跳转后的页面</h1> 从页面显示效果来看,页面已经完成了跳转,但是 地址没有变化,因此此跳转属于服务器端跳转,只 要是服务器端跳转,则地址栏永远没有变化
相关文档
最新文档