17.11.29,web学习第十五天,还有一年,努力吧青年Request
来源:互联网 发布:大数据经济学 编辑:程序博客网 时间:2024/06/16 04:18
15.Request
1.service方法结束,方法返回后tomcat内核会去response缓冲区中获得获取内容并组装成一个http响应。
2.请求头里的User-Agent是可以判断是什么浏览器的!
3.URI所有的资源地址,URL通常为网络地址。URI>URL
4.Request获取请求行的资源:
getMethod();获得客户端的请求方式。
getRequestURI();
getRequestURL();
String getContextPath(); 获得web应用的名称。
String getQueryString()-get 提交url地址后的参数的字符串。如果该参数是post提交,返回null
结果:
POST
/Request/Request---项目名加后面的资源地址
http://localhost:8080/Request/Request -- 拿到的是http路径
null
/Request
注意:当表单post提交到当前项目的某个Servlet下时action=”直接填urlpattern的名不用在前面加项目名“。
5.remote远程操作,遥控器,遥远的。
request获得客户端的ip地址,request.getRemoteAddr();
6.request获得请求头:
String str= Request.getHeader(“name”);
Enumration ea=Request.getHeaderNames();
Enumration ea=request.getHeaders(String name);
Long l=request.getDataHeader(String name);
int getIntHeader(String name)
头里面有一个referer的头,此记录着来访的地址
7.request获得请求体:请求体是通过post表单操作提交过来的。
例:username=zhangsan&password=123......checkbox....
String getParameter(String name); 重要
String[] getParameterValues(“name”);
Enumration e=getParameterNames(); 这和Iterator相似,它有
while(e.hasMoreElements()){
e.nextElement();
}
这个使用方法!
Map<String,String[]> getParameterMap(); 重要 有方法:
Map<String String[]> map=request.getParaterMap();
Set<Entry<String,String[]>> set=map.entrySet();
for(Entry<String,Steing[]> ss:set){
String key=ss.getKey();
String [] value=ss.getValue();
} ------>这是直接封装成了map整体对象了
总结:对于7.这一整套方法,对于GET提交来说也适用!
8.request的其他功能 它是一个域对象
Request转发和重定向的区别。转发是被动接收数据。
方法:setAttribute(String name,Object c)
getAttribute(String name);
removeAttribute(String name);
Request域的作用范围:一次请求中。
要了解上述功能首先了解:转发:
RequestDispatcher dp=request.getRequestDispatcher(“/servlet”);
dp.forward(request,reponse);
语法:
获得请求转发器:
RequestDispatcher getRequestDispatcher(String path);
通过转发器对象转发:
forword(ServletRequest sr,ServletResponse sr);
6.servletContext与request的生命周期的比较:
servletContext:创建:服务器启动时创建
销毁:服务器关闭时销毁。作用范围:整个web应用。
Request:创建:访问时创建 销毁:访问结束时销毁;
作用范围:一次请求中。
转发和重定向的区别:
重定向两次请求,转发一次请求。
重定向地址栏发生变化,转发的地址栏不发生变化。
重定向可以访问外部资源,转发只可以访问内部资源、
转发的性能优先重定向。
记住:重定向得写地址,因为访问外部资源
7.Demo完成用户注册+乱码的解决:
表达post乱码的解决:
request.setCharacterEncoding(“UTF-8”);
Get方法上述操作不适用。可按以下流程操作:
源(UTF-8)字符串--UTF-8编码--iso-8859-1解码 按这个顺序反着来就行!
此案例注意事项:
使用BeanUtiles(jar包)工具类的populate(jb,map<String,String[]>),将request.getparameterMap()得到的Map集合封装到jb对象中,其中jb对象的字段名必须得和map中的键名相同。
//设置request的编码---只适合post方式
request.setCharacterEncoding("UTF-8");//get方式乱码解决//String username = request.getParameter("username");//乱码//先用iso8859-1编码 在使用utf-8解码//username = new String(username.getBytes("iso8859-1"),"UTF-8"); //1、获取数据//String username = request.getParameter("username");//System.out.println(username);//String password = request.getParameter("password");//.....//2、将散装的封装到javaBean//User user = new User();//user.setUsername(username);//user.setPassword(password);//使用BeanUtils进行自动映射封装//BeanUtils工作原理:将map中的数据 根据key与实体的属性的对应关系封装//只要key的名字与实体的属性 的名字一样 就自动封装到实体中Map<String, String[]> properties = request.getParameterMap();User user = new User();try {BeanUtils.populate(user, properties);} catch (IllegalAccessException | InvocationTargetException e) {e.printStackTrace();} //现在这个位置 user对象已经封装好了//手动封装uid----uuid---随机不重复的字符串32位--java代码生成后是36位user.setUid(UUID.randomUUID().toString()); //3、将参数传递给一个业务操作方法try {regist(user);} catch (SQLException e) {e.printStackTrace();}//4、认为注册成功跳转到登录页面response.sendRedirect(request.getContextPath()+"/login.jsp");} //注册的方法public void regist(User user) throws SQLException{//操作数据库QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());String sql = "insert into user values(?,?,?,?,?,?,?,?,?,?)";runner.update(sql,user.getUid(),user.getUsername(),user.getPassword(),user.getName(),user.getEmail(),null,user.getBirthday(),user.getSex(),null,null);
8.uuid是32位,java方法中36位,加了4杠。
9.对案例二、完成登录错误信息的回显总结:
思路:获取表单里的用户名和密码然后在servlet中去数据中查找是否有此条记录,有(重定向)则跳转到主页面,无(转发给jsp页面)则给出提示信息。
Jsp页面显示提示信息:<%=request.getAttribute(“name”)%>
Servlet获取数据记得要setCharacterEncoding(“xxx”);
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String password = request.getParameter("password");
System.out.println(name);
System.out.println(password);
DataSource cpds= new ComboPooledDataSource();
QueryRunner qr=new QueryRunner(cpds);
String sql="select * from user where name=? and password=?";
try {
javaBean jb= qr.query(sql, new BeanHandler<javaBean>(javaBean.class),name,password);
if (jb!=null) {
System.out.println("不为空");
response.sendRedirect("http://localhost:8080/Request/");
}else{
request.setAttribute("name", "账号或密码错误,请重新输入!");
RequestDispatcher requestDispatcher = request.getRequestDispatcher("login.jsp");
requestDispatcher.forward(request, response);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
13.servletRequest,RequestDispatcher都是接口
- 17.11.29,web学习第十五天,还有一年,努力吧青年Request
- 17.11.16,web学习第五天,还有一年,努力吧青年!
- 17.11.23,web学习第十天,还有一年,努力吧青年
- 17.11.24,web学习第十一天,还有一年,努力吧青年
- 17.11.27,web学习第十三天,还有一年,努力吧青年Servlet
- 17.12.13,web学习第二十五天,还有一年,努力吧青年动态代理
- 17.12.03,web学习第十七天,还有一年,努力吧青年JSP
- 17.12.04,web学习第十八天,还有一年,努力吧青年EL和JSTL
- 17.12.05,web学习第十九天,还有一年,努力吧青年事务mysql
- 17.11.11,web学习第一天,还有一年,努力吧青年
- 17.11.12,web学习第二天,还有一年,努力吧青年!
- 17.11.13,web学习第三天,还有一年,努力吧青年!
- 17.11.14,web学习第四天,还有一年,努力吧青年!
- 17.11.15,web学习第四天,还有一年,努力吧青年!
- 17.11.18,web学习第六天,还有一年,努力吧青年
- 17.11.20,web学习第七天,还有一年,努力吧青年
- 17.11.21,web学习第八天,还有一年,努力吧青年
- 17.11.22,web学习第九天,还有一年,努力吧青年
- A轮、B轮、C轮融资、VC、PE、IPO概念解析
- PAT (Basic Level) Practise (中文)1040. 有几个PAT(25)
- 请求转发和重定向
- LINUX下做JAVA程序启动脚本(优化可执行Raspbian Debian9U1)
- c++中的auto和for循环
- 17.11.29,web学习第十五天,还有一年,努力吧青年Request
- 编码
- nginx反向代理实践
- TOMCAT环境变量配置
- 用 Object.create实现类式继承
- 获取jQuery对象 Uncaught ReferenceError: jQuery is not defined(…) 的解决办法
- 这是一个转型AI的励志故事,从非科班到拿下竞赛一等奖
- C和指针之动态内存分配之(编写calloc函数,函数内部使用malloc函数来获取内存)
- Hexo + GitHub搭建个人博客 --- Standard Edition