JSP三个指令: page指令、include指令、taglib指令
page 指令用来设置JSP页面的属性和相关功能,基本语法形式如下:
<%@ page attribute1="value1" [...attribute2="value n"]%>
inclued 指令是在JSP页面生成Servlet时引入需要包含的页文件,可以是HTML文件也可以是JSP文件,还可以是其它文件(例如JS文件),作用是在标签插入的位置插入静态的文件内容,使其与JSP文件组合成新的JSP页面,然后由JSP引擎翻译成Servlet文件。
基本语法如下:
<%@ include file="url"%>
taglib指令(又名标签指令)是JSP新增的一个指令,用户可以自定义新的标签在页面中执行。基本语法如下:
<% taglib uri="tagliburl" prefix="tagpre" %>
JSP动作:
<jsp:include page="relative url" flush=true"/>
<jsp:forward page="relative url"/>
<jsp:param name="参数名称" value="参数值"/>
request对象常用方法
getParameter()
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>homework1</title>
</head>
<span style="font-family:SimSun;font-size:18px;"><body>
<h1>测试</h1>
<hr/>
<form action="getParameter.jsp" method="post">
用户名:<input type="text" name="username"/><br/>
密 码:<input type="password" name="password"/><br/>
爱 好:<input type="checkbox" name="hobby" value="cf"/>吃饭
<input type="checkbox" name="hobby" value="sj"/>睡觉
<input type="checkbox" name="hobby" value="ddm"/>打代码
<br>
<input type="submit" value="提交"/>
</form>
</body>
</span>
</html>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>' getParameter.jsp'</title>
</head>
<body>
<center>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
String[] hobby = request.getParameterValues("hobby");
%>
<td>get:<%=request.getParameter("xxx") %></td><br>
<td>get:<%=request.getParameter("yyy") %></td><br>
<td>username:<%=username %></td><br>
<td>password:<%=password %></td><br>
<td>password:<%=Arrays.toString(hobby) %></td><br>
</center>
</body>
</html>
getParameterValues()
response对象例子
setHeader
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>My JSP setMeta.JSP</title>
</head>
<body>
<center class="aa">
<p class="bb">
现在的时间为:<br>
<%
out.print(""+ new Date());
response.setHeader("refresh","1");
response.setHeader("description","实时的显示当前时间");
response.setHeader("keywords","实时,显示,当前时间");
response.setHeader("cache-control","no-cache");
%>
<br><br>
copyright:2015
</center>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
response.sendRedirect("sendPageError.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>sendRedirect.jsp</title>
</head>
<body>
This is my JSP page.<br>
</body>
</html>