欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

javaweb学习--jsp

程序员文章站 2023-11-24 22:38:40
阅读电子书《Java Web从入门到精通》密码:461c,学习JavaWeb基础知识。由于本人已有html基础,所以直接略过前面部分,进入jsp学习 jsp页面添加库引用,引入项目文件 页面示例 application对象 request对象,jsp:forward标签 跳转后页面 config对象 ......

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