java中request对象各种方法的使用实例分析
本文实例讲述了java中request对象各种方法的使用。分享给大家供大家参考,具体如下:
request对象是从客户端向服务器端发出请求,包括用户提交的信息以及客户端的一些信息。request对象是javax.servlet.http.HttpServletRequest类的实现实例。
request对象封装了浏览器的请求信息,通过request对象的各种方法可以获取客户端以及用户提交的各项请求信息。
使用request对象获取客户端提交的请求参数的常用方法如下:
1.StringgetParameter(Stringname),获取客户端的参数值,并以字符串形式返回指定参数的值,如果参数不存在则返回空值。用表单、链接或网址栏传递参数时,使用此方法。
例如,获取客户端name的参数值:
Stringname=request.getParameter("name");2.String[]getParameterValues(Stringname),获取单个参数的所有参数值,主要用于获取复选框的值,返回值类型是字符串数组String[]
例如,获取客户端hobby复选框的所有取值:
String[]hobbys=request.getParameterValues("hobby");
if(hobbys!=null)
{
out.println("您的爱好有:");
for(inti=0;i<hobbys.length;i++)
out.println(hobbys[i]);
}
3.voidsetCharacterEncoding(Stringencoding),设置字符编码方式,用来解决传递非英文字符所出现的乱码问题。
例如
request.setCharacterEncoding("UTF-8");实例:使用request对象实现用户注册功能
zhuce.html源代码如下:
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"> <html> <head> <title>个人信息注册</title> <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> <metahttp-equiv="description"content="thisismypage"> <metahttp-equiv="content-type"content="text/html;charset=UTF-8"> <!--<linkrel="stylesheet"type="text/css"href="./styles.css">--> </head> <body> <h1align="center">个人信息注册</h1> <formaction="zhuce.jsp"method="post"> 姓名:<inputtype="text"name="name"><br> 密码:<inputtype="password"name="pwd"><br> 请选择你的职业: <inputtype="radio"name="career"value="农民">农民 <inputtype="radio"name="career"value="工人">工人 <inputtype="radio"name="career"value="学生"checked>学生 <inputtype="radio"name="career"value="教师">教师 <br> 你喜欢的城市: <selectname="city"> <optionvalue="辽宁省">辽宁省</option> <optionvalue="湖北省">湖北省</option> <optionvalue="河南省">河南省</option> <optionvalue="山东省">山东省</option> <optionvalue="江苏省">江苏省</option> <optionvalue="湖南省"selected>湖南省</option> </select> <br> 请选择你的爱好: <inputtype="checkbox"name="hobby"value="旅游">旅游 <inputtype="checkbox"name="hobby"value="看书"checked>看书 <inputtype="checkbox"name="hobby"value="游戏">游戏 <inputtype="checkbox"name="hobby"value="琴棋书画">琴棋书画 <br> 自我介绍: <textareaname="intro">自我介绍</textarea> <br> <inputtype="submit"name="submit"value="提交"> </form> </body> </html>
zhuce.jsp源代码如下:
<%@pagelanguage="java"import="java.util.*"contentType="text/html;charset=UTF-8"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>个人信息注册</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
<!--
<linkrel="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(inti=0;i<hobbys.length;i++)
out.print(hobbys[i]);
}
%>
<br>
自我介绍:<%=request.getParameter("intro")%><br>
</body>
</html>
希望本文所述对大家Java程序设计有所帮助。