本文實例講述了java中request對象各種方法的使用。分享給大家供大家參考,具體如下:
request對象是從客戶端向服務器端發出請求,包括用戶提交的信息以及客戶端的一些信息。request對象是javax.servlet.http.HttpServletRequest類的實現實例。
request對象封裝了瀏覽器的請求信息,通過request對象的各種方法可以獲取客戶端以及用戶提交的各項請求信息。
使用request對象獲取客戶端提交的請求參數的常用方法如下:
1.String getParameter(String name),獲取客戶端的參數值,并以字符串形式返回指定參數的值,如果參數不存在則返回空值。用表單、鏈接或網址欄傳遞參數時,使用此方法。
例如,獲取客戶端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對象實現用戶注冊功能
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程序設計有所幫助。