一、存放位置:jsp文件一般是存放在WEB-INF文件夹下,这样在外部就不能直接访问了,只能在程序中访问的到。用到的语句是(这里是跳到login.jsp文件):
request.getRequestDispatcher("WEB-INF/login.jsp").forward(request, response);
二、路径的书写
①绝对路径(使用率90%):大部分都是使用绝对路径,比如html文件里面导js包、css包等都需要用到绝对路径,在HBuilder里面写的大多数可能是相对路径,但是在合页面的时候必须要用绝对路径,不然那些jar包就是用不了了。
绝对路径包括内站和外站,格式分别是:
内站:/项目名/资源名 <a href="/CookieAndSession/CookieServlet">给cookie设置值</a>
外站:http://www.baidu.com 协议://主机名/端口号/资源名
②相对路径(使用率几乎为0)
格式:../资源所在的文件夹名称/资源名称
③内部路径
看到forward、errorpage、include都应该想到内部路径。
三、九大内置对象
对象 | 数据结构 |
---|---|
request | HttpServletRequest |
response | HttpServletResponse |
out | JspWriter |
config | HttpServletConfig |
pageContext | PageContext |
session | HttpSession |
application | ServletContext |
1、四大域
①pageContext:
作用范围:一个界面
何时产生:执行jsp打开时
何时销毁:jsp界面关闭
由谁产生:服务器
②request
作用范围:一次请求
何时产生:浏览器发出请求
何时销毁:会话结束
由谁产生:服务器
③session
作用范围:一次会话
何时产生:第一次getSession时
何时销毁:会话结束
由谁产生:服务器
④application
作用范围:整个工程
何时产生:web应用正常启动
何时销毁:web应用正常关闭
由谁产生:服务器
PageContext的作用:
①作为域对象
②控制其他三大域,给其他三大域设置相应的值
pageContext.setAttribute("username",
"aa_page",PageContext.PAGE_SCOPE);
pageContext.setAttribute("username", "bb_request",PageContext.REQUEST_SCOPE);
pageContext.setAttribute("username", "cc_session",PageContext.SESSION_SCOPE);
pageContext.setAttribute("username", "dd_app",PageContext.APPLICATION_SCOPE);
第三个参数是标明为哪个域设置参数
③获取其他八大内置对象
<%
pageContext.getRequest();
pageContext.getOut();
pageContext.getServletConfig();
%>
④自动在四大域中查找
pageContext.setAttribute("username", "aa_page",PageContext.PAGE_SCOPE);
pageContext.setAttribute("username", "bb_request",PageContext.REQUEST_SCOPE);
pageContext.setAttribute("username", "cc_session",PageContext.SESSION_SCOPE);
pageContext.setAttribute("username", "dd_app",PageContext.APPLICATION_SCOPE);
String a1=(String)pageContext.getAttribute("username");
如果没有标明是在哪个域里面找,它就会从最小的域找起,直到找到能匹配的,如果没找到,就会返回一个null。
application的作用:
①设置、获取全局参数
//首先要获取application
ServletContext app = getServletContext();
String str="test";
ServletContext app = getServletContext();
app.setAttribute("test", str);
②获得项目文件的路径
String realPath = app.getContextPath();
③获得mime type
String type=app.getMimeType();