1.什么是内置对象:
由web容器创建,用户可以直接使用的对象
2.常见的内置对象有哪些:
out 向浏览器输出内容
request 封装客户请求
response 封装客户端响应
session 封装会话
application 封装应用程序信息
3.out对象的常用方法:
void print(Object obj); 输出任意内容
void println(Object obj); 输出并且多一个空格
表达式翻译后就是out.print(表达式);
4.request对象的常见方法:
String getParameter(String name);
采集客户端提交的单个数据,其中name是参数名称
<input type="text" name="name" ...>
http://localhost/myweb/addServlet?name=name ...
String[] getParameterVales(String name);
采集客户端提交的一组数据,其中name是参数名称
<input type="checkbox" name="name" ...>
<input type="checkbox" name="name" ...>
http://localhost/myweb/addServlet?name=name ...
void setAttribute(String key,Object value);
按照指定的key把value存入request作用域
Object getAttribute(String key);
按照指定的key获取存入过的request作用域的值
RequestDispatcher getRequestDispatcher(String path);
获取一个转发对象
void forward(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response)
常用写法:
request.getRequestDispatcher("转发路径").forward(request,response);
void setCharacterEncoding(String encoding);
设置请求对象的编码
String getScheme();
获取请求的协议
String getServerName();
获取主机地址
int getServerPort();
返回主机端口
String getRequestURI();
获取资源路径
String getQueryString();
获取查询参数
String getContextPath();
获取应用上下文名称
//getRealPath方法有两种,分别由request对象和applicaltion对象调用,作用相同,推荐使用第二种
String getRealPath(String path);
获取相对路径的绝对路径
getRealPath("/WEB-INF/db")
ServletContext getServletContext();
获取ServletContext对象,即应用上下文对象(application对象)
String getRealPath(String path);
Cookie[] getCookies();
获取所有Cookies对象
5.解决http请求的中文乱码:
1.设置request对象的编码:
void setCharacterEncoding(String encoding);
这段代码必须在采集数据之前
2.通过String类的构造方法转换编码
String result=new String(byte[],String encoding);
例如:
第一步:采集数据
String getParameter();
第二步:转换编码(处理post比较好)
String resut=new String(username.getBytes("ISO-8859-1"),"utf-8");
3.修改conf/server.xml文件的<Connector>标签,添加URIEncoding属性(处理get比较好)
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
URIEncoding="UTF-8"
redirectPort="8443" />
6.如何实现转发以及转发的特点:
实现转发:
通过RequestDispatcher对象的转发方法
void forward(ServletRequest request,ServletResponse response);
完整代码:
request.getRequestDispatcher("转发地址").forward(request,response);
转发的特点:
a.客户端提交的数据不会丢失
b.存储在request或以上作用域的数据不会丢失(即通过setAttribute存储的数据会保存下来)
c.客户端url地址不会发生改变
7.在request作用域存取数据:
存入数据:
request.setAttribute(String key,Object value);
如:
String username=request.getParameter("username");
request.setAttribute("username",username);
取出数据:
Object getAttribute(String key);
如:
String username=(String)request.getAttribute("username");
注意:
a.可以把任何类型的数据存入request作用域
b.取出数据时,往往需要数据类型转换
c.存储在request作用域的数据,只在转发范围内有效,一旦经历一次重定向,数据就消失
8.response对象的常见方法
void setCharacterEncoding(String encoding);
设置响应对象的编码
void sendRedirect(String path);
重定向到新的页面
void addCookie(Cookie cookie);
把cookie对象添加到响应对象
9.实现重定向以及重定向的特点是什么:
实现重定向:
void sendRedirect(String path);
重定向的特点:
a.客户端提交的数据会丢失
b.存储在session或以上作用域的数据不会丢失(request通过setAttribute存储的数据不会保存下来)
c.客户端url地址会发生改变
10.session对象的常见方法:
String getId()
获取session的ID
void setAttribute(String name,Object value);
按指定的name存入任意数据类型的value
Object getAttribute(String name);
按指定的name取出value
void removeAttribute(String name);
按指定的name删除value,删除之后当前name和value都消失
void setMaxInactiveInterval(int time);
设置session的过期时间,以秒为单位,默认值为1800
int getMaxInactiveInterval()
获取session的过期时间
void invalidate();
销毁session
11.什么是会话:
客户端浏览器和服务器的一系列连贯的交互,称之为一次会话
在java Web中,通过session表示会话对象
12.会话失效的几种可能:
1.会话主动失效:
通过调用session.invalidate();
有些情况下调用以下方法也可导致失效
void removeAttribute(String name);(传入key,可以通过getId获取)
2.被动失效
session超时
3.伪失效
更换或者重启浏览器
13.application对象的常见用法:
1.void setAttribute(String name,Object value);
按指定的name存入任意数据类型的value
2.Object getAttribute(String name);
按照指定的name获取application域的指定值
3.void removeAttribute(String name);
按照指定的name移除指定值和key
4.String getRealPath(String path);
获取相对路径的资源的绝对路径
5.String getContextPath();
获取应用上下文名称
14.jsp的作用域:
由小到大:
pageContext 当前页面
request request作用域
session 一次会话
application 整个服务器
15.Cookie的常见方法:
构造方法:
Cookie(String name,String value);
注意:Cookie不是jsp内置对象,所以需要new创建它的对象
成员方法:
String getName();
获取写入Cookie的键
String getValue();
获取写入Cookie的值
int getMaxAge();
获取Cookie过期时间,默认值是-1,永不过期
void setMaxAge(int time);
设置Cookie过期时间
16.cookie和session的相同和不同:
相同点:
都能存取数据
不同点:
存储位置不同
cookie:浏览器
session:服务器内部
存储数据的类型不同
cookie:只能存储字符串类型的数据
session:能存储任意类型数据
过期时间不同·
cookie:默认不过期,永久保存,值为-1
session:默认1800s,即30min
安全性不同
cookie:一般存储不重要信息
session:一般存储重要信息
注意:cookie和session协同工作,共同实现会话跟踪
由web容器创建,用户可以直接使用的对象
2.常见的内置对象有哪些:
out 向浏览器输出内容
request 封装客户请求
response 封装客户端响应
session 封装会话
application 封装应用程序信息
3.out对象的常用方法:
void print(Object obj); 输出任意内容
void println(Object obj); 输出并且多一个空格
表达式翻译后就是out.print(表达式);
4.request对象的常见方法:
String getParameter(String name);
采集客户端提交的单个数据,其中name是参数名称
<input type="text" name="name" ...>
http://localhost/myweb/addServlet?name=name ...
String[] getParameterVales(String name);
采集客户端提交的一组数据,其中name是参数名称
<input type="checkbox" name="name" ...>
<input type="checkbox" name="name" ...>
http://localhost/myweb/addServlet?name=name ...
void setAttribute(String key,Object value);
按照指定的key把value存入request作用域
Object getAttribute(String key);
按照指定的key获取存入过的request作用域的值
RequestDispatcher getRequestDispatcher(String path);
获取一个转发对象
void forward(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response)
常用写法:
request.getRequestDispatcher("转发路径").forward(request,response);
void setCharacterEncoding(String encoding);
设置请求对象的编码
String getScheme();
获取请求的协议
String getServerName();
获取主机地址
int getServerPort();
返回主机端口
String getRequestURI();
获取资源路径
String getQueryString();
获取查询参数
String getContextPath();
获取应用上下文名称
//getRealPath方法有两种,分别由request对象和applicaltion对象调用,作用相同,推荐使用第二种
String getRealPath(String path);
获取相对路径的绝对路径
getRealPath("/WEB-INF/db")
ServletContext getServletContext();
获取ServletContext对象,即应用上下文对象(application对象)
String getRealPath(String path);
Cookie[] getCookies();
获取所有Cookies对象
5.解决http请求的中文乱码:
1.设置request对象的编码:
void setCharacterEncoding(String encoding);
这段代码必须在采集数据之前
2.通过String类的构造方法转换编码
String result=new String(byte[],String encoding);
例如:
第一步:采集数据
String getParameter();
第二步:转换编码(处理post比较好)
String resut=new String(username.getBytes("ISO-8859-1"),"utf-8");
3.修改conf/server.xml文件的<Connector>标签,添加URIEncoding属性(处理get比较好)
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
URIEncoding="UTF-8"
redirectPort="8443" />
6.如何实现转发以及转发的特点:
实现转发:
通过RequestDispatcher对象的转发方法
void forward(ServletRequest request,ServletResponse response);
完整代码:
request.getRequestDispatcher("转发地址").forward(request,response);
转发的特点:
a.客户端提交的数据不会丢失
b.存储在request或以上作用域的数据不会丢失(即通过setAttribute存储的数据会保存下来)
c.客户端url地址不会发生改变
7.在request作用域存取数据:
存入数据:
request.setAttribute(String key,Object value);
如:
String username=request.getParameter("username");
request.setAttribute("username",username);
取出数据:
Object getAttribute(String key);
如:
String username=(String)request.getAttribute("username");
注意:
a.可以把任何类型的数据存入request作用域
b.取出数据时,往往需要数据类型转换
c.存储在request作用域的数据,只在转发范围内有效,一旦经历一次重定向,数据就消失
8.response对象的常见方法
void setCharacterEncoding(String encoding);
设置响应对象的编码
void sendRedirect(String path);
重定向到新的页面
void addCookie(Cookie cookie);
把cookie对象添加到响应对象
9.实现重定向以及重定向的特点是什么:
实现重定向:
void sendRedirect(String path);
重定向的特点:
a.客户端提交的数据会丢失
b.存储在session或以上作用域的数据不会丢失(request通过setAttribute存储的数据不会保存下来)
c.客户端url地址会发生改变
10.session对象的常见方法:
String getId()
获取session的ID
void setAttribute(String name,Object value);
按指定的name存入任意数据类型的value
Object getAttribute(String name);
按指定的name取出value
void removeAttribute(String name);
按指定的name删除value,删除之后当前name和value都消失
void setMaxInactiveInterval(int time);
设置session的过期时间,以秒为单位,默认值为1800
int getMaxInactiveInterval()
获取session的过期时间
void invalidate();
销毁session
11.什么是会话:
客户端浏览器和服务器的一系列连贯的交互,称之为一次会话
在java Web中,通过session表示会话对象
12.会话失效的几种可能:
1.会话主动失效:
通过调用session.invalidate();
有些情况下调用以下方法也可导致失效
void removeAttribute(String name);(传入key,可以通过getId获取)
2.被动失效
session超时
3.伪失效
更换或者重启浏览器
13.application对象的常见用法:
1.void setAttribute(String name,Object value);
按指定的name存入任意数据类型的value
2.Object getAttribute(String name);
按照指定的name获取application域的指定值
3.void removeAttribute(String name);
按照指定的name移除指定值和key
4.String getRealPath(String path);
获取相对路径的资源的绝对路径
5.String getContextPath();
获取应用上下文名称
14.jsp的作用域:
由小到大:
pageContext 当前页面
request request作用域
session 一次会话
application 整个服务器
15.Cookie的常见方法:
构造方法:
Cookie(String name,String value);
注意:Cookie不是jsp内置对象,所以需要new创建它的对象
成员方法:
String getName();
获取写入Cookie的键
String getValue();
获取写入Cookie的值
int getMaxAge();
获取Cookie过期时间,默认值是-1,永不过期
void setMaxAge(int time);
设置Cookie过期时间
16.cookie和session的相同和不同:
相同点:
都能存取数据
不同点:
存储位置不同
cookie:浏览器
session:服务器内部
存储数据的类型不同
cookie:只能存储字符串类型的数据
session:能存储任意类型数据
过期时间不同·
cookie:默认不过期,永久保存,值为-1
session:默认1800s,即30min
安全性不同
cookie:一般存储不重要信息
session:一般存储重要信息
注意:cookie和session协同工作,共同实现会话跟踪