JSP内部对象详解

铅笔科技

铅笔科技

2016-02-19 19:29

关注图老师设计创意栏目可以让大家能更好的了解电脑,知道有关于电脑的更多有趣教程,今天给大家分享JSP内部对象详解教程,希望对大家能有一点小小的帮助。

  1.1 application
  *在jsp中获得application对象
  如:getServletContext().setAttribute("counter",new mycount.Counter());
  如:jsp:useBean scope="application" id="counter" class="mycounter.Counter"/
  *在jsp中处理On Application Start和On Session Start事件的方法
  使用HttpSessionBindingListener类.
  添加session:
  session.putValue("bingdings.listener",new MyListener(getServletContext());
  定义MyListener类:
  import javax.servlet.http.*;
  import javax.servlet.*;
  public class MyListener implements HttpSessionBindingListener{
  ServletContext context;
  public MyListener(ServletContext context){
  this.context=context;
  }
  public void valueBound(HttpSessionBindingEvent event){
  System.out.println("valuebound:someone just bound my listener to a session!");
  }
  public void valueUnbound(HttpSessionBindingEvent event){
  System.out.println("valueunbound:someone just unbound my listener!");
  }
  }

  1.2 request
  *获取一个正在运行时的jsp/servlet文件的绝对url地址
  Stringf file=request.getRequestURL();
  if(requet.getQueryString()!=null{
  file+='?'+request.getqueryString();
  }
  URL reconstructedURL=new URL(request.getScheme(),request.getServerName(),request.getServerPort(),file);
  out.println(reconstructedURL.toString());
  *获取客户端通过哪一个url访问本页面
  String callPage=request.getHeader("Referer");
  *获取当前脚本在当疥文件系统中的真实路径
  request.getRealPath(request.getServletPath());
  *判断多个submit中的一个
  input type=submit name="sub" value="up"
  input type=submit name="sub" value="down"
  在jsp中使用request.getParameter("sub");就可分辨

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/webkaifa/)

  1.3 response
  *网页重定向之三方法
  (1)response.sendRedirect(url);
  (2)%response.setStatus(HttpServletResponse.SC_MOVED_PREMANENTLY);
  String nowloc="/newpath/index.htm";
  response.setHeader("Location",newloc);%
  (3)jsp:forward page="/newpage.jsp"/
  注意上法只能在任何输出还没有发送到客户端之前使用这种方法
  *禁用缓存
  %response.setHeader("Cache-Control","no-store");
  response.setDateHeader("Expires",0);%

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/webkaifa/)

  1.4 session
  *存活时间
  %session.setMaxInactiveInterval(300);%
  *注销
  session.invalidate();

  1.5 exception
  *在jsp页面中处理Servlet的错误
  protected void sendErrorRedirect(HttpServletRequest request,
  HttpServletResponse response,String errorPageURL,Throwable e)
  throws ServletException,IOException{
  request.setAttibute("javax.servlet.jsp.jspException",e);
  getServletConfig().getServletContext();
  getRequestDispatcher(errorPageURL).forward(request,response);
  }
  public void doPost(HttpServletRequest request,HttpServletResponse response){
  try{
  //
  }
  catch(Exception e){try{
  sendErrorRedirect(request,response,"/jsp/ErrPage.jsp",e);
  }catch(Exception e){e.printStackTrace();}
  }
  }
  *在jsp页面中输出错误的stacktrace
  (1)
  %@ page isErrorPage="true%
  %
  out.println("pre");
  printWriter pw=response.getWriter();
  exception.printStackTrace(pw);
  out.println("/pre");
  %
  (2)
  %@ page isErrorPage="true%
  pre
  %
  exception.printStackTrace(new PrintWriter(out));
  %
  /pre

  1.6 Cookie
  *设置cookie
  %
  Cookie mycookie=new Cookie("aName","aValue");
  response.addCookie(mycookie);
  //mycookie.setMaxAge(time);
  %

展开更多 50%)
分享

猜你喜欢

JSP内部对象详解

Web开发
JSP内部对象详解

CNET JSP BASIC:JSP的内部对象

Java JAVA基础
CNET JSP BASIC:JSP的内部对象

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

JSP开发入门(四)--JSP的内部对象

Java JAVA基础
JSP开发入门(四)--JSP的内部对象

JavaScript常用内部对象

Web开发
JavaScript常用内部对象

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

jsp的内部方法使用

Web开发
jsp的内部方法使用

ASP内置对象 Request对象 详解

ASP
ASP内置对象 Request对象 详解

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

使用技巧:Java实现随机验证码功能实例

使用技巧:Java实现随机验证码功能实例

ASP二进制流方法隐藏图片文件真实地址

ASP二进制流方法隐藏图片文件真实地址
下拉加载更多内容 ↓