Jsp技术是将Java代码嵌入到HTML网页代码中,再由Jsp引擎将这些代码转换成Servlet代码,从而解决了Servlet输出网页需要大量的println代码的不足。
3.1 JSP概念
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>JSP测试页</title>
</head>
<body>
<%
java.util.Date now = new java.util.Date();
out.println("当前时间是:"+now);
%>
<h1>你好,这就是一个JSP页面</h1>
</body>
</html>
3.2 JSP执行过程
3.3JSP的优点
4. JSP页面元素
<%@ page contentType="text/html;charset=UTF-8" %>
<HTML>
<BODY>
<%
for ( int i=0; i<2; i++ )
{
%>
你好<br>
<%
}
%>
</BODY>
</HTML>
<%@ page contentType="text/html;charset=UTF-8" %>
<HTML>
<BODY>
<%
for ( int i=0; i<2; i++ )
{
out.print("你好<br>");
}
%>
</BODY>
</HTML>
4.1JSP脚本元素
4.1.1声明<%! %>
在<%! …… %>中声明的变量、方法、类,在整个jsp文件内都有效,是全局的。
<%!inti = 0; %>
<%!inta, b, c; %>
<%!Date date;%>
计数器 程序:
<%@ page contentType="text/html;charset=GBK" %>
<HTML>
<BODY >
<%! int i=0; %>
<% i++; %>
<P>您是第 <%=i%>个访问本站的客户。
</BODY>
</HTML>
注意观察与前面计数器程序的异同
<%@ page contentType="text/html;charset=UTF-8" %>
<HTML>
<BODY >
<% int i=0; %>
<% i++; %>
<P>您是第 <%=i%>个访问本站的客户。
</BODY>
</HTML>
<%@ page contentType="text/html;charset=UTF-8" %>
<HTML>
<BODY>
<%!
long continueSum(int n)
{ int sum=0;
for(int i=1;i<=n;i++)
sum=sum+i;
return sum;
}
%>
<P>1到100的连续和:<BR>
<% long sum;
sum=continueSum(100);
out.print(sum);
%>
</BODY>
</HTML>
4 .1.2 JAVA 表达式
<%=expression %> 或<%=表达式%>
<% a=12;
b=a;
c=a+b; %>
a=<%=a %><br>
b=<%=b %><br>
c=<%=c %><br>
<%@page contentType="text/html;charset=UTF-8"%>
<HTML>
<BODY>
<p> Sin(0.9)除以3等于
<%=Math.sin(0.90)/3%>
<p>3的平方是:<%=Math.pow(3,2)%>
<p>12345679乘72等于
<%=12345679*72%>
<p> 5的平方根等于<%=Math.sqrt(5)%>
<p>99大于100吗?回答:<%=99>100%>
</BODY>
</HTML>
4.2JSP指令
4.2.1page指令
<%@ page language = "java" import ="java.util.*"
buffer = "8kb" errorPage="error.jsp" isErrorPage="false"%>
<html>
<head>
<title> 测试 page 指令</title>
</head>
<body>
<h1> 这是一个测试页面</h1>
</body>
</html>
4 .2.2include指
<%@include file = "文件名"%>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@include file="head.jsp"%>
<html>
<head>
<title>include 指令</title>
</head>
<body>
<h4 align="center">你好,欢迎您!</h4>
</body>
</html>
4
.2.3t
aglib
指令
<%@ taglib uri = "标签库表述符文件"prefix = "前缀名"%>
<html>
<body>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
</body>
</html>
JSP 页面在根目录中搜索mytaglib签库描述符文件,
而 mytags前缀将mytaglib中的标签嵌入JSP页面
<c:outvalue="${param.name}" default="!"/>
<c:setvar="username"value="jack" scope="session"/>
<c:iftest="${stu.age<18}"var="young">对不起</c:if>
<c:choose>
<c:whentest="${c.coloreq'red'}">颜色:<c:outvalue="${c.color}"/></c:when>
<c:otherwise>颜色:<c:out value="${c.color}"/></c:otherwise>
</c:choose>
<c:forEachvar="user"items="${users}">
<c:out value="${user.name}"/>
<c:out value="${user.pwd}"/>
</c:forEach>
<c:redirecturl="${test}"/>
<c:forTokens……>
……..
JSP动作
1 JavaBean
1.1 JavaBean的构成
package a;
public class Person
{
private String name;
public String getName()
{
return name;
}
public void setName(String myname)
{
name = myname;
}
}
1.2 JSP
使用
JavaBean
2.1 useBean动作
...
<head>
<jsp:useBean id="p" class="Person" scope="page"/>
</head>
...
创建引用并嵌入现有的 Bean
id 属性创建对class属性中所述类的引用
2.2 setProperty / getProperty 动作2.3 useBean动作范围
2.4 forward动作
2.5 include动作
隐式对象
内置对象 | 代表内容 | 范围 |
request | 客户端的请求信息被封装在该对象中 | request |
response | 对请求的应答 | page |
pageContext | 对JSP页面内所有的对象及名字空间的访问,可以取输出流、session、request、response、属性、页面跳转、包含文件 | page |
session | 客户端与服务器的一次会话,从客户连到服务器的一个Web Application开始, 直到客户端与服务器断开连接为止 | session |
application | 实现了用户间数据的共享,可存放全局变量.它开始于服务器的启动,直到服务器的关闭 | application |
out | 向输出流写入内容的对象 | page |
config | 本 JSP 的ServletConfig | page |
page | 实现处理本页当前请求的类的实例 | page |
根据页面表单组件名称获取请求页面提交数据(如文本框、密码框、单选框、下拉框等)
获取页面请求中一个表单组件对应多个值时的用户的请求数据(如复选框)
使用request对象要注意避免使用空对象,否则出现NullPointerException异常
<%!
public String formatStr(String str){
return str==null?"":str;
}
%>
<%
String name = formatStr(request.getParameter("name"));
String pwd = formatStr(request.getParameter("pwd"));
String confirm_pwd
= formatStr(request.getParameter("confirm_pwd"));
String gender = formatStr(request.getParameter("gender"));
String xueli = formatStr(request.getParameter("xueli"));
String[] channels = request.getParameterValues("channel");
%>
发送方式 | 显示中文接收方法 |
get | <%@page contentType="text/html; charset=UTF-8"%> <% //不用转换编码 String name = request.getParameter("userName"); out.println("name="+name); %> |
post | <%@page contentType="text/html; charset=UTF-8"%> <% request.setCharacterEncoding("UTF-8"); String name = request.getParameter("userName"); out.println("name="+name); %> |
设置作为响应生成的内容的类型和字符编码
发送一个响应给浏览器,指示其应请求另一个URL
<%@ page contentType="text/html; charset=utf-8" %>
<html>
<head>
<title>response对象</title>
</head>
<body>
<%
response.setContentType("text/html;charset=utf-8");
response.sendRedirect("response2.jsp");
%>
</body>
</html>
2.3out 对象
<html>
<body>
<%
out.print("欢迎来到 JSP:隐式对象") ;
out.write("欢迎来到 JSP:隐式对象") ;
%>
…
</body>
</html>
3作用域通信对象
page、request、session和 application
session、application和 pageContext
3.1session 对象
以键/值的方式,将一个对象的值存放到session中
根据名称去获取session中存放对象的值
<%
session.setAttribute("name", "admin");
%>
<%
if(session.getAttribute("name")!=null)
{
String name = (String)session.setAttribute("name");
}
%>
3.2application
对象
以键/值的方式,将一个对象的值存放到application中
根据名称去获取application中存放对象的值
<%
application.setAttribute("ChartRecord", "hihi");
%>
<%
if(application.getAttribute("ChartRecord")!=null)
{
String chartRecord =
(String) application.getAttribute("ChartRecord");
}
%>
3.3
pageContext
对象
以键/值的方式,将一个对象的值存放到pageContext中
scope为作用域范围,取值如下:
根据名称去获取pageContext中存放对象的值
scope为作用域范围,取值如上
<%@ page pageEncoding="UTF-8"%>
<%
pageContext.setAttribute("page","page");
//pageContext.setAttribute("page","page",PageContext.PAGE_SCOPE); //同上步
pageContext.setAttribute("request","request",PageContext.REQUEST_SCOPE);
pageContext.setAttribute("session","session",PageContext.SESSION_SCOPE);
pageContext.setAttribute("application","application",PageContext.APPLICATION_SCOPE);
//本页接收
out.println(pageContext.getAttribute("page"));
out.println(pageContext.getAttribute("request"));
out.println(pageContext.getAttribute("request",PageContext.REQUEST_SCOPE));
out.println(pageContext.getAttribute("session"));
out.println(pageContext.getAttribute("session",PageContext.SESSION_SCOPE));
out.println(pageContext.getAttribute("application"));
out.println(pageContext.getAttribute("application",PageContext.APPLICATION_SCOPE));
//其他页能否接收?
%>
4 Servlet 对象
<% @ page info="我的信息"
contentType="text/html;charset=UTF-8"%>
<html>
<body>
<%=((javax.servlet.jsp.HttpJspPage)page).getServletInfo()%>
</body>
</html>
4.2
config
对象
String propertyFile
=(String)config.getInitParameter("PropertyFile");
config对象
获取初始化参数
5 exception 对象
<%@ page isErrorPage="true" %>
<html>
<head><title> 处理错误 </title></head>
<body>
<% if ( exception != null )
{
out.write("\n 发生错误。\n");
}
else
{
out.write("\n 您已访问此页面,
但是没有可用的错误信息\n");
}
%>
</body>
</html>
6
错误页定义
web.xml:
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
Jsp页面:
<%@ page isErrorPage="true" %>
<%@ page errorPage="error.jsp" isErrorPage="false" %>
7
页面跳转的区别
request.getRequestDispatcher("new.jsp").forward(request,response)
<jsp:forwardpage="new.jsp"></jsp:forward>
response.sendRedirect("new.jsp")
<script>
window.location="new.jsp";
//window.navigate("new.jsp");
</script>
总结: