jsp一共有九大内置对象
- out
- request
- response
- session
- application
- page
- pagecontext
- exception
- config
1.out
- out.println()像客户端打印
- out.flush()讲缓冲区内容输出到客户端
- out.clear()清除缓冲区内容,在flush之后会异常
- out.cleatBuffer()清除,但是不会抛出异常
void.close()关闭输出流
2.requset
3.response
请求转发与请求重定向
4. session
5.application
jsp内置对象实例,实现用户登录
//login.jsp<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%><% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>imooc - Login
//dologin.jsp<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%><% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; String username =""; String password =""; request.setCharacterEncoding("utf-8");//防止中文乱码 username = request.getParameter("username"); password = request.getParameter("password"); //如果用户和密码都等于admin,则登录成功 if("admin".equals(username)&&"admin".equals(password)) { session.setAttribute("loginUser", username); request.getRequestDispatcher("login_success.jsp").forward(request, response); //服务器内部转发 用requset } else {//失败 请求重定向 response.sendRedirect("login_failure.jsp"); }%>
//login_success.jsp<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%><% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>imooc - Login
login_failure.jsp@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%><% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>imooc - Login 登录失败!请检查用户或者密码! 返回登录