<pre name="code" class="java"><span style="font-family:Microsoft YaHei;font-size:18px;">1、内置对象介绍</span>
可以不加声明和创建就可以再JSP页面脚本(Java程序片和Java表达式)中使用的成员变量
由Tomcat创建
2、out
类型:Javax.serlet.jsp.JspWriter
描述:主要用来向客户端输出数据
作用域:page
重要方法:print()/println()/write() 向客户端输出数据
3、request
类型:Java.servlet.http.HttpServletRequest
描述:来自客户端的请求经Servlet容器处理后由request对象进行封装
作用域:request
重要方法:
getParameter(key):获取提交表单的数据
getParameterValues(key):获取提交表单的一组数据
request.getRequestDispatcher("list.jsp").forward(request,response):转发
request.setAttribute(key,object):设置请求对象的属性
request.getAttribute(key):获取请求对象的属性
request.setCharacterEncoding("gbk"):对请求数据重新编码
<pre name="code" class="java"><%@ 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>Insert title here</title>
</head>
<body>
<form action="request-receive.jsp" method="post">
user:<input type="text" name="user"/><br/>
pwd:<input type="password" name="pwd"/><br/>
<input type="checkbox" name="likes" value="睡觉"/>睡觉
<input type="checkbox" name="likes" value="吃饭"/>吃饭
<input type="checkbox" name="likes" value="打豆豆"/>打豆豆
<br/>
<input type="submit" value="登陆"/>
</form>
</body>
</html>
<!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//获取请求数据
//设置post请求方式的编码
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("user");
String pwd=request.getParameter("pwd");
String [] likes=request.getParameterValues("likes");
for(int i=0;i<likes.length;i++){
System.out.println(likes[i]);
}
if(("xb".equals(name))&&("123".equals(pwd))){
request.setAttribute("sex", "男");
out.print("success");
request.getRequestDispatcher("request-success.jsp").forward(request,response);
}else{
out.print("fail");
}
%>
<%@ 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>Insert title here</title>
</head>
<body>
<%=request.getParameter("user")
%>
<%=request.getAttribute("sex")
%>
</body>
</html>
</pre><pre name="code" class="java">
4、reponse
类型:Javax.serlet.http.HttpServletResponse
描述:封闭了JSP响应,然后被发送到客户端以响应客户的请求
作用域:page
重要方法:response.sendRedirect("页面"):页面跳转、重定向
response.setCharaterEncoding("gbk"):设置响应编码
</pre><p></p><p><span style="font-family:'Microsoft YaHei'; font-size:18px">5、session</span></p><p><span style="font-family:'Microsoft YaHei'; font-size:18px"><span style="white-space:pre"> </span>类型:Java.servlet.http.HttpSession</span></p><p><span style="font-family:'Microsoft YaHei'; font-size:18px"><span style="white-space:pre"> </span>描述:表示一个会话,用来保存用户信息,以跟踪每个用户的状态</span></p><p><span style="font-family:'Microsoft YaHei'; font-size:18px"><span style="white-space:pre"> </span>session(会话):是指在一段时间内客户端和服务器之间的一连串交互过程</span></p><p><span style="font-family:'Microsoft YaHei'; font-size:18px"><span style="white-space:pre"> </span>作用域:session</span></p><p><span style="font-family:'Microsoft YaHei'; font-size:18px"><span style="white-space:pre"> </span>重要方法:session.getid() 取得session 的id号</span></p><p><span style="font-family:'Microsoft YaHei'; font-size:18px"><span style="white-space:pre"> </span>session.isnew() 判断session是不是新建的</span></p><p><span style="font-family:'Microsoft YaHei'; font-size:18px"><span style="white-space:pre"> </span>session.setAttribute(key,object):往当前会话中设置一个属性</span></p><p><span style="font-family:'Microsoft YaHei'; font-size:18px"><span style="white-space:pre"> </span>session.getAttribute(key):获取当前会话中的一个属性</span></p><p><span style="font-family:'Microsoft YaHei'; font-size:18px"><span style="white-space:pre"> </span>session.removeAttribute(key):删除当前会话中的属性</span></p><p><span style="font-family:'Microsoft YaHei'; font-size:18px"><span style="white-space:pre"> </span>session.setMacInactiveInterval(1000*60*30) 设置当前会话的失效时间</span></p><p><span style="font-family:'Microsoft YaHei'; font-size:18px"><span style="white-space:pre"> </span>session.incalidate():初始化当前会话(退出功能,删除当前会话的数据)</span></p><p><span style="font-family:'Microsoft YaHei'; font-size:18px"></span><pre name="code" class="java"><h2>登陆表单 session的使用</h2>
<form action="session.jsp" method="post">
user:<input type="text" name="user"/><br/>
pwd:<input type="password" name="pwd"/><br/>
<input type="submit" value="登陆"/>
</form>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String name= request.getParameter("user");
String pwd=request.getParameter("pwd");
if("admin".equals(name) && "123".equals(pwd)){
//把用户名保存到当前会话对象中
//设置session的有效时间30分钟、默认30分钟
session.setMaxInactiveInterval(1000*60*30);
session.setAttribute("name", name);
response.sendRedirect("session-success.jsp");
}else{
response.sendRedirect("response.jsp");
}
%>
<%@ 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>Insert title here</title>
</head>
<body>
<%
String name=(String)session.getAttribute("name");
out.print(name);
//session.invalidate(); 设置session的重新初始化,在程序退出时使用
%>
</body>
</html>
短期数据放在request中
6、pageContext
类型:javax.servlet.jsp.PageContext
描述:本jsp的页面上下文
作用于:page
<%
//通过pageContext上下文对象获取当前页面的其它内置对象
pageContext.getRequest();
pageContext.getResponse();
pageContext.getSession();
String path=request.getContextPath();
out.println(path);
%>
7、application
类型:javax.servlet.ServletContext
描述:从servlet配置对象获得servlet上下文
作用于:application
<h2>application对象</h2>
<%
//一个应用程序只用一个application对象
//在一个服务器启东市创建,到服务器关闭时销毁
//所有客户端共享一份
String serverpath=application.getContextPath();
out.println(serverpath);
//向application对象添加数据
application.setAttribute("", "");
%>
类型:javax.servlet.ServletConfig
描述:本JSP的ServletConfig
作用于:page
9、page
类型java.lang.Object
描述:实现处理本页当前请求的类的实例(javax.servlet.jsp.Http.JspPage),转换后的Servlet类本身
作用域:page
10、exception
类型:java.lang.Exception
描述:本JSP页面的异常现象
作用域:page
JSP常见错误状态码
403:禁止访问。比如IP地址被拒绝,站点访问被拒绝
404:找不到。没有找到文件或目录
500:服务器由于遇到错误而不能完成该请求,web服务器太忙