代码:
动态包含:
index_dynamic.jsp
head1.jsp
静态包含:
index1.jsp
head1.jsp
结果:
index_caculate.jsp
<body>
<form action="run_caculate.jsp">
<tr>
<td>
请输入数字:
td>
<td>
<input type="text" name="number" value="<%=request.getParameter("number")%>"/>
td>
tr>
<tr>
<td>
立方后数值:
td>
<td>
<input type="text" name="cubed_number" value="<%=request.getParameter("rt_number")%>"/>
td>
tr>
<tr>
<input align="center" type="submit" value="运算"/>
tr>
form>
</body>
run_caculate.jsp
<body>
<%
String str_number = request.getParameter("number");
double float_number = Double.valueOf(str_number);
double cute_number = float_number * float_number * float_number;
%>
<jsp:forward page="index_caculate.jsp">
<jsp:param name="rt_number" value="<%=cute_number%>"/>
jsp:forward>
body>
结果:
forward.jsp
<body bgcolor=blue>
<h1 align="center">forward页面跳转h1><br>
<jsp:forward page="/forforward.jsp">
<jsp:param name="A" value="A"/>
<jsp:param name="B" value='<%=request.getParameter("A")%>'/>
<jsp:param name="C" value="C"/>
jsp:forward>
body>
forforward.jsp
<body>
<h1 align="center">forforward页面h1>br>
<%=request.getParameter("A")%><br>
<%=request.getParameter("B")%><br>
<%=request.getParameter("C")%><br>
body>
sendredirect.jsp
<body bgcolor=red>
<h1 align="center">sendredirect页面跳转h1><br>
<%response.sendRedirect("sendsendredirect.jsp");%>
body>
sendsendredirect.jsp
<body>
<h1 align="center">sendsendredirect页面h1>br>
body>
结果:
login_with_cookie.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page language="java" isErrorPage="true" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
if("POST".equals(request.getMethod()))
{
Cookie cookieUsername = new Cookie("username",request.getParameter("username"));
Cookie visittimesCookie = new Cookie("visitTimes","0");
response.addCookie(cookieUsername);
response.addCookie(visittimesCookie);
response.sendRedirect(request.getContextPath()+"/cookie.jsp");
return;
}
%>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>带有cookie保存登录次数的登录页面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">
head>
<body>
<div align="center" style="margin:10px;">
<fieldset>
<legend>登录legend>
<form action="login_with_cookie.jsp" method="post">
<table>
<tr>
<td>td>
<td>
<%
if(exception!=null)
{
%>
<span style="color:red"><%=exception.getMessage()%>span>
<%
}
%>
td>
tr>
<tr>
<td>账号td>
<td>
<input type="text" name="username" style="width:200px"/>
td>
tr>
<tr>
<td>密码td>
<td>
<input type="password" name="password" style="width:200px"/>
td>
tr>
<tr>
<td>td>
<td>
<input type="submit" value="登 录" class="button"/>
td>
tr>
table>
form>
fieldset>
div>
body>
html>
cookie.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page language="java" errorPage="login_with_cookie.jsp" %>
<%
request.setCharacterEncoding("utf-8");
String username = "";
int visitTimes = 0;
//所有的cookie
Cookie[] cookies = request.getCookies();
//遍历所有的cookie寻找 用户账号信息与登录次数信息
for(int i = 0; cookies!=null&&i
{
Cookie cookie = cookies[i];
if("username".equals(cookie.getName()))
{
username = cookie.getValue();
}
else if("visitTimes".equals(cookie.getName()))
{
visitTimes = Integer.parseInt(cookie.getValue());
cookie.setValue(""+ ++visitTimes);
}
}
//如果没有找到Cookie中保存的用户名,则转到登录页面
if(username == null || username.trim().equals(""))
{
throw new Exception("您还没有登录.请先登录");
}
//修改Cookie,更新用户的访问次数
Cookie visitTimesCookie = new Cookie("visitTimes",Integer.toString(visitTimes));
response.addCookie(visitTimesCookie);
%>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>cookietitle>
<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">
head>
<body>
<div align="center" method="post">
<fieldset>
<legend>登录信息legend>
<form action="login_with_cookie.jsp" method="post">
<table>
<tr>
<td>你的账号:td>
<td><%=username%>td>
tr>
<tr>
<td>登录次数:td>
<td><%=visitTimes%>td>
tr>
<tr>
<td>td>
<td>
<input type="button" value="刷 新" onclick="location='<%=request.getRequestURI()%>?ts=' + new Date().getTime();" class="button"/>
td>
tr>
table>
form>
fieldset>
div>
body>
html>
login_with_session.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>login_with_session登录界面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">
head>
<body>
<h1 align="center">login_with_session登录界面h1>
<jsp:forward page="/onLineUser.jsp">
<jsp:param name="username" value="<%=request.getRemotePort()%>"/>
jsp:forward>
body>
html>
onLineUser.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.util.*"%>
<%@ page import="mySession.onLineUser"%>
<jsp:useBean id="onlineuser" class="mySession.onLineUser" scope="application"/>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'onLineUser.jsp' starting pagetitle>
<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">
head>
<body>
<center>
<% session = request.getSession(false);%>
<%
String username = request.getParameter("username");
if(onlineuser.existUser(username))
{
out.println("用户" + username + "已经登录");
}
else
{
session.setMaxInactiveInterval(50);
session.setAttribute(username, onlineuser);
out.println("欢迎新用户: + username + "登录到系统");
}
out.println("
当前在线用户人数:" + onlineuser.getCount()+"登录到系统!");
Vector vt = onlineuser.getOnLineUser();
Enumeration e = vt.elements();
out.println("再线拥护列表");
out.println(" out.println(" while(e.hasMoreElements()) { out.println(" out.println((String)e.nextElement()+""); out.println(" } out.println("");
"); ");用户名 ");");
%>
<centet>
<p>黄诗博制作p>
<p>p>
<%
out.println("");
%>
centet>
center>
body>
html>
logout.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="mySession.onLineUser,java.util.*" %>
<jsp:useBean id="onlineuser" class="mySession.onLineUser" scope="application"/>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>搞定jsp在线人数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">
head>
<body>
<center>
<h1>登录成功,欢迎您访问Java家h1>
center>
<%
String username = request.getParameter("username");
if(onlineuser.deleteUser(username))
{
out.println(username + "已经退出系统!");
}
else
{
out.println(username + "没有登录到系统!");
}
%>
<center>
<p>黄诗博制作p>
<p>p>
<p><a href="logout.jsp">退出系统a>p>
center>
body>
html>
结果:
用qq浏览器访问
2.session和cookie的区别和联系,cookie主要用于客户端,而session用于服务端。Cookie技术是客户端的解决方案,Cookie就是由服务器发给客户端的特殊信息,而这些信息以文本文件的方式存放在客户端,然后客户端每次向服务器发送请求的时候都会带上这些特殊的信息。Session是另一种记录客户状态的机制,不同的是Cookie保存在客户端浏览器中,而Session保存在服务器上。客户端浏览器访问服务器的时候,服务器把客户端信息以某种形式记录在服务器上。这就是Session。客户端浏览器再次访问时只需要从该Session中查找该客户的状态就可以了。
如果说Cookie机制是通过检查客户身上的“通行证”来确定客户身份的话,那么Session机制就是通过检查服务器上的“客户明细表”来确认客户身份。Session相当于程序在服务器上建立的一份客户档案,客户来访的时候只需要查询客户档案表就可以了。
3.
静态包含: 先合并再翻译
动态包含: 先翻译再合并
4.了解jsp:param和jsp:forward的使用
forward用来跳转,param用来传输数据