当前位置: 代码迷 >> 综合 >> JSP内置对象——请求对象request
  详细解决方案

JSP内置对象——请求对象request

热度:76   发布时间:2023-09-27 12:47:07.0

1. 使用方法:

当表单提交某个参数后,(其可以是一个用户名(一串字符串),或者是一组多选框(一个字符串数组))则可以在表的action响应页面通过request取得。

  • 直接使用
    String name = requrst.getParameter("username");
    
  • 取数组
    String yourcities[ ]request.getParameterValues("cities")
    

2.解决NullPointerException异常与中文字符乱码:

  • 首先异常是因为:当没有输入内容反而调用则会“空指针”异常。

    中文乱码问题

  • 更改编码
    request.setCharacterEncoding("UTF-8");
    
  • 获取信息重新编码
    String name = request.getParameter("userName");
    byte b[]= name.getBytes("UTF-8");
    name = new String(b);
    

3.其他常用方法

  • 存值
    request.setAttribute(String name(被赋予的名字),Object obj(符合类型对象))
    
  • 取值、移除
    getAttribute();
    removeAttribute();
  相关解决方案