JSP内置对象request、response(四)
来源:互联网 发布:通达信软件怎么用 编辑:程序博客网 时间:2024/06/11 04:37
(一)request对象
1.作用:使用request对象接收请求参数
2.request对象方法:https://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/http/HttpServletRequest.html
3.使用getParammeter()和getParammeterValues()获取请求参数
4.获取网页请求参数
<%@ 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> <center> 实例演示 <%--获取name和address请求参数值 --%> <% String name=request.getParameter("name"); String address=request.getParameter("address"); %> <p> 欢迎 <%=name %>来到 <%=address %> </p> </center></body></html>
地址栏输入:http://localhost/Project/index.jsp?name=kaina&address=NewYork
输出 :
5.request获取表单请求参数
实例演示:
<%@ 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>表单收集数据</title></head><body> <form action="requestPage.jsp" method="post" > 用户名:<br/> <input type="text" name="name"><hr/> 男 <input type="radio" name="gender" value="男"> 女 <input type="radio" name="gender" value="女"><hr/> 喜欢的颜色:<br/> 红:<input type="checkbox" name="color" value="红"> 绿:<input type="checkbox" name="color" value="绿"> 黄:<input type="checkbox" name="color" value="黄"><hr/> 来自的国家:<br/> <select name="country"> <option value="中国">中国</option> <option value="美国">美国</option> <option value="英国">英国</option> </select> <hr/> <input type="submit" value="提交"> <input type="reset" value="重置"> </form></body></html>---------- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ page import="java.util.*" %> <!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>requestPage</title></head><body> <% //1.获取所有请求头的名称 Enumeration<String> headerNames=request.getHeaderNames(); //2.遍历所有请求头名称对应的值 while(headerNames.hasMoreElements()){ //3.获取每一个请求头名称 String headerName=headerNames.nextElement(); //4.打印每一个请求头名称的值 out.print(headerName+"-->"+request.getHeader(headerName)+"<br/>"); } out.print("<hr/>"); //5.设置字符编码 request.setCharacterEncoding("utf-8"); //6.获取表单收集的数值 String name=request.getParameter("name"); String gender=request.getParameter("gender"); //7.使用getParameterValues()获得字符串数组 String[] color=request.getParameterValues("color"); String country=request.getParameter("country"); %> 您的名字:<%=name %><hr/> 您的性别 :<%=gender %><hr/> 您喜欢的颜色:<%for(String c:color){ out.print(c+" "); } %> <hr/> 您来自的国家 <%=country %><hr/> </body></html>
输出:
6.解决请求参数的中文乱码问题
request.setCharacterEncoding("utf-8");
7.通过request对象传递数据
说明:
1.使用setAttribute()方法保存数据
2.使用getAtribute()方法获取数据
3.使用removeAttribute()方法删除保存的数据
4.使用getAttributeNames()获取保存数据的属性名
(二)Response对象
1.作用:当用户发出HTTP请求时,服务器对客户端做出响应,此时需要使用response对象。
2.response常用方法:https://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/http/HttpServletResponse.html
3.reponse对象实现页面重定向
注意:
1.地址栏URL改变
2.无法访问原来的请求参数
实例演示:
<%@ 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> <center> 跳转页面到谷歌主页 <%response.sendRedirect("https://www.google.com "); %> </center></body></html>
4.response对象实现网页定时刷新并跳转
response.setHeader("Refresh","3;URL=http://www.baidu.com");
5.解决response中文乱码 输出问题
方法.1//1.使用utf-8编码response.setCharacterEncoding("utf-8");//2.通知浏览器使用utf-8解码response.setHeader("text/html;charset=utf-8");
方法.response.setContentType("text/html;charset=utf-8");
- JSP内置对象request、response(四)
- JSP内置对象之request和response
- JSP内置对象(1)----request、response
- JSP内置对象:request和response
- JSP内置对象(1)----request、response
- JSP内置对象之out request response
- jsp的内置对象request和response
- JSP内置对象:request和response
- 简简单单---- (四)jsp内置对象request
- JSP内置对象的使用 request,response,session(一)
- JSP九大内置对象之【request和response】
- JSP 学习内置对象:request response session。。。。。的常用方法
- JSP九大内置对象之request和response
- JSP内置对象request,response,重定向与转发
- 【JSP笔记】内置对象(一)request,response
- java web之路 : jsp request/response内置对象
- JSP内置对象----response
- JSP内置对象----response
- AlertDialog
- 冒泡程序的优化问题
- javax.persistence.entitynotfoundexception怎么解决
- CIFAR-10和CIFAR-100数据集读取显示
- 内核中的kmalloc函数详解
- JSP内置对象request、response(四)
- 【java基础:net】基于TCP的客户端与服务端进行字母大小写转换的Demonstration
- Android ShapeButton
- 【MyBatis】mybatis快速入门(二)
- Sklearn-RandomForest随机森林参数及实例
- EAST: An Efficient and Accurate Scene Text Detector安装
- C++基类指针与派生成员
- Nexpose部署教程
- MiniUI的多表整合crud