JSP内置对象之--request对象

一、乱码解决

void setCharacterEncoding(java.lang.String env)
                          throws java.io.UnsupportedEncodingException

二、接收参数

表单定义,传递各种参数:request_demo_01.html
<html>
<head>
	<title>表单定义,传递各种参数</title>
</head>
<body>
	<form action = "request_demo_01.jsp" method = "post">
		姓名:<input type = "text" name = "uname"><br>
		兴趣:	<input type = "checkbox" name = "inst" value = "跳舞">跳舞		
				<input type = "checkbox" name = "inst" value = "唱歌">唱歌
				<input type = "checkbox" name = "inst" value = "编程">编程
				<input type = "checkbox" name = "inst" value = "游泳">游泳
				<input type = "checkbox" name = "inst" value = "旅游">旅游
				<input type = "hidden" name = "id" value = "3"> 
				<br><input	type ="submit" value = "提交">
				<input type = "reset" value = "重置">
	</form>
</body>
</html>
接收参数:request_demo_01.jsp
<%@ page contentType = "text/html" pageEncoding = "GBK"%>
<html>
<head>
	<title>接收参数</title>
<head>

<body>
	<%
		request.setCharacterEncoding("GBK");
		String name = request.getParameter("uname");
		String id = request.getParameter("id");
		String inst[] = request.getParameterValues("inst");
	%>
		<h3>编号:<%=id%></h3>
		<h3>姓名:<%=name%></h3>
	
<h3>兴趣:
<%
	if(inst!=null){	
%>
	<%
		for (int i = 0; i < inst.length; i++){
	%>
			<%=inst[i]%>、
	<%
		}
	%>
</h3>
	<%
	}	
	%>
</body>
</html>

JSP内置对象之--request对象_第1张图片

JSP内置对象之--request对象_第2张图片

注意问题:

1、接收内容操作第1步:中文乱码问题

2、重置按钮(reset) VS 按钮 (button)
VS 提交(submit)


3、java.lang.String[] getParameterValues(java.lang.String name)
注意判断空数组,否则会出现:NullPointerException异常



你可能感兴趣的:(编程,jsp,String,input,button,旅游)