request对象常用方法

版权声明:本文为博主原创文章,转载请标注,谢谢 https://blog.csdn.net/qq_43462019/article/details/86584144
request的常用方法
方法名称 说明
String getParameter(String name) 根据表单组件名称获取提交数据
String[]getParameterValues(String name) 获取表单组件对应多个值时的请求数据
void setCharacterEncoding(String charset) 指定每个请求的编码
RequestDispatcher getRequestDispatcher(String path) 返回一个RequestDispatcher对象,该对象的forward()方法用于转发请求

1.jsp页面本身的编码

一旦一种编码格式已经指定,另外一种编码格式如果不进行指定的话,默认都采用已经指定的编码格式

pageEncoding:页面本身的编码

contentType:浏览器渲染页面的时候采用的编码格式

2.浏览器渲染页面采用的编码 

utf-8

3.服务器保存数据采用的编码(request)

ISO-8859-1编码来保存数据的

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="NewFile.jsp" method="post"> //或者get
	<table>
		<tr>
			<td>用戶名</td>
			<td><input type = "text" name="username"></td>
		</tr>
		<tr>
			<td>密码</td>
			<td><input type = "password" name="password"></td>
		</tr>
		<tr>
			<td>信息来源</td>
			<td>
				<input type = "checkBox" name="channel" value="报刊">报刊
				<input type = "checkBox" name="channel" value="朋友推荐">朋友推荐
				<input type = "checkBox" name="channel" value="电视">电视
				<input type = "checkBox" name="channel" value="网络">网络
			</td>
		</tr>
		<tr>
			<td colspan="2"><input type="submit" value="提交"></td>
		</tr>
	</table>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		request.setCharacterEncoding("utf-8");
		//设置的是request内部保存数据的格式 不包括url
		//1.在request获取出来的数据编码
		//2.改变url编码
		String userName = request.getParameter("username");
		String password = request.getParameter("password");
		String channels[] = request.getParameterValues("channel");
		String userNamestr = new String(userName.getBytes("ISO-8859-1"),"utf-8") ;
	%>

	<table>
		<caption>您输入的注册信息</caption>
		<tr>
			<td>用户名</td>
			<td><%=userName%></td>  //post使用此语句
			<td><%=userNamestr%></td>  //get使用此语句
		</tr>
		<tr>
			<td>密码</td>
			<td><%=password%></td>
		</tr>
		<tr>
			<td>信息来源</td>
			<td>
				<%
					for (String channel : channels) {
						out.print(channel);
					}
				%>
			</td>
		</tr>
	</table>
</body>
</html>

 request内置对象用来处理客服端请求

request常用的方法有

1.getParameter

2.getParameterValues

3.setCharacterEncoding

pageEncoding用来指定jsp本身的编码,而contentType用来指定的是浏览器渲染采用的编码格式

Tomcat默认采用ISO-8859-1对字符进行解码

猜你喜欢

转载自blog.csdn.net/qq_43462019/article/details/86584144
今日推荐