JSP response对象
response 对象的常用方法如下:
方 法 | 说 明 |
---|---|
void addHeader(String name, String value) | 添加头信息(参数名称和对应值) |
void addCookie(Cookie cookie) | 添加 cookie 信息 |
void sendRedirect(String location) | 实现页面重定向 |
void setStatus(int sc) | 实现页面的响应状态代码 |
void setContentType(String type) | 设置页面的 MIME 类型和字符集 |
void setCharacterEncoding(String charset) | 设定页面响应的编码类型 |
示例
下面在 login.jsp 新建表单,在 checkdetails.jsp 接收 login.jsp 提交的用户名和密码,与指定的用户名和密码相比,相同则登录成功,重定向到 success.jsp;反之登录失败,重定向到 failed.jsp。login.jsp 代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>编程帮(www.biancheng网站站点" rel="nofollow" />
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>编程帮(www.biancheng网站站点" rel="nofollow" />
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>编程帮(www.biancheng网站站点" rel="nofollow" />
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>编程帮(www.biancheng网站站点" rel="nofollow" />
登录页面(输入正确的用户名和密码)

登录成功页面

登录页面(输入错误的用户名)

登录失败页面