javaweb学习--jsp

 阅读电子书《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>

 

posted @ 2018-09-12 20:50  倾听-静轩水月  阅读(292)  评论(0编辑  收藏  举报