当前位置:文档之家› jsp基础知识解析

jsp基础知识解析


Import指令
Impor指令是page中唯一允许多次导入的指令 <%@page contentType="test/html;charset=GBK"%> <%@page import="java.sql.*"%> <%@page import="java.io.*"%>
Include指令
两种表现形式: @include指令 <jsp:include>指令 @include是静态包含,只将被包含的内容显示 出来,可以包含任何文件
动态打印表格
<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>
哪种输出方式更好? 打印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>"); %>
建立forward4.jsp: <%@page contentType="text/html;charset=GBK"%> <h2>跳转之后的页面!</h2> <h2><%=request.getParameter("ref1")%></h 2> <h2><%=request.getParameter("ref2")%></h 2>
课程简介
JSP技术是Java Web技术的基础。它是 基于Java Servlet以及Java平台的Web开发技 术,具有“一次编写,各处运行”等,优点 牢固掌握JSP技术,是架构高性能Web应用 的基础。 本课程旨在使学生能够较全面系统地了解 目前基于WEB开发的过程,重点掌握JSP动 态网站架构与应用开发技术。
<%
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>
Scriptlet
在JSP中Scriptlet形式有三种: <%%> <%!%> <%=%>
第一种Scriptlet <%%>
在<%%>中可以定义变量,编写语句。 编写数字累加的操作,从1-100累加 <% int sum=0; for(int i=0;i<=100;i++){ sum+=i; } out.print("<h1>sum="+sum+"</h1>"); %>
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>
语法一: 建立forward1.jsp <jsp:forward page="forward2.jsp"/> 建立forward2.jsp <%@page contentType="text/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"%>
建立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>
第二种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>"); %>
相关主题