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");就可分辨

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);%>

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);
%>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
request对象JSP中的内置对象之一,代表了客户端发出的HTTP请求。它提供了一系列的方法,用于获取请求的信息。下面是request对象的所有方法详解: 1. void setAttribute(String name, Object obj):将一个对象绑定到request对象上,并使用指定的名称作为键名。 2. Object getAttribute(String name):根据指定的键名,从request对象上获取绑定的对象。如果没有找到,则返回null。 3. Enumeration<String> getAttributeNames():获取所有绑定到request对象上的对象的名称。 4. String getCharacterEncoding():获取请求中的字符编码方式。 5. void setCharacterEncoding(String env):设置请求中的字符编码方式。 6. int getContentLength():获取请求中的数据长度。 7. String getContentType():获取请求中的数据类型。 8. ServletInputStream getInputStream():获取请求中的输入流。 9. String getParameter(String name):获取请求中指定参数的值。 10. Enumeration<String> getParameterNames():获取请求中所有参数的名称。 11. String[] getParameterValues(String name):获取请求中指定参数的所有值。 12. Map<String, String[]> getParameterMap():获取请求中所有参数的Map集合。 13. String getProtocol():获取请求的协议名称和版本号。 14. String getScheme():获取请求的协议名称。 15. String getServerName():获取请求的服务器名称。 16. int getServerPort():获取请求的服务器端口号。 17. BufferedReader getReader():获取请求中的字符输入流。 18. String getRemoteAddr():获取请求的远程IP地址。 19. String getRemoteHost():获取请求的远程主机名。 20. void setAttribute(String name, Object obj, int scope):将一个对象绑定到指定作用域上。 21. Object getAttribute(String name, int scope):根据指定的键名和作用域,从request对象上获取绑定的对象。 22. void removeAttribute(String name):从request对象上移除指定名称的绑定对象。 23. void removeAttribute(String name, int scope):从指定作用域上移除指定名称的绑定对象。 总之,request对象提供了丰富的方法,可以获取请求中的各种信息,为开发者提供了非常方便的手段。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值