(1)编写程序request.jsp,要求先用request.setAttribute("attr","Hello!")设置request对象属性,再利用request对象的相应方法分别获取如下值:attr属性的值、上下文路径、Cookies、Host、ServerName、ServerPort、RemoteAddr、WEB项目的物理路径;并使用out对象的println方法分别输出相应值。
实验代码如下:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>request 主要方法调用示例</title>
</head>
<body>
<font size="2">
request 主要方法调用示例: <br>
<%
request.setAttribute("attr","Hello!");
out.println("attr属性的值为:"+request.getAttribute("attr")+"<br>");
request.removeAttribute("attr");
out.println("上下文路径为:"+request.getContextPath()+ "<br>");
Cookie[] cookies = request.getCookies();
if (cookies != null) {
out.print("Cookies: "+ cookies.toString());
out.println("<br>");
} else {
out.println("没有找到Cookies。<br>");
}
out.println("Host:" + request.getServerName() + ":" + request.getServerPort() +"<br>");
out.println("ServerName:" + request.getServerName() + "<br>");
out.println("ServerPort:" + request.getServerPort() + "<br>");
out.println("RemoteAddr:" + request.getRemoteAddr() + "<br>");
out.println("属性移除操作以后attr属性的值为:"+request.getAttribute("attr")+"<br>");
out.println("WEB项目的物理路径:"+this.getServletContext().getRealPath("")+"<br>");
%>
</font>
</body>
</html>
实验结果如下:
(2)编写用户表单应用程序:分别编写reqform.jsp提交如图用户信息、编写req_recv.jsp接收用户表单传递程序。
reqform.jsp实验代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户表单</title>
<script type="text/javascript">
function hidePassword(){
var password=document.getElementByld("password");
if(password.type=="password"){
password.type="text";
}else{
password.type="password";
}
}
</script>
</head>
<body>
<form action="req_recv.jsp" method="post">
<table align="center" border="1">
<tr> <td>用户名:</td> <td><input type="text" name="name" required></td> </tr>
<tr> <td>密码:</td> <td> <input type="password" id="password" name="password" onclick="hidePassword()" required></td><tr>
<tr>
<td colspan="2" align="center">
<input type="checkbox" name="like" value="骑车" checked>骑自行车
<input type="checkbox" name="like" value="驾车" checked>驾驶小汽车
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="提交">
<input type="submit" value="取消">
</td>
</tr>
</table>
</form>
</body>
</html>
结果:
req_recv.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>接收到的用户表单数据</title>
</head>
<body>
<font size="2">
<%
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
%>
使用request.getParameter("name")取得的值:
<%= name%>
<br>使用request.getParameterNames()取得表单所有参数的值:<br>
<%
String current_param = "";
String current_vaul = "";
request.setCharacterEncoding("UTF-8");
Enumeration params = request.getParameterNames();
while( params.hasMoreElements() ) {
current_param = (String)params.nextElement();
current_vaul=request.getParameter(current_param);
%>
表单参数名称: <%=current_param%>
=<%=current_vaul%><br>
<% }%>
</font>
</body>
</html>
结果: