阅读电子书《Java Web从入门到精通》密码:461c,学习JavaWeb基础知识。由于本人已有html基础,所以直接略过前面部分,进入jsp学习

jsp页面添加库引用,引入项目文件

引用包<%@ page import="java.util.Date" %>
引用文件<%@ include file="top.jsp" %><%-- 不带编译功能,原页面是什么就是什么 --%>
引用文件<jsp:include page="top.jsp" /><%-- 子页面单独编译 --%>

 页面示例

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<!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 style="margin:0;">
<%@ include file="top.jsp" %><%-- 不带编译功能,原页面是什么就是什么 --%>
<jsp:include page="top.jsp" /><%-- 子页面单独编译 --%>
<center>哈哈</center>
<%
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String today = df.format(date);
%>
当前时间:<%= today %>
<br /><button onclick="location.href='process.jsp';">登录</button>
<%@ include file="copyright.jsp" %>
<jsp:include page="copyright.jsp" />
</body>
</html>

application对象

<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Application</title>
</head>
<body>
<%=application.getInitParameter("url")%>
<br />
<pre><%-- 显现println的换行效果 --%>
<%
Enumeration enema = application.getInitParameterNames(); //获取全部初始化参数
while (enema.hasMoreElements()) {
String name = (String) enema.nextElement(); //获取参数名
String value = application.getInitParameter(name);
out.println(value);
}
%>
</pre>
</body>
</html>

request对象,jsp:forward标签

<body>
<%
try{
int money = 100;
int number = 0;
request.setAttribute("result", money/number);
}catch(Exception e){
request.setAttribute("result", "错误");
}
%>
<jsp:forward page="attribute_deal.jsp" />
</body>

跳转后页面

<body>
<% String message = request.getAttribute("result").toString(); %>
<%= message %>
</body>

config对象(实际开发中不常用)

<body>
<%
config.getServletContext(); //获取Servlet上下文
config.getServletName(); //获取Servlet服务器名
config.getInitParameter(); //获取服务器所有初始参数名称,返回值为java.util.Enumeration对象
config.getInitParameterNames(); //获取服务器中name参数的初始值
%>
</body>

exception对象(实际不常用)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" errorPage="exception_error.jsp"%><!-- 添加errorPage--> <body>
<center>exception.getMessage(); 返回exception对象的异常信息字符串</center>
<br />
<center>exception.getLocalizedmessage(); 返回本地化的异常错误</center>
<br />
<center>exception.fillInStackTrace(); 重写异常错误的栈执行轨迹</center>
<br />
<%
request.setAttribute("price", "12.5 元");
float price = Float.parseFloat(request.getAttribute("price").toString());
%>
</body>

错误提示页面

<body>
错误提示:<%=exception.getMessage() %>
</body>

http使用

<%
//禁用页面缓存
response.setHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", 0);
%>
<%
//response.setHeader("refresh", "10"); //10秒刷新一次
response.setHeader("refresh", "5;URL=login.jsp"); //5秒跳转到登录页
%>

简单登录验证

<body>
<form id="loginForm" action="jsp_practice_validate.jsp">
用户名:<br />
<input type="text" id="username" name="username" style="width: 120px;" /><br />
密   码:<br />
<input type="password" id="pwd" name="pwd" style="" /><br />
<input type="submit" value="登录" />
</form>
</body>

验证页面

<body>
登录信息验证中...
<%
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
if (username.equalsIgnoreCase("admin") && pwd.equals("123456")) {
response.sendRedirect("jsp_practice_success.jsp?username=" + username);
} else {
response.sendRedirect("jsp_practice_error.jsp");
}
%>
</body>

验证成功

<body>
<%= request.getParameter("username") %>登录成功!
</body>

验证失败

<body>
用户名或密码错误....
<%
response.setHeader("refresh", "30;URL=jsp_practice.jsp");
%>
</body>

国际化操作

<body>
<%
java.util.Locale locale = request.getLocale();
String localeStr = locale.toString();
String str = "";
if (locale.equals(java.util.Locale.US)) {
str = "Hello";
}
if (locale.equals(java.util.Locale.CHINA)) {
str = "你好";
}
%>
<%= str %>
</body>

页面输出

<body>
<pre><%-- 显现println的换行效果 --%>
<%
Enumeration enema = application.getInitParameterNames(); //获取全部初始化参数
while (enema.hasMoreElements()) {
String name = (String) enema.nextElement(); //获取参数名
String value = application.getInitParameter(name);
out.println(value);
}
out.clear(); //清除缓冲区中的内容
out.clearBuffer(); //清除当前缓冲区的内容
out.flush(); //刷新流
out.isAutoFlush(); //检测当前缓冲区已满时是自动清空,还是抛出异常
out.getBufferSize(); //获取缓冲区的大小
%>
</pre>
</body>

page对象(实际不常用)

<body>
<%!Object object;%>
<ul>
<li>getClass()返回当前Object的类:<%= page.getClass() %></li>
<li>hashCode()返回该Object的哈希代码:<%= page.hashCode() %></li>
</ul>
</body>

pageContext对象(实际不常用)

<body>
<%
pageContext.forward("login.jsp"); //把页面转发到另一个页面
pageContext.getAttribute("userName"); //获取参数值
pageContext.getAttributeNamesInScope(0); //获取某范围的参数名的集合,返回值为java.utilEnumeration对象
pageContext.getException(); //返回exception对象
pageContext.getRequest(); //返回request对象
pageContext.getResponse(); //返回response对象
pageContext.getSession(); //返回session对象
pageContext.getOut(); //返回out对象
pageContext.getApplication(); //返回application对象
//pageCoutext.setAttribute(""); //为指定范围内的属性设置属性值
//pageContext.removeAttribute(); //删除指定范围内的指定属性
%>
</body>

request对象详细

<body>
<br />客户端提交信息的方式:<%=request.getMethod() %>
<br />使用的协议:<%=request.getProtocol() %>
<br />获取发出请求字符串的客户端地址(URI):<%=request.getRequestURI() %>
<br />获取发出请求字符串的客户端地址(URL):<%=request.getRequestURL() %>
<br />获取提交数据的客户端IP地址:<%=request.getRemoteAddr() %>
<br />获取服务器端口号:<%=request.getServerPort() %>
<br />获取服务器名称:<%=request.getServerName() %>
<br />获取客户端的主机名:<%=request.getRemoteHost() %>
<br />获取客户端所有请求的脚本文件的文件路径:<%=request.getServletPath() %>
<br />获得Http协议定义的文件头信息Host的值:<%=request.getHeader("host") %>
<br />获得Http协议定义的文件头信息User-Agent的值:<%=request.getHeader("user-agent") %>
<br />获得Http协议定义的文件头信息accept-language的值:<%=request.getHeader("accept-language") %>
<br />获得请求文件的绝对路径:<%=request.getRealPath("index.jsp") %>
</body>

session对象

<body>
<%
session.setAttribute("userName", "SessionTest");
String userName = session.getAttribute("userName").toString(); //session.getLastAccessedTime(); //返回客户端最后一次与会话相关的请求时间
//session.getMaxInactiveInterval(); //以秒为单位返回一个会话内两个请求最大时间间隔
//session.setMaxInactiveInterval(3600); //以秒为单位设置session的有效时间 session.removeAttribute("userName");
session.invalidate(); //手动销毁Session
%>
</body>

最新文章

  1. Matlab 周期方波信号傅里叶级数展开
  2. SQL游标(cursor)详细说明及内部循环使用示例
  3. tcp三次握手与四次挥手
  4. hdu2546 01背包
  5. LightOJ1036 A Refining Company(DP)
  6. codeforces B. Simple Molecules 解题报告
  7. mysql DATE_ADD DATE_SUB
  8. poj3525Most Distant Point from the Sea(半平面交)
  9. Centos更新yum packet源
  10. hibernate tool连接oracle生成pojo和xml文件无法查询表解决办法
  11. 每日一词【命令行CMD】
  12. HDU-4627 The Unsolvable Problem 简单数学
  13. Sysstat LINUX工具包址
  14. 【转】BLE 学习记录
  15. NYOJ-括号配对问题 &lt;技巧性的非栈道法&gt;
  16. divide an integer into X parts (as even as possible)
  17. 编码 decode &amp; encode
  18. java实现随机四则运算
  19. word使用
  20. ORACLE报表触发器

热门文章

  1. linux静态ip的设置
  2. 解决Eclipse中无法查看Java源码
  3. Nginx篇--解读nginx配置
  4. Java集合类的那点通俗的认知
  5. php_D3_“简易聊天室 ”实现的关键技术 详解
  6. IDEA搭建Spring Boot项目
  7. Linux - CentOS 登陆密码找回解决方法
  8. [七]基础数据类型之Float详解
  9. Perl系列文章
  10. mac终端代理