1. JSP编译指令page
定义JSP程序的全局属性,包括当前JSP所使用的脚本语言类型,需要导入的java包的列表等。一个JSP中可以有多个page指令
- language:脚本语言的种类
- contentType:设置生成网页的编码,还有mame
- charset:服务器生成网页的编码
- pageEncoding:JSP本身的编码
- import:导入java类
import="java.util.ArrayList,java.util.LinkedList"
- errorPage:设置错误处理页面
errorPage="error.jsp"
- isErrorPage:设置当前JSP程序是否为错误处理程序
isErrorPage="true"
2. JSP编译指令include
在body体内添加
<%@ include file="***.html" %>
<%@ include file="***.jsp" %>
3. JSP动作指令forward
编译指令:通知servelet引擎的处理消息,他只在JSP消息转换成servelet的过程中起作用。
动作指令:客户端在请求期间动态被执行。
forward:用于将当前的程序重新定向到一个HTML页面或者是一个动态程序。有两种形式,一种是带参数的,一种是不带参数的。forward是重定向指令,它下面的指令不会被执行。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:forward page="hello.jsp"></jsp:forward>
param指令:传递参数
// 设置参数
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:forward page="hello.jsp">
<jsp:param value="darkmi" name="userName"/>
<jsp:param value="123456" name="password"/>
</jsp:forward>
// 获取参数值(hello.jsp内)
<body>
String userName = request.getParameter("userName");
String password = request.getParameter("password");
</body>
4. JSP动作指令include
动态包含指令,用于在JSP程序中包含一个静态或者动态的文件,静态文件一般是html文件,动态文件一般就是JSP文件,如果是静态文件直接加载到当前JSP文件中,如果是JSP文件会先被JSP引擎执行然后才会将生成的内容加载到JSP中。
示例:
(1)可以把新建的jsp文件中的head标签剪切到一个空的html文件中,然后用include动态指令包含
<jsp:include page="head.html"></jsp:include>
(2)可以把新建的jsp文件的body标签剪切到一个空的jsp文件内
// 2.jsp
<jsp:include page="1.jsp">
<jsp:param value="red" name="bgcolor"/>
</jsp:include>
// 1.jsp
<body bgcolor="<%=request.getParameter("bgcoler") %>">
</body>
5. JSP动作指令useBean
- java类生成默认构造函数:
编辑代码区域右键 -> Source -> Generate Consturctor Using Fields…
在新打开的区域点击取消选择所有属性(Deselect All) - java类生成getter和setter方法:
编辑代码区域右键 -> Source -> Generate Getters And Setters… - 格式化代码
Command+Shift+F
userBean的使用:
// register.jsp
<body>
// action:表单信息将要提交的URL
<form action="do_register.jsp" method="post">
用户名:<input type="text" name="userName"/>
密码:<input type="password" name="password"/>
<input type="submit" value="提交"/>
</form>
</body>
// do_register.jsp
<body>
<!-- -------使用userBean-------- -->
<!-- id:设置javaBean的名称。通过id可以在jsp的其在地方引用这个javaBean -->
<!-- class:类的完全限定名 -->
<jsp:useBean id="user" class="com.jikexueyuan.entity.UserEntity"></jsp:useBean>
<!-- name:指定javaBean的名称 -->
<jsp:setProperty property="userName" name="user"/>
<jsp:setProperty property="password" name="user"/>
<jsp:getProperty property="userName" name="user"/>
<jsp:getProperty property="password" name="user"/>
<br/>
<!-- -------不使用userBean-------- -->
<%
// request:jsp的内置对象
String userName = request.getParameter("userName");
String password = request.getParameter("password");
%>
</body>
// userEntity.java
package com.jikexueyuan.entity;
import java.io.Serializable;
public class UserEntity implements Serializable {
private String userName;
private String password;
public UserEntity() {
super();
}
public String getUserName() {
System.out.println(userName);
// 可以处理网页传来的数据
return userName+"123";
}
public void setUserName(String userName) {
System.out.println(userName);
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}