JSP编程-步步为营
【第一个JSP举例】
header.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<% out.println("welcome调用这个JSP页面"); %>
footer.jsp
<br> </body>
</html>
jsp代码
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%> <%@ include file="/header.jsp" %><%--包含页头 --%> <%!
int i=0;
private int count(){
i++;
return i;
}
%>
<p>调用count输出<%=count() %></p>
<p>调用count输出<%=count() %></p>
<br>
<%@ include file="/footer.jsp" %><%--包含页底 --%>

【概念总结】
language定义使用的脚本语言
contentType定义jsp字符编码和页面响应的MIME类型
pageEncoding jsp页面的字符编码
scriptlet标签
<%! %>可定义全局变量、全局方法、全局类
<% %>可定义局部变量
<%= %>输出
JSP注释
<!-- -->HTML客户端可见
<%-- --%>客户端不可见
//单行注释
/* 多行注释 */
静态包含
<%@ include file="header.html" %>
<%@ include file="footer.jsp" %>
动态包含
<jsp:include page="header.html" />
<jsp:include page="footer.jsp" />
【jsp九大内置对象】
pageContext request response session application config
out page exception
【四大作用域】
Page范围 只在一个页面中保存数据 抽象类
Request范围 只在一个请求中保存数据 接口
Session范围 在一个会话范围中保存数据,仅供单个用户使用
接口
Application范围 在整个服务器上保存数据,所有用户共享
【Page范围】
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body> <%
pageContext.setAttribute("name","名字");
pageContext.setAttribute("age",24);
%> <%
String str1=(String)pageContext.getAttribute("name");
int n=(Integer)pageContext.getAttribute("age");
%>
<p>姓名:<%=str1 %></p>
<p>年龄:<%=n %></p>
</body>
</html>
【Request范围】
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body> <%
request.setAttribute("name","马云");
request.setAttribute("age",23);
request.setAttribute("sex","男");
%>
<jsp:forward page="f_target.jsp"></jsp:forward> </body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body> <%
String str=(String)request.getAttribute("name");
int age=(Integer)request.getAttribute("age");
String sex=(String)request.getAttribute("sex");
%> <p>姓名:<%=str %></p>
<p>年龄:<%=age %></p>
<p>性别: <%=sex %></p> </body>
</html>
【Session范围】
session保存在服务器中,只要页面不关闭,就可以随时取得
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Session</title>
</head>
<body>
<h3>设置Session</h3>
<%
session.setAttribute("bookName","JAVA编程");
session.setAttribute("price",25.5);
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>get Session</title>
</head>
<body> <%
String bookName=(String)session.getAttribute("bookName");
double price=(Double)session.getAttribute("price");
%> <p>图书名称:<%=bookName %></p>
<p>图书价格:<%=price %></p> </body>
</html>
【Application范围】范围更大,浏览器关闭或者换用其他浏览器,也可以访问数据
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Application</title>
</head>
<body> <%
application.setAttribute("bookName","数据库");
application.setAttribute("price",45.85);
%> </body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Application</title>
</head>
<body> <%
String bookName=(String)application.getAttribute("bookName");
double price=(Double)application.getAttribute("price");
%>
<p>图书名称:<%=bookName %></p>
<p>图书价格:<%=price %></p>
</body>
</html>
知识点:
【获取Request的头信息】jsp引入 枚举
输出 name value
<%@ page import="java.util.*" %> <%
Enumeration e=request.getHeaderNames();
while(e.hasMoreElements()){
String name=(String)e.nextElement();
String value=request.getHeader(name);
%>
<p><%=name %> <%=value %></p>
<%
}
%>
【Response】示例:每隔一秒刷新时间
<%
response.setIntHeader("Refresh",1);
Date date=new Date();
%>
<h3>时间</h3>
<%=date.toLocaleString() %>
网页重定向
<h3>网页重定向-客户端跳转</h3>
<%--response.sendRedirect("page4.jsp");--%>
<h3>用户登录post</h3>
<form action="login.jsp" method="post">
用户:<input type="text" name="userName" /><br><br>
密码:<input type="password" name="password" /><br><br>
记住我<input type="checkbox" name="remember" value="remember" /><br><br>
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</form>
login.jsp的代码
<%
String name=request.getParameter("userName");
String pass=request.getParameter("password");
String rem=request.getParameter("remember");
%>
<%=name %>
<%=pass %>
<%=rem %>
post get比较
post放在数据包里-get放在Url后面
get数据量小、不安全
【Cookie示例】用户登录记住用户名和密码
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script>
function resetValue(){
document.getElementById("userName").value="";
document.getElementById("password").value="";
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test response</title>
</head>
<body>
<%
String userName=null;
String password=null;
Cookie[] cos=request.getCookies();
Cookie c=null;
for(int i=0;i<cos.length;i++){
c=cos[i];
if(c.getName().equals("nameAndPass")){
//out.println("保存的用户名和密码"+c.getValue ());
userName=c.getValue().split("-")[0];
password=c.getValue().split("-")[1];
}
}
if(userName==null){userName="";}
if(password==null){password="";}
%>
<h3>用户登录post</h3>
<form action="login.jsp" method="post">
用户:<input type="text" name="userName" id="userName" value="<%=userName %>"/><br><br>
密码:<input type="password" name="password" id="password" value="<%=password %>"/><br><br>
记住我<input type="checkbox" name="remember" value="remember" /><br><br>
<input type="submit" value="提交" />
<input type="button" value="重置" onclick="resetValue()" />
</form> </body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="javax.servlet.http.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>login.jsp</title>
</head>
<body> <%
String name=request.getParameter("userName");
String pass=request.getParameter("password");
String rem=request.getParameter("remember");
if("remember".equals(rem)){
Cookie c=new Cookie("nameAndPass",name +"-"+pass);
c.setMaxAge(24*60*60*7);//记录一个星期
response.addCookie(c);//保存Cookie
}
response.sendRedirect("page5_response.jsp");
%> </body>
</html>
【out对象】
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test out</title>
</head>
<body>
<%
int total=out.getBufferSize();//总缓存大小
int noUse=out.getRemaining();//未使用缓存大小
int u=total-noUse;
%>
<p>总缓存大小:<%=total %></p>
<p>未使用缓存大小:<%=noUse %></p>
<p>已使用缓存大小:<%=u %></p>
</body>
</html>
【config对象】
config.getInitParameter();//获得xml里的初始化参数param
【exception异常处理】
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page errorPage="ex2.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Exception</title>
</head>
<body>
<%
int a=1;
int b=0;
out.println(a/b);
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page isErrorPage="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>isErrorPage</title>
</head>
<body> <%
if(exception!=null){
String str=exception.getMessage();
out.println("错误提示信息:<br>");
out.println(str);
}
%> </body>
</html>
此处需要在浏览器中运行,不在tomcat里运行

【pageContext对象】页面上下文
<%
pageContext.setAttribute("name0","pageInfo");
request.setAttribute("name1","requestInfo");
session.setAttribute("name2","sessionInfo");
application.setAttribute("name3","applicationInfo"); out.println("->"+pageContext.getAttribute("name0")+"<br>");
out.println("->"+pageContext.getRequest().getAttribute("name1")+"<br>");
out.println("->"+pageContext.getSession().getAttribute("name2")+"<br>");
out.println("->"+session.getServletContext().getAttribute("name3")+"<br>");
%>
JSP编程-步步为营的更多相关文章
- jsp编程
jsp编程 jsp的实质和工作原理 注释 九大内置对象 jsp文件的结构解析 脚本语法 jsp指令 jsp动作元素 EL表达式 jsp的实质和工作原理: jsp (全称:Java Server Pa ...
- 利用JSP编程技术实现一个简单的购物车程序
实验二 JSP编程 一.实验目的1. 掌握JSP指令的使用方法:2. 掌握JSP动作的使用方法:3. 掌握JSP内置对象的使用方法:4. 掌握JavaBean的编程技术及使用方法:5. 掌握JSP ...
- Java EE JSP编程基础
一.JSP编程介绍 JSP是实现普通静态HTML和动态HTML混合编码的技术,可以说是Servlet的一种变形,相比Servlet它更像普通的Web页面.JSP在第一次运行时会花费很长时间,原因在与其 ...
- Servlet编程-步步为营
[环境]eclipse j2ee;Tomcat 7.0; [模型1] package com.zhiqi; import ...; public class TestServlet extends H ...
- Jave工具——servlet+jsp编程中mysql数据库连接及操作通用工具类
该工具类是在JavaWeb中连接mysql所用到的通用工具类 该类用于Java+Servlet的编程中,方便数据库的操作,连接,获取其列表值.下面是这个数据库操作类的通用方法,基本上能够用于类里面只含 ...
- Java Servlet JSP编程(一)
最近想学学java编程,java现在的应用还是挺广泛的,有必要学习一下. # index.jsp <%@ page language="java" contentType=& ...
- Servlet,jsp,JSP技术 ,JSP编程
一.Servlet 思考 1 浏览器可以直接打开JAVA/class文件吗? 不可以 2浏览器可以打开HTML.JS 文件吗? 可以 3 JAVA程序可以生成HTML文件吗?可以的,用IO流. 4 ...
- JSP编程技术5-购物车的实现-session会话对象
首先十分感谢大家对我的文章的支持,我是个刚刚才找到自己方向的在校大学生,当然我的知识和能力有限,眼下正在努力恶补学习中.当我看到自己首次发表到CSDN首页的文章才几个小时阅读量就超过了100时,对我来 ...
- JSP编程中常用的JavaScript技术(转载)
1.<tronMouseOver=this.style.backgroundColor=’#FFFFFF’ onMouseOut=this.style.backgroundColor=”> ...
随机推荐
- CodeForces - 407A
Triangle Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Sta ...
- Html标签<a>的target属性
target属性规定了在何处打开超链接的文档. 如果在一个 <a> 标签内包含一个 target 属性,浏览器将会载入和显示用这个标签的 href 属性命名的.名称与这个目标吻合的框架或者 ...
- Telnet命令检测远程主机上的端口是否开启
ping命令不能检测端口,只能检测你和相应IP是否能连通. 本地虚拟机里安装了一个Ubuntu,使用Putty连接22端口操作时提示失败,于是查看对应端口是否开启. Windows下要检测远程主机上的 ...
- 《Java程序性能优化》学习笔记 JVM和并发优化
第四章 并行程序优化 1.非阻塞同步避免了基于锁的同步的缺陷,无锁算法没有锁竞争带来的系统开销,也没有线程间频繁调度带来的开销.CAS算法:包含3个参数CAS(v,e,n).V表示要更新的变量,E表示 ...
- php变量的几种写法
一.最简单的 $str = 'Hello World!'; 二.来个变种 $str = 'good'; $good = 'test'; $test = 'Hello World!'; echo $$$ ...
- 十六进制数'\0x'和'\x'有什么区别?(转)
区别不大,都是把数按16进制输出. \0x:当输出的数转换为16进制只有1位时,在前面补0,如 0a,其它情况按照实际情况输出. \x:按照输出数转换为16进制的实际位数输出. 此外,小写x和大写X也 ...
- Effective C++笔记:构造/析构/赋值运算
条款05:了解C++默默编写并调用哪些函数 默认构造函数.拷贝构造函数.拷贝赋值函数.析构函数构成了一个类的脊梁,只有良好的处理这些函数的定义才能保证类的设计良好性. 当我们没有人为的定义上面的几个函 ...
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环
D. Dividing Kingdom II Long time ago, there was a great kingdom and it was being ruled by The Grea ...
- Codeforces Round #352 (Div. 2) C. Recycling Bottles 贪心
C. Recycling Bottles It was recycling day in Kekoland. To celebrate it Adil and Bera went to Centr ...
- stack+DFS ZOJ 1004 Anagrams by Stack
题目传送门 /* stack 容器的应用: 要求字典序升序输出,所以先搜索入栈的 然后逐个判断是否满足答案,若不满足,回溯继续搜索,输出所有符合的结果 */ #include <cstdio&g ...