您的位置:首页 > 产品设计 > UI/UE

JSP引擎的内置对象Request对象来获取客户提交的信息

2017-11-30 21:25 302 查看
Example3_4.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Request对象</title>

</head>

<body>
<form action="tree2.jsp" method="post">
<input type="text" name="username" /> <input type="submit" value="提交"
name="submit" />
</form>

</body>
</html>

tree2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<%@ page import="java.util.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>
<br>
<span style="color: blue"> 客户使用的协议是: <%
String protocol = request.getProtocol();
out.println("<span style='color:red'>" + protocol + "</span>");
%>
</span>
<br>
<span style="color: blue">获取接收客户提交信息的页面: <%
String path = request.getServletPath();
out.println("<span style='color:red'>" + path + "</span>");
%></span>
<br>
<span style="color: blue">接收客户提交信息的长度: <%
int length = request.getContentLength();
out.println("<span style='color:red'>" + length + "</span>");
%></span>
<br>
<span style="color: blue">客户提交信息的方式: <%
String method = request.getMethod();
out.println("<span style='color:red'>" + method + "</span>");
%></span>
<br>
<span style="color: blue">获取http头文件中user-agent的值: <%
String header1 = request.getHeader("User-Agent");
out.println("<span style='color:red'>" + header1 + "</span>");
%></span>
<br>
<span style="color: blue">获取http头文件中accept的值: <%
String header2 = request.getHeader("accept");
out.println("<span style='color:red'>" + header2 + "</span>");
%></span>
<br>
<span style="color: blue">获取http头文件中host的值: <%
String header3 = request.getHeader("Host");
out.println("<span style='color:red'>" + header3 + "</span>");
%></span>
<br>
<span style="color: blue">获取http头文件中accept-encoding的值: <%
String header4 = request.getHeader("accept-encoding");
out.println("<span style='color:red'>" + header4 + "</span>");
%></span>
<br>
<span style="color: blue">获取客户的IP地址: <%
String IP = request.getRemoteAddr();
out.println("<span style='color:red'>" + IP + "</span>");
%></span>
<br>
<span style="color: blue">获取客户机的名称: <%
String clientName = request.getRemoteHost();
out.println("<span style='color:red'>" + clientName + "</span>");
%></span>
<br>
<span style="color: blue">获取服务器的名称: <%
String serverName = request.getServerName();
out.println("<span style='color:red'>" + serverName + "</span>");
%></span>
<br>
<span style="color: blue">获取服务器的端口: <%
int serverPort = request.getServerPort();
out.println("<span style='color:red'>" + serverPort + "</span>");
%></span>
<br>
<span style="color: blue">获取客户端提交的所有参数的名字: <%
Enumeration e = request.getParameterNames();
while (e.hasMoreElements()) {
String s = (String) e.nextElement();
out.println("<span style='color:red'>" + s + "</span>");
}
%></span>
<br>
<span style="color: blue">获取头名字的一个枚举: <%
Enumeration enum_headed = request.getHeaderNames();
while (enum_headed.hasMoreElements()) {
String s = (String) enum_headed.nextElement();
out.println("<span style='color:red'>" + s + "</span>");
}
%></span>
4000
;
<br>
<span style="color: blue">获取头文件中指定头名字的全部值的一个枚举: <%
Enumeration enum_headedValues = request.getHeaders("cookie");
while (enum_headedValues.hasMoreElements()) {
String s = (String) enum_headedValues.nextElement();
out.println("<span style='color:red'>" + s + "</span>");
}
%></span>
<br>
<span style="color: blue">按钮的名字: <%
String buttonName = request.getParameter("submit");
//以独特的字节编码格式读入到一个字节数组内存
//再将该字节数组转换成字符串
%></span>
<br><%=buttonName%>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息