JSP常用的内置——response对象

1.response对象定义

response对象将服务器端数据发送到客户端,该对象包含响应客户请求的有关信息,封装了JSP产生的响应,然后被发送到客户端以响应客户的请求。

2.response对象常用的方法

(1)sendRedirect():用于重新定向客户端的请求;

(2)getBufferSize():用于获取实际缓冲区的大小,如果没使用缓冲区则返回0;

(3)setCharacterEncoding():用于设置响应的字符编码方式;

(4)getCharacterEncoding():用于获取响应的字符编码方式。

3.response对象应用实例

response.jsp页面综合使用了response对象的多个方法。单击“确定”按钮后跳转到responseHandle.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>response对象应用实例</title>
</head>
<body>
<hr>
<%
    response.setBufferSize(1024);
%>
<%=response.getBufferSize() %>
<br>
<%
    response.setCharacterEncoding("UTF-8");
%>
<%=response.getCharacterEncoding() %>
<br>
网站友情链接:
<hr>
<form action="responseHandle.jsp" method="get">
    <select name="link">
        <option value="W3School">W3School</option>
        <option value="baidu">百度</option>
    </select>
    <input type="submit" name="submit" value="确定">
</form>
</body>
</html>

对应的图片为

responseHandle.jsp页面根据选择的数据调用sendRedirect()方法进行页面重定向

<%@ 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 address=request.getParameter("link");
    if(address!=null){
        if(address.equals("W3School")){
            response.sendRedirect("https://www.w3school.com.cn/");
        }else{
            response.sendRedirect("https://www.baidu.com/");
        }
    }
%>
</body>
</html>

对应的结果是跳转到W3School或者百度页面。

posted @ 2020-11-01 10:02  星火天涯  阅读(935)  评论(0)    收藏  举报