一、JSP 本质
JSP 本质是一个Servlet,JSP主要负责与用户交互,将最终的页面呈现给用户,html+css+js的混合文件
二、jsp工作原理
当服务器接收到一个后缀是jsp的请求时,将该请求交给JsP引攣去处理,每一个JSP页面第一次被访问的时候JSP引擎会将它翻译成一个 Servlet文件,再由Web容器调用Servlet完成响应。
三、单纯从开发的角度看,JSP就是在HTML中嵌入Java程序。
1、jsp脚本嵌套:执行java逻辑代码
<body>
<%
String str=abc();
%>
</body>
2、JSP声明:定义java方法
<body>
<%!
public String abc(){
return "hellowored";
}
%>
</body>
3、JSP表达式:把Java对象直接输出到HTML页面中
<body>
<%= str%>
</body>
4、JSP语法
<body>
<%
ArrayList<User> list=new ArrayList<>();
list.add(new User("刘亦菲",18));
list.add(new User("唐嫣",20));
list.add(new User("范爷",58));
%>
<table>
<tr>
<th>姓名</th>
<th>年龄</th>
</tr>
<%
for (int i = 0; i <list.size() ; i++) {
%>
<tr>
<td><%=list.get(i).getName()%></td>
<td><%=list.get(i).getAge()%></td>
</tr>
<%
}
%>
</table>
</body>
四、JSP的9大内置对象
- request:表示一次请求, Httpservletrequest。
常用方法
- String getParameter( String key)获取客户端传来的参数。
- void setAttribute(String key, Object value)通过键值对的形式保存数据。
- Object getAttribute( String key)通过key取出 value。
- Request Dispatcher getrequestdispatcher(String path) 的forward方法用于请求转发。
- Stringl getparameter values0获取客户端传来的多个同名参数
- void set Character Encoding(String charset)指定每个请求的编码
index.jsp页面
<%
//获取参数
String idstr = request.getParameter("id");
//将参数转换成Integer类型
int id = Integer.parseInt(idstr);
//将id封装到requset
request.setAttribute("id",id);
//将封装类容转发到xxx.jsp
request.getRequestDispatcher("xxx.jsp").forward(request,response);
%>
<p>id=<%=id%></p>
xxx.jsp
<%=request.getAttribute("id")%>
=====================================
<%
request.setCharacterEncoding("utf-8");
String[] names=request.getParameterValues("name");
%>
<!--Arrays.toString(String str)取出集合中所有元素,并拼接输出-->
<%=Arrays.toString(names)%>
- response:表示一次应, Httpservletresponse
- sendRedirect( String path)重定向,页面之间的跳转
转发 getrequestDispatcher和重定向 sendredirect的区别
转发是将同一个请求传给下一个页面,重定向是创建一个新的请求传给下一个页面,之前的请求结束生命周期转发:同一个请求在服务器之间传递,地址栏不变,也叫服务器跳转重定向:由客户端发送一次新的请求来访问跳转后的目资源,地址栏改变,也叫客户跳转
如果两个页面之间需要通过 request来传值,则必须使用转发,不能使用重定向
2.ADAS
- pageContext:页面上下文,获取页面信息, PageContext
- session:表示一次会话,保存用户信息, Httpsession
用户会话
服务器无法识别每一次HTP请求的出处(不知道来自于哪个终端),它只会接受到一个请求信号,所以就存在一个问题:将用户的响应发送给其他人,必须有一种技术来让服务器知道请求来自思,这就是会话技术
会话:就是客户端和服务器之间发生的一系列连续的请求和响应的过程
会话状态:指服务器和浏览器在会话过程中产生的状态信息,借助于会话状态,服务器能够把属于同一次会话的一系列请求和响应关联起来。
实现会话有两种方式
- session
session常用的方法:
- String getid() 获取 sessionID
- void setMaxInactiveInterval(int interval) 设置 session的失效时间,单位为秒
- int getMaxInactiveInterval() 取当前 session的失效时间
- void invalidate() 设置 session立即失效
- void setAttribute(String key, Object value) 通过键值对的形式来存储数据
- Object getAttribute( String key) 通过键获取对应的数据
- void removeAttribute(String key) 通过键删除对应的数据
session模拟登入
web.xml内配置默认参数和servlet
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>cn.itcast.web.test.login</servlet-class>
<!--加载默认参数-->
<init-param>
<param-name>myusername</param-name>
<param-value>admin</param-value>
</init-param>
<init-param>
<param-name>mypassword</param-name>
<param-value>123123</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</servlet>
登入页面login.jsp
<body>
<form action="/login" method="post">
<table>
<tr>
<th>账号:</th>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<th>密码:</th>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td><input type="submit" value="登入"></td>
<td><input type="reset" value="重置"></td>
</tr>
</table>
</form>
</body>
逻辑页面Login.java
//登入
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取传递参数
String username = req.getParameter("username");
String password = req.getParameter("password");
//登入一次30分钟内再次打开不需要再次登入
if (username.equals(myusername) && password.equals(mypassword)){
HttpSession session = req.getSession();
//设置 session的失效时间,单位为秒
session.setMaxInactiveInterval(1800);
//将数据储存在session中
session.setAttribute("username",username);
resp.sendRedirect("firstpage.jsp");
}else {
System.out.println("失败");
resp.sendRedirect("login.jsp");
resp.getWriter().write("请确认账号密码");
}
}
首页firstpage.jsp
<body>
<h1>这是首页</h1>
<p>欢迎回来:<%=session.getAttribute("username")%><a href="/login" >退出登入</a></p>
</body>
退出登入
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//开启session
HttpSession session = req.getSession();
//结束会话
session.invalidate();
//重定向到登入页面
resp.sendRedirect("login.jsp");
}
cookie
创建cookie
//创建Cookie
Cookie cookie=new Cookie("name","zhangshang");
//创建Cookie响应回去
response.addCookie(cookie);
获取cookie值
Cookie[] cookies = request.getCookies();
for (Cookie cook:cookies){
System.out.println(cook.getName()+"---"+cook.getValue());
}
cookie的常用方法
- void setmaxage(int age)设置 Cookie的有效时间,单位为秒
- int getmaxageo获取 Cookie的有效时间,默认关闭浏览器结束
- String getname0获取 Cookie的name
- String setvalued)获取 Cookie的 value
代码体现
Cookie[] cookies1 = request.getCookies();
cookies1[0].getMaxAge()
cookies1[0].getName();
cookies1[0].getValue();
session与cookie的区别
session:
- 保存在服务器
- 保存的数据是 object
- 会随看会话的结束而销毁
- 保存重要信息
cookie:
- 保存在浏览器
- 保存的数据是 String
- 可以长期保存在浏览器中,无会话无关
- 保存不重要信息
- application:表示当前Web应用,全局对象,保存所有用户共享信息,Servletconte
- config:当前sP对应的 Servlet的 Servletconfig对象,获取当前 Servlet的信
- out:向浏笕器输出数据, Jspwriter
- page:当前JSP对应的 Serve对象, Servlet
- excerption:表示P页面发生的异常, Exception
五、JSP内置对象的作用域
4大作用域
page request、 session、 application这4个可以作为传输数据的载体存在。
setAttribute getAttributel 方法用来存储数据和获取数据
- page作用域:对应的内置对象是 pageContext
- request作用域:对应的内置对象是 request
- session作用域:对应的内置对象是 session
- application作用域:对应的内置对象是 application
作用域大小
- page<request<session<application
有效范围
- page只在当前页面有效。
- request只在一次请求内有效
- session在一次会话内有效
- application对应整个WEB应用的。
统计网站访问量
<%
//获取当前访问量
Integer count=(Integer) application.getAttribute("count");
if (count==null){
count=1;
application.setAttribute("count",count);
}else{
count++;
application.setAttribute("count",count);
}
%>
你是第<%=application.getAttribute("count")%>位嫖客