当前位置: 移动技术网 > IT编程>开发语言>Java > java中request对象各种方法的使用实例分析

java中request对象各种方法的使用实例分析

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文实例讲述了java中request对象各种方法的使用。分享给大家供大家参考,具体如下:

request对象是从客户端向服务器端发出请求,包括用户提交的信息以及客户端的一些信息。request对象是javax.servlet.http.httpservletrequest类的实现实例。

request对象封装了浏览器的请求信息,通过request对象的各种方法可以获取客户端以及用户提交的各项请求信息。

使用request对象获取客户端提交的请求参数的常用方法如下:

1.string getparameter(string name),获取客户端的参数值,并以字符串形式返回指定参数的值,如果参数不存在则返回空值。用表单、链接或网址栏传递参数时,使用此方法。

例如,获取客户端name的参数值:

复制代码 代码如下:
string name = request.getparameter("name");

2.string[ ] getparametervalues(string name),获取单个参数的所有参数值,主要用于获取复选框的值,返回值类型是字符串数组string[ ]

例如,获取客户端hobby复选框的所有取值:

string[ ] hobbys = request.getparametervalues("hobby");
if(hobbys != null)
{
out.println("您的爱好有:");
for(int i=0;i<hobbys.length;i++)
 out.println(hobbys[i]);
}

3.void setcharacterencoding(string encoding),设置字符编码方式,用来解决传递非英文字符所出现的乱码问题

例如

复制代码 代码如下:
request.setcharacterencoding("utf-8");

实例:使用request对象实现用户注册功能

zhuce.html源代码如下:

<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
 <title>个人信息注册</title>
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="this is my page">
 <meta http-equiv="content-type" content="text/html; charset=utf-8">
 <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
 </head>
 <body>
 <h1 align="center">个人信息注册</h1>
 <form action="zhuce.jsp" method="post">
  姓名:<input type="text" name="name"><br>
  密码:<input type="password" name="pwd"><br>
  请选择你的职业:
  <input type="radio" name="career" value="农民">农民
  <input type="radio" name="career" value="工人">工人
  <input type="radio" name="career" value="学生" checked>学生
  <input type="radio" name="career" value="教师">教师
  <br>
  你喜欢的城市:
  <select name="city">
  <option value="辽宁省">辽宁省</option>
  <option value="湖北省">湖北省</option>
  <option value="河南省">河南省</option>
  <option value="山东省">山东省</option>
  <option value="江苏省">江苏省</option>
  <option value="湖南省" selected>湖南省</option>
  </select>
  <br>
  请选择你的爱好:
  <input type="checkbox" name="hobby" value="旅游">旅游
  <input type="checkbox" name="hobby" value="看书" checked>看书
  <input type="checkbox" name="hobby" value="游戏">游戏
  <input type="checkbox" name="hobby" value="琴棋书画">琴棋书画
  <br>
  自我介绍:
  <textarea name="intro">自我介绍</textarea>
  <br>
  <input type="submit" name="submit" value="提交">
 </form>
 </body>
</html>

zhuce.jsp源代码如下:

<%@ page language="java" import="java.util.*" contenttype="text/html;charset=utf-8"%>
<%
string path = request.getcontextpath();
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
 <base href="<%=basepath%>">
 <title>个人信息注册</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0"> 
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="this is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
 </head>
 <body>
 <%request.setcharacterencoding("utf-8"); %>
  您的姓名是:<%=request.getparameter("name") %><br>
  您的密码是:<%=request.getparameter("pwd") %><br>
  您的职业是:<%=request.getparameter("career") %><br>
  您喜欢的城市是:<%=request.getparameter("city") %><br>
  您的爱好有:<%string[] hobbys = request.getparametervalues("hobby");
  if(hobbys != null)
  {
  out.println("您的爱好有:");
  for(int i=0;i<hobbys.length;i++)
   out.print(hobbys[i]);
  }
  %>
  <br>
  自我介绍:<%=request.getparameter("intro") %><br>
 </body>
</html>

希望本文所述对大家java程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网