JSP 总结
第一天:
jsp(全称Java Server Pages)
优点:
跨平台性、易维护性、易管理性等等
实际上JSP就是指在HTML中嵌入JAVA脚本语言,当用户通过浏览器请求访问Web应用时,使用JSP容器请求对JSP进行处理,然后将生成的页面返回给客户端浏览器进行显示。
工作原理

执行原理

如果没有发生改变则

什么是URL?
URL(Uniform Resoure Locator)统一资源定位器,是用于完整地描述Internet上网页和其他资源的地址的一种表示方法,即网址。
URL的组成

tomcat7.0
lib jar
bin 命令
webapps 应用程序
work 工作目录
temp 临时文件
conf 配置文件
logs 日志文件
tomcat与myeclipse的配置
window
showview
console
db brower
package explorer
servers
JSP的基本语法
指令
<%@ import="java.sql.*"%>
page指令
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
language:java唯一值,表示脚本中使用的编程语言
contentType:设置了内容的类型和静态页面的编码 (告诉浏览器以什么编码显示)
pageEncoding:页面本身的编码格式 (写页面时用的编码格式) include指令 把目标页面的内容包含到当前页面,产生页面叠加以后的输出效果 //相当于将两个页面合并;编译时就包含进来 <%@include file="foot.jsp"%> //可插入任意位置 taglib指令
小脚本
<% %> 写java代码等同在index.java的service方法里写代码
小脚本规则:
1、 你使用的脚本语言决定了脚本小程序的规则;
2、 语句间以分号分隔;
3、 可以使用默认的对象、import进的类、declaration声明的方法和对象以及useBean tag中声明的对象。
声明
<%! %>写Java代码等于在index.java类里,service方法之外写代码
声明规则:
1、 JSP中声明的变量和方法对应于Servlet中的实例方法和实例变量。这些将被同时请求该页面的所有用户所共享;
2、在使用变量或方法前须先定义(不是说声明变量的位置在页面中要处于使用变量的前面,而是指变量不声明不能使用);
3、声明的变量或方法的作用域为当前页面或包含的页面;
4、语句间以分号分隔。
表达式
<%= %>等同<%out.println();%>
表达式规则:
1、你使用的脚本语言决定了脚本小程序的规则;
2、执行的顺序为从左到右;
3、分号不能用于表达式。
注释
jsp<%--内容--%> 不会被翻译
html<!--内容--> 不会显示
java注释:// /* */ 不会被编译
第二天:request && response
request是客户端发向服务器端的请求,response是服务器端对客户端的响应,两者发出对象与接收对象不同

区别


//login.jsp <form action="/demo/test.jsp" method="get"> 帐号:<input type="text" name="userName"/> 密码:<input type="text" name="userPwd"/> 爱好: <input type="checkbox" name="love" value="sport"/>运动 <input type="checkbox" name="love" value="swim"/>游泳 <input type="checkbox" name="love" value="sleep"/>睡觉 <input type="submit" values="登录"/> </form>
//test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ include file = "logins.jsp" %>//包含logins.jsp <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'test.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> </body> </html>
//logins.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'logins.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% String user = request.getParameter("userName"); String pwd = request.getParameter("userPwd"); boolean flag = false; if(user.equals("111")&&pwd.equals("111")){ flag = true; session.setAttribute("userName", user); session.setAttribute("userPwd", pwd); session.setMaxInactiveInterval(2);//回话有效时间 单位是秒 } if(flag){ request.getRequestDispatcher("welcome.jsp").forward(request, response); }else{ response.sendRedirect("/demo/login.jsp"); } %> </body> </html>
//welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'welcome.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% String user = (String)session.getAttribute("userName"); String pwd = (String)session.getAttribute("userPwd"); if(user ==null||pwd==null){ response.sendRedirect("login.jsp");//密码帐号null则重定向到login.jsp } %> <h1><a href = "login.jsp">欢迎</a></h1> </body> </html>

浙公网安备 33010602011771号