JSP内置对象

JSP动作元素:

<%@page contentType="text/html;charset=UTF-8"%>
<%request.setCharacterEncoding("GB2312");%>
<jsp:useBean id="student" class="ch04.Student" scope="session"/>
<jsp:setProperty name="student" property="*"/>
<jsp:setProperty name="student" property="age" value="30"/>
<html>
<head>
<title>
Action动作示例
</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
</head>
<body>
<%if(student.isQuery()){ %>
<h2>查询结果:</h2><br>
     姓名:<jsp:getProperty name="student" property="name"/><br>
     学号:<jsp:getProperty name="student" property="stuId"/><br>
     年龄:<jsp:getProperty name="student" property="age"/><br>
<%} %>
<hr>
<h2>
学生
</h2>
<form name="javabean" action="action.jsp" method="get">
<input type=hidden name="query" value="true">
<p>姓名<input type="text" name="name">
<p>学号<input type="text" name="stuId">

<p>

<input type="submit" value="提交"/>
<input type="reset" value="重置"/>
</p>
</form>
</body>
</html>


JSP内置对象:
response对象:
<%@page contentType="text/html;charset=UTF-8"%>
<%
	String str=request.getParameter("submit");
	if(str==null){
		str="";
	}
	if(str.equals("yes")){
		response.setContentType("application/msword;charset=UTF-8");
	}
%>
<html>
<head>
	<title>response对象示例</title>
</head>
<body>
	<p>我正在学习response对象
	<p>将当前页面保存为word文档吗?
	<form method="get" name="form">
		<input type="submit" value="yes" name="submit">
	</form>
</body>	
</html>

out对象:
getBufferSize()方法取得目前缓冲区的大小(KB),getRemaining()取得目前使用后还剩余的缓冲区的大小(KB);
<%@page contentType="text/html;charset=UTF-8"%>
<html>
	<head>
		<title>OUT示例</title>
	</head>
<body>
	<h2>javax.servlet.jsp.JspWriter-out对象</h2>
	<%
		out.println("学习使用out对象:<br>");
		int Buffer=out.getBufferSize();
		int Available=out.getRemaining();
	%>
	BufferSize:<%=Buffer%><br>
	Available:<%=Available%><br>
</body>
</html>


session对象:
<%@page contentType="text/html;charset=UTF-8"%>
<%!
	int number=0;
	synchronized void countPeople(){
		number++;
		}
%>
<%
	if(session.isNew()){
		countPeople();
		String str=String.valueOf(number);
		session.setAttribute("count",str);
		}
	String sessionID=session.getId();
%>
<html>
<head>
	<title>session对象示例</title>
</head>
<body>
<h2>session</h2>
<p>您的session对象的ID是:<%=sessionID%>
<p>您是第<%=(String)session.getAttribute("count")%>个访问本站的人
</body>
</html>


application对象:
<%@page contentType="text/html;charset=UTF-8"%>
<%
	int Num=0;
	String activePage=application.getRealPath("header.jsp");
%>
<html>
	<head>
		<title>application对象示例</title>
	</head>
	<body>
	<center>
		<font size=5>applicaion对象示例</font>
	</center>
	<hr>
	 
	 本页面对应的实际路径是:<%=activePage%><br>
	 <br>
	 application对象中变量Num的值是:<%=Num%>
	</body>
</html>


exception对象:
error.jsp页面在运行中会抛出空指针导常,如:
<%@page contentType="text/html;charset=UTF-8"%>
<%@page import="java.util.*" errorPage="header.jsp"%>
<% 
	Calendar rightNow=null;
	rightNow.getTime();
%>

   如果发生空指针异常,重定向到exception.jsp中,它使用exception对象报告发生异常的错误原因:
<%@page contentType="text/html;charset=UTF-8" isErrorPage="true"%>
<%@page import="java.io.*"%>
<html>
	<head>
		<title>exception对象示例</title>
	</head>
	<body>
		<p>exception对象示例
		<hr size="0">
		
		<font size="-1">
		下面有错误发生,请将错误报告到:[email protected]
		</font>
		
		<%
			exception.printStackTrace();
			StringWriter sout=new StringWriter();
			PrintWriter pout=new PrintWriter(sout);
			exception.printStackTrace(pout);
		%>
		<pre>
			<%=sout.toString()%>
		</pre>
		
	</body>
</html>





你可能感兴趣的:(java,html,jsp,servlet)