二.Response对象:对客户的请求做出动态的响应,向客户端发送数据。飞飞Asp技术乐园 1.动态响应contenType属性 当一个用户访问一个jsp页面时,如果该页面用page指令设置页面的contentType属性是text/html,那么jsp引擎将按照这种属性值作出反映。如果要动态改变这个属性值来响应客户,就需要使用response对象的setContentType(String s)方法来改变contentType的属性值。 格式:response.setContentType(String s) 参数s可取text/html,, application/x-msexcel, application/msword等。 例如:response1.jsp
<%@ page c %> <HTML> <BODY bgcolor=cyan><Font size=1 > <P>我正在学习response对象的 <BR>setContentType方法 <P>将当前页面保存为word文档吗? <FORM action="" method="get" name=form> <INPUT TYPE="submit" value="yes" name="submit"> </FORM> <% String str=request.getParameter("submit"); if(str==null) {str=""; } if(str.equals("yes")) {response.setContentType("application/msword;charset=GB2312"); } %> </FONT> </BODY> </HTML>
2.Response重定向:在某些情况下,当响应客户时,需要将客户重新引导至另一个页面,可以使用response的sendRedirect(URL)方法实现客户的重定向。飞飞Asp技术乐.园 例如:response2.jsp:
<html> <head><title>Where to go</title></head> <body> <% String address = request.getParameter("where"); if(address!=null){ if(address.equals("ChinaWebber")) response.sendRedirect(" http://www.ChinaWebber.com"); else if(address.equals("Yahoo")) response.sendRedirect(" http://www.yahoo.com"); else if(address.equals("Sun")) response.sendRedirect(" http://www.sun.com"); } %> <b>Please select:</b><br> <form action="goto.jsp" method="GET"> <select name="where"> <option value="ChinaWebber" selected>go to ChinaWebber <option value="Sun" > go to Sun <option value="Microsoft" > go to Microsoft </select> <input type="submit" value="go"> </form>
|