jsp入门——2、指令元素和动作元素

jsp page指令:能够控制导入哪些类,该servlet扩展哪个类,产生哪种MIME类型,如何处理多线程,servlet是否共享会话,输出缓冲区的大小行为,由哪个页面处理意外错误

import属性:指定由jsp页面生成的servlet生成的页面的MIME,

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
         pageEncoding="ISO-8859-1"%>
<%@ page import="com.bean.*" %>导入类
<%@ page contentType="application/vnd.ms-excel"%>生成电子表格
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="com.bean.*" %>
<%@ page contentType="application/vnd.ms-excel"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<%
		User user = new User();
		user.setName("small cat");
		out.print(user.getName());
	%>
</body>
</html>

 errorpage属性:指定一个JSP页面,抛出异常如果未在当前页面捕获,则由指定页面处理,错误处理可以通过exception访问抛出异常

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" errorPage="error.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
	String name = null;
	int len = name.length();
%>
</body>
</html>

当未指定errorpage

指定error.jsp,用exception捕获异常时要加入isErrorPage="true"属性

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" isErrorPage="true"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
当前繁忙,请稍后重试
<%=exception.toString() %>
</body>
</html>

 

 include指令:可以在多个页面重用jsp内容,且需要jsp页面能够影响主页面。输出内容small cat是imp.jsp中的内容

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%@ include file="imp.jsp" %><br>
my home
</body>
</html>

session属性:指定页面是不是会话的一部分,不常用

<%@ page session="true" %>
<%@ page session="false" %>

jsp:forward:转发请求到指定文件,

扫描二维码关注公众号,回复: 2801749 查看本文章
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:forward page="login.jsp"></jsp:forward>
</body>
</html>

jsp:getProperty:不需要用到显示的java编程就能访问bean的属性

<jsp:getProperty property="id" name="per"/>
<jsp:getProperty property="name" name="per"/>
<jsp:getProperty property="age" name="per"/>

等价于下面表达式

<%=per.getId() %>
<%=per.getName() %>
<%=per.getAge() %>

jsp:setProperty:不需要用到显示的java编程就能设置bean的值

<jsp:setProperty property="id" name="per" value="1"/>
<jsp:setProperty property="name" name="per" value="zilaiye"/>
<jsp:setProperty property="age" name="per" value="13"/>

等价于下面表达式

<%
  per.setId(2);
  per.setName("kakaxi");
  per.setAge(23);			
%>

jsp:useBean:不需要用到显示的java编程就能创建java类的实例,从请求参数中导出对象值更容易,在页面和servlet间共享对象更容易,很少用

	<jsp:useBean id="per" class="com.bean.Person" />

等同于

<%  Person per = new Person();  %>

小例子:

login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form id="f1" action="beantest.jsp" method="post">
	id:<input type="text" name="id"><br>
	name:<input type="text" name="name"><br>
	age:<input type="text" name="age"><br>
	<input type="submit">
</form>
</body>
</html>

beantest.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="com.bean.*" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<%-- <%
		Person per = new Person();
		per.setId(2);
		per.setName("kakaxi");
		per.setAge(23);
		
		out.println(per.getId());
		out.println(per.getName());
		out.println(per.getAge());
	%> --%>
	
	<jsp:useBean id="per" class="com.bean.Person" scope="session"/>
	
	<%-- <jsp:setProperty property="id" name="per" value="1"/>
	<jsp:setProperty property="name" name="per" value="zilaiye"/>
	<jsp:setProperty property="age" name="per" value="13"/> --%>
	
	<jsp:setProperty property="*" name="per"/>
	
	<%-- <jsp:getProperty property="id" name="per"/>
	<jsp:getProperty property="name" name="per"/>
	<jsp:getProperty property="age" name="per"/> --%>
	
	<%-- ${per.id}
	${per.name}
	${per.age} --%>
	
	<%=per.getId() %>
	<%=per.getName() %>
	<%=per.getAge() %>
</body>
</html>

计算器:

package com.bean;

public class Calculator {

	private double first;
	private double second;
	private String operator;
	
	private double result;

	public double getFirst() {
		return first;
	}

	public void setFirst(double first) {
		this.first = first;
	}

	public double getSecond() {
		return second;
	}

	public void setSecond(double second) {
		this.second = second;
	}

	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

	public double getResult() {
		if(operator!=null&&operator.equals("+")) {
			result = first + second;
		}else if(operator!=null&&operator.equals("-")) {
			result = first - second;
		}else if(operator!=null&&operator.equals("*")) {
			result = first * second;
		}else if(operator!=null&&operator.equals("/")) {
			result = first / second;
		}else {
			result = 0 ;
		}
		return result;
	}

	public void setResult(double result) {
		this.result = result;
	}
}

jsp页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="c" class="com.bean.Calculator"></jsp:useBean>
<jsp:setProperty property="*" name="c"/>
Result:${c.first} ${c.operator} ${c.second} = ${c.result}
<form action="calculator.jsp" method="post">
	<table>
		<tr>
			<td>first num:</td>
			<td><input type="text" name="first"></td>
		</tr>
		
		<tr>
			<td>operator</td>
			<td><select name="operator">
					<option value="+">+</option>
					<option value="-">-</option>
					<option value="*">*</option>
					<option value="/">/</option>
				</select>
			</td>
		</tr>
		
		<tr>
			<td>second num:</td>
			<td><input type="text" name="second"></td>
		</tr>
		<tr>
			<td><input type="submit" value="calculator"></td>
		</tr>
	</table>
</form>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/Milan__Kundera/article/details/81490338