※ 如何写一个jsp页面以及在页面中如何写java代码。
jsp页面中主要写的东西分为三部分:
1. jsp的脚本元素
1.1表达式(expression)
形式:<%= %>
例如:<%="hello" %>
<%=1+1 %>
<%=s.getName() %>
将来翻译到java文件中的位置:
_jspService方法中的out.print(..)里面的参数.
例如上面那几个例子会翻译成
out.print("hello");
out.print(1+1);
out.print(s.getName());
所以System.out.prntln()这个方法的参数可以写什么,那么我们这个jsp页面中表达式里面就可以写什么.
注意:在jsp中的表达式里面不需要加;号。
1.2脚本(scriptlet)
形式:<% ... %>
<%
....
%>
例如:
<%
Student s = new Student();
String name = "tom";
String username = s.getName();
List<String> list = new ArrayList<String>();
list.add("hello")
%>
将来翻译到java文件中的位置:
脚本中的代码将来会被直接翻译到_jspService这个方法中.
在一个方法中我们可以写什么样的代码,那么在脚本中就可以写什么样的代码.
注意:在脚本中所写的代码,代码的最后是要加上;号的。因为我们在一个方法里面所写的没一句代码后面都是要加;号。
在脚本声明的变量,我们是可以在表达式里面使用的,但是要注意要先声明变量再使用变量.只要查看脚本和表达式分别翻译到java文件中的位置,就可以很清楚的认识到这一点.
1.3声明(declaration)
形式:<%!
.....
%>
例如:
<%!
private String address;
public String go(){
System.out.println("hello world jd1307");
return "this is go()";
}
%>
将来翻译到java文件中的位置:
直接将声明中的代码翻译到java文件里面所定义的类中。所以我们直接可以在一个类中写什么,就可以在声明中写什么.(一般在声明中会去定义一些类中的成员变量或者方法)
注意:这里面的代码,定义变量的时候要加;号,定义方法的时候不用加;号,这是和我们写一个java类语法是一样的。
2. jsp的指令元素
jsp的指令是给jsp引擎看的,让jsp引擎在翻译jsp页面成java文件的时候,知道需要注意哪些地方的设置.比如页面中的编码、页面中脚本里面所用的编程语言、翻译的java文件中需要引入哪些其他包下的java类等等.
写法:
<%@指令名字 属性="值" .. %>
※ page指令
1)表示当前页面中的编程语言是java,目前这个属性值只能写java
language="java"
2)在当前页面中要引入哪些包下的类.
import="java.util.HashMap"
import="java.util.HashMap,java.sql.Connection"
3)设置jsp页面文件保存时候所用的编码
pageEncoding="UTF-8"
4)设置服务器将来使用io把jsp页面内容一行一行的输出给浏览器的时候,使用什么编码向浏览器输出.
contentType="text/html; charset=UTF-8"
5)设置jsp页面被翻译成java文件的时候,java文件中的类要继承那个父类.这个属性不用设置,jsp引擎会给它一个默认的父类去继承的.
extends=""
6)设置当前这个jsp页面是否支持session对象的使用.默认是支持的.
session="true"
7)设置jsp页面是否是线程安全的.
isThreadSafe="true"
8)如果a.jsp页面中设置了errorPage="b.jsp",那么a.jsp页面在运行的时候一旦出错,就会自动跳转到b.jsp里面.
errorPage=""
9)如果一个页面中设置了isErrorPage="true",那么就表示这个页面是用来专门显示错误信息的页面.
然后在这个页面中就能够使用到隐藏对象exception来显示出错误的信息了.
isErrorPage=""
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<base href="<%=basePath %>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<!-- 脚本元素(写java代码)
表达式
System.out.println();
表达式相当于
out.write();输出,该输出在_jspSevice(req,res)
方法中,表达式中只可以直接操作
8个内置对象(request,session,application
pagecontext,config,out,page,response
注意:表达式中每有分号
)
-->
<%= 1+1%>
<%= "test"%>
<%= request.getParameter("name")%>
<!-- 脚本
java代码(java中的方法能写的
内容该位置都可以写)
脚本中的代码将来在_jspService(req,res)
中
-->
<%
for(int i=0;i<100;i++){
System.out.println("--"+i);
out.println(i);
}
%>
<!-- 声明(全局变量或方法)
位置和_jspService方法同级
-->
<%!
private String name;
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
%>
</body>
</html>
//isErrorpage写在错误的页面err.jsp
//test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
errorPage="err.jsp"%>
<!-- 跳转错误界面 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<%
int i=10/0;
%>
</body>
</html>
//err.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
isErrorPage="true"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<base href="<%=basePath %>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<!-- exception隐藏对象,
isErrorPage true可以直接使用 -->
<%=exception.printStackTrace() %>
亲:资源不存在
<body>
</body>
</html>
※ include指令
<%@include file="" %>
作用:在当前页面中使用include指令可以把另外一个页面的内容引入到当前页面。
一个页面包含/引入另外一个页面有俩种方式:静态包含 动态包含。
(这个include指令就属于静态包含)
静态包含特点:例如a.jsp页面中静态包含了b.jsp页面,那么在翻译期间,jsp引擎在翻译a.jsp成为一个a_jsp.java文件的时候,发现jsp页面中有include指令,这时候jsp引擎就会把被包含的页面b.jsp中内容原封不动的拿到a_jsp.java中,然后用io输出出去.
<!--head.jsp-->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
${param.name}
<h1>头部导航栏部分</h1>
<!--include.jsp-->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- taglib引入标签库,uri指向的标签库的名字
prefix给标签库起前缀(防止命名冲突)
-->
<%@taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="h"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<base href="<%=basePath %>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<!-- 引入其他的jsp页面内容
特点:引入页面的jsp页面不会编译成java文件
当前页面在编译成java页面的过程,直接把需要
导入的内容加载进来
-->
<%@include file="head.jsp?name" %>
<h1>内容区域</h1>
<%@include file="foot.jsp" %>
</body>
</html>
<!--foot.jsp-->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<h1>杰普。。。。。。</h1>
※ taglib指令
作用:在当前jsp页面中引入一些特殊的标签库。比如jstl标签库、struts2标签库等等。jar包放在WebContent下的WEB-INF的lib包内。
3. jsp的动作元素
1)jsp:useBean(构建对象)
<jsp:useBean id="s" class="com.briup.bean.Student" scope="page"></jsp:useBean>
相当于代码:
<%
Student s = null;
s = (Student)pageContext.getAttribute("s");
if(s==null){
s = new Student();
pageContext.setAttribute("s",s);
}
%>
2)jsp:setProperty(设置属性)
<jsp:setProperty property="name" value="tom" name="s"/>
相当于代码:
<%
Student s = (Student)pageContext.setAttribute("s");
s.setName("tom");
%>
3)jsp:getProperty(获取属性)
<jsp:getProperty property="name" name="s"/>
相当于代码:
<%
Student s = (Student)pageContext.getAttribute("s");
out.write(s.getName());
%>
<!--测试代码:-->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<base href="<%=basePath %>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<!-- 构建对象,并存储在特定的容器中
id构建对象的变量名
class 包名+类名
scope存储容器的范围 page request session application
-->
<jsp:useBean id="stu" class="com.briup.web.bean.User"
scope="page"></jsp:useBean>
<!--
Object obj=pageContext.getAttribute("stu");
Student stu=null;
if(obj==null){
stu=Class.forName("com.briup.web.bean.User").newInstance();
pageContext.setAttribute("stu",stu);
}
-->
<!-- out.write();-->
<!--该注解叫做隐藏注解,用户不可见,
jsp页面编译成java文件的时候忽略 -->
<%-- <%= pageContext.getAttribute("stu")%>
<%
out.println(pageContext.getAttribute("stu"));
%>
test${pageScope.stu}test --%>
<!-- 给对象设置属性
name指的是对象的变量名
property执行对象的属性,将来自动拼接set首字母大写
-->
<jsp:setProperty property="passwd" name="stu" value="0123456"/>
<!--
Student stu=(Student)pageContext.getAttribute("stu");
stu.setPasswd("123456")
-->
<%-- ${pageScope.stu.passwd} --%>
<!-- 从容器中的对象取其属性值 -->
<jsp:getProperty property="passwd" name="stu"/>
<!--
Student stu=(Student)pageContext.getAttribute("stu");
out.println(stu.getPasswd());
-->
</body>
</html>
4)jsp:forward(页面跳转)
jsp:param(缓存客户端请求参数)
注意:param只能获取客户端请求的参数。服务端带过去的获取不到(三大容器.setAttribute())
<jsp:forward page="target.jsp"></jsp:forward>
//跳转的同时还可以传参数
<jsp:forward page="target.jsp?name=tom"></jsp:forward>
或者
<jsp:forward page="target.jsp">
<jsp:param value="tom" name="name"/>
</jsp:forward>
//这个动作元素专门是传参数使用的
<jsp:param value="tom" name="name"/>
<!--测试代码:forward.jsp-->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<base href="<%=basePath %>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<%
//request.getRequestDispatcher("/test.jsp").forward(request, response);
//request.getRequestDispatcher("/sess").forward(request, response);
%>
<!-- 服务器内部跳转到页面或servlet类中 -->
<%-- <jsp:forward page="fo.jsp"></jsp:forward> --%>
<!-- 通过地址栏传的参数并没有放到四大容器内,(不在request容器内) -->
<%-- <jsp:forward page="fo.jsp?name=tom&age=33"></jsp:forward> --%>
<!--注意:jsp:forward子标签前面不能写注解之类的内容 -->
<jsp:forward page="fo.jsp">
<jsp:param value="tom" name="name"/>
<jsp:param value="33" name="age"/>
</jsp:forward>
</body>
</html>
<!--fo.jsp-->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<base href="<%=basePath %>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<%=request.getParameter("name") %>
<%=request.getParameter("age") %>
<br>
<hr>
<!-- 客户端请求提交的参数都可以直接取 -->
<!-- 此处不能用指定容器获取,只能用param-->
${param.name}
${param.age}
</body>
</html>
5)jsp:include(动态包含)
<jsp:include page="foot2.jsp?name=tom"></jsp:include>
<!--测试代码:jspinclude.jsp-->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<base href="<%=basePath %>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<!-- 动态包含(把其他页面的内容引入单前
页面,加载其他页面是在单前页面
运行的时候加载)
动态包含可以传参数给加载的页面
静态包含不可以传参数给加载的页面
-->
<jsp:include page="head.jsp?name=jake"></jsp:include>
<h1>内容部分</h1>
<jsp:include page="foot.jsp"></jsp:include>
</body>
</html>
动态包含特点:在把jsp文件翻译成java文件的期间,动态包含并不会去把被包含的页面原封不动的拿过来,而是会把动态包含这个标签翻译成一个方法的调用,将来运行页面调用到这个方法的时候才会去拿被包含页面的内容.同时还可以给动态包含的页面传参数.静态包含是不能传参数的。
注意:总结和对比后,分析出动态包含和静态包含各自的特点,以及哪一个执行的效率更快一些.
6)jsp:element(添加标签)
//使用jsp的动作元素向浏览器输出一个标签
<jsp:element name="font">
<jsp:attribute name="color">blue</jsp:attribute>
<jsp:body>hello world</jsp:body>
</jsp:element>
这个效果相当于在页面代码中直接写上<font color="blue">hello world</font>
或者:
<%
out.println("<font color='blue'>hello world</font>");
%>
<!--测试代码:这种构建标签方法过于繁琐,一般不用-->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<base href="<%=basePath %>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<!-- 用jsp指令构建标签对象
<font color="red" size="35">test</font>
jsp:element
name属性是标签的名字
jsp:attribute
name 指的是属性的名字
jsp:attribute中间的文本指的是属性的值
jsp:body标签标签修饰的文本内容
<div class="test">
<span>test</span>
</div>
<jsp:element name="div">
<jsp:attribute name="class">test</jsp:attribute>
<jsp:body>
<jsp:element name="span">
<jsp:body>test</jsp:body>
/jsp:element>
</jsp:body>
</jsp:element>
-->
<%-- <jsp:element name="font">test</jsp:element> --%>
<jsp:element name="font">
<jsp:attribute name="color">red</jsp:attribute>
<jsp:attribute name="size">33</jsp:attribute>
<jsp:body>test</jsp:body>
</jsp:element>
</body>
</html>