本文实例讲述了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复选框的所有取值:
1
2
3
4
5
6
7
|
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源代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
<!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源代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<%@ 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程序设计有所帮助。