使用get或post提交,区别:
使用get方法提交的信息,会在提交的过程中显示在浏览器的地址栏中,post不会
如果为了保密起见,一般用post
举例使用(用Eclipse Jee)
新建一个工程,新建RequestTest.jsp,用来提交表单信息
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>欢迎学习request内置对象</title>
</head>
<body>
<form action = "Request.jsp" method = post>
<table>
<tr>
<td> 用户名: </td>
<td> <input type = text name = name >
</td>
</tr>
<tr>
<td> 密码: </td>
<td> <input type = password name = password >
</td>
</tr>
<tr>
<td> <input type = submit value = 提交数据 >
</td>
</tr>
</table>
</form>
</body>
</html>
运行结果:
创建Request.jsp,用来接受表单提交过去的数据
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>欢迎学习request内置对象</title>
</head>
<body>
<h1> 欢迎学习request内置对象 </h1>
<% String name = request.getParameter("name");
String ps = request.getParameter("password");
out.println ("用户名:" + name);
out.println ("密码:" + ps);%>
</body>
</html>
运行RequestTest.jsp
输入
显示
但如果传递中文字符,如:
中文字符变成乱码
需要对request设置编码
request.jsp
在String name前加一句
request.setCharacterEncoding ("utf-8");
即能成功