一、request内置对象
1.request内置对象是使用最多的一个对象,其主要作用是用来接受客户端发送而来的请求信息,例如:请求的参数、发送的头信息等都属于客户端发来的信息,request是javax.servlet.http.HttpServletRequest接口的实例化对象,表示此对象主要是应用在HTTP协议上
2.对应接口:public interface HttpServletRequest extends ServletRequest
现在使用的主要是HTTP协议,但是以后可能出现更多的新的协议,要想支持这种新的协议则肯定要继承ServletRequest接口,所以即使ServletRequest只有一个子接口HttpServletRequest,也没有将二者合并。
二、乱码解决
由于request属于接收客户端的参数,会有其默认的语言编码,主要是由于浏览器的默认编码是UTF-8,而中文编码是GBK,由于这两种编码不一样,造成乱码。
方法一、要解决乱码问题,必须使用resquest提供的同意设置编码的方法。
void setCharacterEncoding(String env) throws UnsupportedEncodingException
request.setCharacterEncoding("GBK");
方法二、利用字节数组设置
String message = new String(request.getParameter("info").getBytes("ISO8859-1"));
三、接收参数
当表当传递的是一个复选框,复选框实际上就是一个数组,肯定要同时接收数据,则需要用到如下方法完成:
String[] getParameterValues(String name)
样例:
request_1.html
<html>
<head><title>欢迎来到望星空</title></head>
<body>
<form action="request_2.jsp" method="post">
请输入信息:<input type="text" name="info"><br>
兴趣:<input type="checkbox" name="inst" value="跳舞">跳舞
<input type="checkbox" name="inst" value="篮球">篮球
<input type="checkbox" name="inst" value="羽毛球">羽毛球<br>
<input type="submit" value="提交">
</form>
</body>
</html>
request_2.jsp
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>hello</title></head>
<body>
<%
request.setCharacterEncoding("GBK");
String information = (String)request.getParameter("info");
String inst[] = request.getParameterValues("inst");
%>
<h2>信息:<%=information%></h2>
<h2>兴趣:
<%
//如果输入为空,则会出现NullPointerException
if(inst != null){
for(int i = 0; i < inst.length; i++){
%>
<%=inst[i]%>
<%
}
}
%>
</h2>
</body>
</html>
四、URL地址重写
在WEB开发中,所有的参数不一定非要由表单传递过来,也可以使用地址重写的方式进行传递,地址重写格式如下:
动态页面地址?参数名称1=参数内容1&参数2=参数2&……
样例:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>欢迎来到望星空</title></head>
<body>
<%
String name = (String)request.getParameter("name");
String inst = (String)request.getParameter("inst");
%>
<h2><%=name%></h2>
<h2><%=inst%></h2>
</body>
</html>
下面通过地址重写的方式完成参数的传递:
http://localhost/joywy/basic/info.jsp?name=Joywy&inst=basketball
表单的提交方式有两种:
1.post提交:提交后的地址栏不会附加目标地址的内容,是只能用在表单上的一种提交形式。
2.get提交:提交后的地址栏是会改变的,而且会使用地址重写的方式完成。会受到地址栏长度的限制。
如果用get提交则需要用到
public Enumeration getParameterNames()这个方法
样例:
page1.html
<html>
<head><title>欢迎来到望星空</title></head>
<body>
<form action="page2.jsp" method="post">
姓名:<input type="text" name="uname"><br>
性别:<input type="radio" name="sex" value="男" checked>男
<input type="radio" name="sex" value="女">女<br>
城市:<select name="city">
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="安徽">安徽</option>
</select><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="DOTA">DOTA<br>
自我介绍:<textarea cols="30" rows="3" name="note"></textarea><br>
<input type="hidden" name="uid" value="1">
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
<body>
</html>
page2.jsp
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head><title>hello</title></head>
<body>
<%
request.setCharacterEncoding("GBK");
Enumeration enu = request.getParameterNames();
%>
<table border="1">
<tr>
<td>参数名称</td>
<td>参数内容</td>
</tr>
<%
while(enu.hasMoreElements()){
String paramName = (String) enu.nextElement();
%>
<tr>
<td><%=paramName%></td>
<td>
<%
if(paramName.startsWith("**")){
String paramValue[] = request.getParameterValues(paramName);
for(int i = 0; i < paramValue.length; i++){
%>
<%=paramValue[i]%>、
<%
}
}else {
String paramValue = request.getParameter(paramName);
%>
<%=paramValue%>
<%
}
%>
</td>
</tr>
<%
}
%>
</table>
</table>
</body>
</html>
五、显示全部的头信息
JAVA的WEB开发使用的是HTTP协议,主要的操作就是基于请求和回应,但是在请求和回应的同时也会包含一些其他的信息(例如:客户端的IP、Cookie、语言等),这些信息就称为头信息。
要想取得头信息的名称可以直接通过request内置对象的getHeaderNames(),而要想取出每一个头信息的内容,则需要使用getHeader()方法。
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head><title>欢迎来到望星空</title></head>
<body>
<%
Enumeration enu = request.getHeaderNames();
while(enu.hasMoreElements()){
String headerName = (String)enu.nextElement();
String headerValue = request.getHeader(headerName);
%>
<h5><%=headerName%>---><%=headerValue%></h5>
<%
}
%>
</body>
</html>
六、角色验证:
相当于在Tomcat上增加一些新的用户,若想增加用户则需要修改配置文件(Tomcat安装目录下conf下的tomcat-users.xml文件)
<user username="admin" password="admin" roles="administrator"/>
配置完成后重新启动服务器,则会增加一下内容:
<role rolename="myuser"/>
除此之外还需要配置WEB-INF/web.xml文件:
<web-resource-collection>
<web-resource-name>
</web-resource-name>
<url-pattern>
</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>mldnuser</role-name>
<role-name>admin</role-name>
</auth-constraint>
<security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Registered Users</realm-name>
</login-config>
<security-role>
<role-name>mldnuser</role-name>
</security-role>
<security-role>
<role-name>admin</role-name>
</security-role>
登录验证:
request.isUserInRole("admin")
七、其他操作
通过request对象获取客户端的IP地址,请求方式、访问的路径、上下文的名称等。
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>欢迎来到望星空</title></head>
<body>
<img src="<%=request.getContextPath()%>/images/android.jpg">
<%
String method = request.getMethod();
String ip = request.getRemoteAddr();
String path = request.getServletPath();
String contextPath = request.getContextPath();
%>
<h3>请求方式:<%=method%></h3>
<h3>IP地址:<%=ip%></h3>
<h3>访问路径:<%=path%></h3>
<h3>上下文名称:<%=contextPath%></h3>
</body>
</html>
重点:1.直接输入地址属于get提交
2.通过request中的getContextPath()方法可以解决路径的跳转问题
3.使用getAttribute()之前一定会存在setAttribute()的设置操作,否则无法取得,使用getParameter()表示接收参数,参数的来源有以下几种:
(1)表单提交
(2)地址重写
(3)通过<jsp:include>、<jsp:forward>传递而来的参数
getParameter()是无法接收setAttribute()设置的内容。