[size=large][color=red][b]代码部分[/b][/color][/size]
request请求参数的获取+BeanUtils框架
package cn.itcast.web.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Demo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
/*
* http://localhost:8080/day07/Demo1?name=jack&password=123回车
*/
//StringBuffer sb = request.getRequestURL();
//String s1 = request.getRequestURI();
//String s2 = request.getQueryString();
//System.out.println(sb);
//System.out.println(s1);
//System.out.println(s2);
//String s1 = request.getPathInfo();
//String s2 = request.getRemoteAddr();
//String s3 = request.getRemoteHost();
//int s4 = request.getRemotePort();//1158
String s1 = request.getLocalAddr();
String s2 = request.getLocalName();
int s3 = request.getLocalPort();//8080
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
getRequestURL方法返回客户端发出请求时的完整URL。
getRequestURI方法返回请求行中的资源名部分。
getQueryString 方法返回请求行中的参数部分。
getPathInfo方法返回请求URL中的额外路径信息。额外路径信息是请求URL中的位于Servlet的路径之后和查询参数之前的内容,它以“/”开头。
getRemoteAddr方法返回发出请求的客户机的IP地址。
getRemoteHost方法返回发出请求的客户机的完整主机名。
getRemotePort方法返回客户机所使用的网络端口号。
getLocalAddr方法返回WEB服务器的IP地址。
getLocalName方法返回WEB服务器的主机名。
package cn.itcast.web.request;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Demo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
/*
String host = request.getHeader("host");
System.out.println("host=" + host);
Enumeration enums = request.getHeaders("accept");
while(enums.hasMoreElements()){
String value = (String) enums.nextElement();
System.out.print(value+"|");
}
*/
Enumeration enums1 = request.getHeaderNames();
while(enums1.hasMoreElements()){
String key = (String) enums1.nextElement();//host|accept
Enumeration enum2 = request.getHeaders(key);
}
}
}
package cn.itcast.web.request;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Demo3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
/*
String name = request.getParameter("name");
String password = request.getParameter("password");
String aihao = request.getParameter("aihao");
String[] aihaos = request.getParameterValues("aihao");
if(name!=null && password!=null && aihaos!=null){
response.getWriter().write("name=" + name + "
");
response.getWriter().write("password=" + password + "
");
for(String s : aihaos){
response.getWriter().write("aihao=" + s + "
");
}
}
*/
Enumeration enums = request.getParameterNames();
while(enums.hasMoreElements()){
String name = (String) enums.nextElement();//name/password/aihao
String[] values = request.getParameterValues(name);//"jack","123","abc"
System.out.print(name + "->");
for(String s : values){
System.out.print(s);
}
System.out.println();
}
}
}
package cn.itcast.web.request;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
public class Demo4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
/*
* http://localhost:8080/day08/Demo4?username=jack&password=123
*/
BeanUtils bu = new BeanUtils();
User user = new User();
Enumeration enums = request.getParameterNames();
while(enums.hasMoreElements()){
String name = (String) enums.nextElement();
try {
bu.setProperty(user,name,request.getParameter(name));
} catch (Exception e) {
throw new RuntimeException("参数装配出错");
}
}
System.out.println("用户名:" + user.getUsername());
System.out.println("密码:" + user.getPassword());
}
}
防盗链
package cn.itcast.web.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Demo5 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//取得客户端点击下载功能页面来自于哪里
String referer = request.getHeader("referer");
if(referer!=null && referer.equals("http://localhost:8080/day07/index.html")){
response.setHeader("content-disposition","attachment;filename=aaa.jpg");
}else{
response.sendRedirect("/day07/fail.html");
}
}
}
index.html
下载
index.html
下载
表单输入项数据的获取
package cn.itcast.web.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Demo6 extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String gender = request.getParameter("gender");
String[] aihao = request.getParameterValues("aihao");
String city = request.getParameter("city");
String upfile = request.getParameter("upfile");
String msg = request.getParameter("msg");
String id = request.getParameter("id");
int index = upfile.lastIndexOf("\\");
String filename = upfile.substring(index+1);
System.out.println("username=" + username);
System.out.println("password=" + password);
System.out.println("gender=" + gender);
for(String s : aihao){
System.out.print(s + "|");
}
System.out.println("\ncity=" + city);
System.out.println("upfile=" + filename);
System.out.println("msg=" + msg);
System.out.println("id=" + id);
}
}
POST请求参数的中文乱码问题
package cn.itcast.web.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Demo7 extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//修改[请求体]的中文编码
request.setCharacterEncoding("UTF-8");
//取得表单输入输
String username = request.getParameter("username");
//显示在浏览器
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write("姓名:" + username);
}
}
URL地址的编码(GET方式对中文数据而言)
package cn.itcast.web.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Demo8 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
/*
* 手工将ISO8859-1的编码以UTF-8的方式查询,从而还原中文
*/
byte[] buf = username.getBytes("ISO8859-1");
username = new String(buf,"UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write("姓名:" + username);
}
}
新用户注册
提交
使用转发,模拟MVC设计模式
package cn.itcast.web.request;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//控制器
public class Demo9 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
/*
* http://localhost:8080/day07/Demo8回车
*/
System.out.println("Demo9::doGet()");
//调用模型
Model m = new Model();
boolean flag = m.check();
if(flag){
//转发
RequestDispatcher rd = request.getRequestDispatcher("/view.html");
rd.forward(request, response);
//response.sendRedirect("/day07/view.html");
}
}
}
如果在调用forward方法之前,在Servlet程序中写入的部分内容已经被真正地传送到了客户端,forward方法将抛出IllegalStateException异常。
如果在调用forward方法之前向Servlet引擎的缓冲区(response)中写入了内容,只要写入到缓冲区中的内容还没有被真正输出到客户端,forward方法就可以被正常执行,原来写入到输出缓冲区中的内容将被清空,但是,已写入到HttpServletResponse对象中的响应头字段信息保持有效。
package cn.itcast.web.request;
//模型
public class Model {
public boolean check(){
System.out.println("Model::check()");
return true;
}
}
index.html
视图显示正确
使用包含,将网页信息分为头/体/尾三部份处理
package cn.itcast.web.request;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//负责产生该网站中所有网页的页头
public class HeaderServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.write("首页 ");
out.write("购物车 ");
out.write("用户注册");
out.write("
");
}
}
package cn.itcast.web.request;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//负责产生该网站中所有网页的页尾
public class FooterServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html;charset=UTF-8");
out.write("关于我们 ");
out.write("联系我们 ");
out.write("法律声明");
out.write("
");
}
}
package cn.itcast.web.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//包含网页头
request.getRequestDispatcher("/HeaderServlet").include(request, response);
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write("用户登录");
response.getWriter().write("
");
//包含网页尾
request.getRequestDispatcher("/FooterServlet").include(request, response);
}
}
将信息存储到request容器对象中
package cn.itcast.web.request;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Demo101 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
/*
* http://localhost:8080/day07/Demo101?name=jack回车
*/
String name = request.getParameter("name");
//将信息存储到request容器对象中
request.setAttribute("NAME",name);
//向浏览器输出内容
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.write("中国");
out.flush();
//out.close();
request.getRequestDispatcher("/Demo102").forward(request, response);
//response.sendRedirect("/day07/Demo102");//客户端
}
}
package cn.itcast.web.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Demo102 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//从request容器对象中根据key取得信息
String name = (String) request.getAttribute("NAME");
response.getWriter().write("name=" + name);
}
}
重定向
package cn.itcast.web.response;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
System.out.println(request.hashCode());
//取得表单中填入的用户名和密码
String username = request.getParameter("username");//浏览器->服务器的请求
String password = request.getParameter("password");
if("jack".equals(username) && "123".equals(password)){
//重定向到ShowServlet
response.sendRedirect("/day07/ShowServlet");
//重定向到success.html
//response.sendRedirect("/day07/success.html");
//以下二句代码的组合,与response.sendRedirect()等价
//response.setStatus(302);
//response.setHeader("locotion","/day07/success.html");
}else{
response.sendRedirect("/day07/fail.html");
}
}
}
package cn.itcast.web.response;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShowServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
System.out.println(request.hashCode());
response.setContentType("text/html;charset=UTF-8");//响应头
response.getWriter().write("登录成功");//字符方式显示//响应体
//response.getOutputStream().write("success".getBytes());//字节方式显示
}
}
观察Cookie特证
package cn.itcast.web.cookie;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Demo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//产生Cookie
Cookie cookie = new Cookie("name","jack");
//以秒为单位,设置Cookie的存活时间
cookie.setMaxAge(7*24*60*60);
//写入浏览器的缓存
response.addCookie(cookie);
}
}
注册页面
新用户注册
[size=large][color=red][b]今日总结[/b][/color][/size]
*1 重定向
response.sendRedirect("/day07/需要重定向到的目标资源位置");
例如:response.sendRedirect("/day07/success.html");
例如:response.sendRedirect("/day07/ShowServlet");
浏览器发放二次请求
浏览器地址栏前后变化
web服务器先后产生不同的request对象
错误提示:
405:
请求类型与方法不配对
java.lang.IllegalStateException: getWriter() has already been called for this response
response.getWriter()和response.getOutputStream()只有选用其中一个,不能同时使用
2 response细节
1)正确选用字符输出和字节输出
2)不能同时使用字符输出和字节输出
3)输出时会将响应头和响应体一通输出给浏览器
4)response产生的流对象在web服务器销毁response对象前,后自行关闭
3 StringBuffer
线程安全,即多个线程并发访问该变量时,不会产生错误问题。
可变字符,即适合于大量的增,删,改,查操作,不会产生新的变量。
String(不可变,即适合于查询操作)
StringBuilder(课后自行完成)
一个可变的字符序列。此类提供一个与 StringBuffer 兼容的 API,但不保证同步。该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)。如果可能,建议优先采用该类,因为在大多数实现中,它比 StringBuffer 要快。
http://localhost:8080/day07/Demo1?name=jack&password=123
URL :http://localhost:8080/day07/Demo1
URI :/day07/Demo1
查询字符串:name=jack&password=123
4 request常用API
URL/URI/RemoteAddr/RemotePort/LocaleAddr/LocalePort/...
*5 取得客户端请求头内容和请求体内容
获得客户机请求头
getHeader(string name)方法:String
getHeaders(String name)方法:Enumeration
getHeaderNames()方法:Enumeration
获得客户机请求参数(即,客户端提交的数据)
getParameter()方法:String
getParameterValues(String name)方法:String[]
getParameterNames方法+BeanUtils框架:Enumeration
*6 Request的应用
1)防盗链
2)各种表单输入项数据的获取
*7 POST和GET请求参数的中文处理
1)POST
记住三点一致:
a)
b)request.setCharacterEncoding("UTF-8");修改[请求体]的中文编码,如果内容在请求头,该行代码不起住用
c)response.setContentType("text/html;charset=UTF-8");
2)GET
/*
* 手工将ISO8859-1的编码以UTF-8的方式查询,从而还原中文
*/
byte[] buf = username.getBytes("ISO8859-1");
username = new String(buf,"UTF-8");
">提交
8 request细节
0)请求对象
1)request容器对象/范围对象
request对象的方式比较:
getHeader(String)
取得请求头
getParameter(String)
取得请求参数
getAttribute(String)
取得request容器对象的属性的值
2)在forward()方法不要有任何的输出代码
*9)转发与重定向的区别
1)转发在WEB网站内,重定向在WEB网站内外
2)对于重定向,/表示webapps;对于转发,/表示当前web应用名,即day07
3)重定向地址栏前后变化,转发地址栏前后不变
4)重定向知道web服务器内部变化,转发不知道web服务器内部变化
5)重定向request对象前后不一致,转发request对象前后一致,response也一样
6)代码
重定向:response.sendRedirect()
转发:request.getRequestDispatcher().forward()
10)包含
类似于Java中的方法调用,最终流程还会回到Servlet中
乱码原因:在GET方式下,中文出错,需要手工强制转换,参照7知识点
11)会话的定义
在同一个浏览器的一次或多次向web服务器的请求的过程,叫会话
ServletContext容器对象/范围对象
HttpServletRequest容器对象/范围对象
请求头:Cookie: name=jack
响应头:Set-Cookie: name=jack; Expires=Fri, 11-Mar-2011 09:07:26 GMT
其中path表示;localhost/day07:但凡访问/day07应用下的所有资源,都会带上cookie,如果访问不是/day07下的所有资源,
则不会带cookie
[img]http://dl.iteye.com/upload/attachment/429977/e068e348-0099-35f7-8780-5aa90fcdeb5a.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/429979/a7d338af-536a-303d-98a7-1855d00f7961.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/429981/0296f2c3-4bde-3908-a693-6d5e40489863.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/429983/d546112b-2ac5-364a-9898-127481ba2c89.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/429985/50cb8ce3-2829-3b3f-ac52-f0ffa612476d.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/429987/04d98d5e-8f36-395f-b453-6ed5e189f19e.jpg[/img]