1.日期显示
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<jsp:useBean id="circle" class="com.hkd.test1.Circle">
<jsp:setProperty property="r" value="10" name="circle"/>
</jsp:useBean>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
圆的周长:<jsp:getProperty property="length" name="circle"/><br/>
圆的面积:<jsp:getProperty property="area" name="circle"/>
</body>
</html>
2.以表格形式显示图书信息
package com.hdk.book;
public class bookinfo {
private String bookid;
private String bookname;
private int count;
public String getBookid() {
return bookid;
}
public void setBookid(String bookid) {
this.bookid = bookid;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public bookinfo(String bookid, String bookname, int count) {
super();
this.bookid = bookid;
this.bookname = bookname;
this.count = count;
}
}
<%@page import="java.util.ArrayList"%>
<%@page import="com.hdk.book.bookinfo"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
bookinfo book1=new bookinfo("1001","java",10);
bookinfo book2=new bookinfo("1002","jsp",20);
bookinfo book3=new bookinfo("1003","javaweb",30);
ArrayList<bookinfo> alist=new ArrayList<bookinfo>();
alist.add(book1);
alist.add(book2);
alist.add(book3);
%>
<table>
<tr><td>图书编号</td><td>图书名称</td><td>借阅数</td></tr>
<%
for(bookinfo b:alist)
{
out.print("<tr>");
out.print("<td>");
out.print(b.getBookid());
out.print("</td>");
out.print("<td>");
out.print(b.getBookname());
out.print("</td>");
out.print("<td>");
out.print(b.getCount());
out.print("</td>");
out.print("</tr>");
}
%>
</table>
</body>
</html>
3.超链接,文本框的形式显示图书信息
<%@page import="java.util.ArrayList"%>
<%@page import="com.hdk.book.bookinfo"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">
function tijiao(s){
var count=document.getElementById(s).value;
alert(count);
}
</script>
<body>
<%
bookinfo book1=new bookinfo("1001","java",10);
bookinfo book2=new bookinfo("1002","jsp",20);
bookinfo book3=new bookinfo("1003","javaweb",30);
ArrayList<bookinfo> alist=new ArrayList<bookinfo>();
alist.add(book1);
alist.add(book2);
alist.add(book3);
%>
<table>
<tr><td>图书编号</td><td>图书名称</td><td>次数</td><td>操作</td></tr>
<%
int i=0;
for(bookinfo b:alist)
{
i++;
%>
<tr><td><a href="#"><%=b.getBookid() %></a></td>
<td><%=b.getBookname() %></td>
<td><input type="text" value="<%=b.getCount() %>" id="count<%=i%>"></td>
<td><input type="button" value="提交" onclick="tijiao('count<%=i%>')"></td>
</tr>
<%
}
%>
</table>
</body>
</html>
4.网页访问次数的显示
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<%!
int num=0;
synchronized void count(){
num++;
}
%>
<body>
<!-- 函数调用 -->
<%count(); %>
您是第<%=num %>个访问者
</body>
</html>
5.使用javabean计算园的周长和面积
package com.hdk.circle;
public class circle {
private double r;
private double length;
private double area;
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public double getLength() {
return 2*Math.PI*r;
}
public void setLength(double length) {
this.length = length;
}
public double getArea() {
return Math.PI*r*r;
}
public void setArea(double area) {
this.area = area;
}
}
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<jsp:useBean id="circle" class="com.hdk.circle.circle">
<jsp:setProperty property="r" name="circle" value="10"/>
</jsp:useBean>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
园的周长:<jsp:getProperty property="length" name="circle"/><br/>
圆的面积:<jsp:getProperty property="area" name="circle"/>
</body>
</html>
6.接收前台页面表单以post方式传过来的数据并显示
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="do前台接收信息并显示.jsp" method="post">
用户名:<input type="text" name="uname" ><br/>
密 码:<input type="text" name="pwd"><br/>
<input type="submit" id="sub" value="提交">
<input type="reset" id="re" value="重置">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String name=request.getParameter("uname");
String pwd=request.getParameter("pwd");
%>
<%=name %><br/>
<%=pwd %>
</body>
</html>
7.get方式(猫狗显示)
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr><td><a href="do猫狗显示.jsp?name=birds&&num=2">鸟类</a></td></tr>
<tr><td><a href="do猫狗显示.jsp?name=cat">猫类</a></td></tr>
<tr><td><a href="do猫狗显示.jsp?name=dog">狗类</a></td></tr>
</table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String name=request.getParameter("name");
String num=request.getParameter("num");
%>
<%=num %><br/>
<%=name %>
</body>
</html>
8.编写注册页面,提交并显示
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="do注册页面.jsp" method="post">
用户名:<input type="text" name="uname" ><br/>
密码:<input type="text" name="upwd" ><br/>
爱好:<input type="checkbox" name="aihao" value="篮球">篮球
<input type="checkbox" name="aihao" value="足球">足球
<input type="checkbox" name="aihao" value="排球">排<br/>
性别:<input type="radio" name="sex" value="男">男
<input type="radio" name="sex" value="女">女<br/>
<input type="submit" name="sub" value="提交">
<input type="reset" name="re" value="重置">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String name=request.getParameter("uname");
String pwd=request.getParameter("upwd");
String sex=request.getParameter("sex");
String aihao[]=request.getParameterValues("aihao");
for(String a:aihao)
{
out.print(a+"<br/>");
}
%>
用户名:<%=name %><br/>
密码:<%=pwd %><br/>
性别:<%=sex %><br/>
</body>
</html>
9.接收数据并显示借阅次数与总借阅次数
<%@page import="com.hdk.book.bookinfo"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
bookinfo book1=new bookinfo("1001","java",10);
bookinfo book2=new bookinfo("1002","jsp",20);
bookinfo book3=new bookinfo("1003","javaweb",30);
ArrayList<bookinfo> alist =new ArrayList<bookinfo>();
alist.add(book1);
alist.add(book2);
alist.add(book3);
%>
<form action="do计算图书总借阅数.jsp" method="post">
<table>
<tr><td>图书编号</td><td>图书名称</td><td>次数</td></tr>
<%
for(bookinfo str:alist)
{
%>
<tr><td><%=str.getBookid() %></td><td><%=str.getBookname() %></td>
<td><input type="text" name="count" value="<%=str.getCount()%>"></td>
</tr>
<% }%>
<tr>
<td>
<input type="submit" name="btn" value="计算">
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String count[]=request.getParameterValues("count");
int sum=0;
if(count!=null)
{
for(String c:count)
{
out.print("这本书的借阅次数为:"+c+"次"+"<br/>");
sum+=Integer.parseInt(c);
}
out.print("总借阅次数为:"+sum);
}
%>
</body>
</html>
10.request获得属性值
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setAttribute("name", "张三");
request.setAttribute("pwd", "12345");
%>
<jsp:forward page="result.jsp"></jsp:forward>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String uname=(String)request.getAttribute("name");
String upwd=(String)request.getAttribute("pwd");
out.print(uname+"<br/>");
out.print(upwd);
%>
</body>
</html>
11.猜数游戏
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
int count=0;
session.setAttribute("count", count);
int snum=(int)(Math.random()*100);
session.setAttribute("snum", snum);
%>
<form action="doinput.jsp" method="post">
随机给您应该0~99的数字<br/>
请您输入您所猜的数字:<input type="text" name="gnum"><br/>
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
int count=(Integer)session.getAttribute("count");
count++;
session.setAttribute("count", count);
int snum=(Integer)session.getAttribute("snum");
String gnum1=request.getParameter("gnum");
int gnum=Integer.parseInt(gnum1);
if(gnum<snum)
{
response.sendRedirect("small.jsp");
}
if(gnum>snum)
{
response.sendRedirect("big.jsp");
}
if(gnum==snum)
{
response.sendRedirect("success.jsp");
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
您猜的数字太小了,请重新猜数<br/>
<form action="doinput.jsp" method="post">
随机给您应该0~99的数字<br/>
请您输入您所猜的数字:<input type="text" name="gnum"><br/>
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
您猜的数字太大了,请重新猜数<br/>
<form action="doinput.jsp" method="post">
随机给您应该0~99的数字<br/>
请您输入您所猜的数字:<input type="text" name="gnum"><br/>
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
long starttime=session.getCreationTime();
long endtime=session.getLastAccessedTime();
long time=(int)((endtime-starttime)/1000);
int count=(Integer)session.getAttribute("count");
int snum=(Integer)session.getAttribute("snum");
%>
恭喜您答对了!
您用了<%=count %>次<br/>
<%
if(count>1&&count<6)
{
out.print("您的表现不错!!!");
}
if(count==1)
{
out.print("您太棒了!!!");
}
if(count>=6)
{
out.print("您需要努力了!!!");
}
%>
产生的随机数为:<%=snum %><br/>
所用时间为:<%=time %>
</body>
</html>
12.application实现网页访问计数
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
int number=0;
if(application.getAttribute("number")==null)
{
number=1;
}
else{
number=(Integer)application.getAttribute("number");
number++;
}
out.print("您是第"+number+"个访问者");
application.setAttribute("number", number);
%>
</body>
</html>
14,五分钟免密登录
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String u="请输入用户名";
String p="请输入密码";
Cookie cookies[]=request.getCookies();
if(cookies!=null)
{
for(Cookie c:cookies)
{
if("uname".equals(c.getName()))
{
u=c.getValue();
}
else if("pwd".equals(c.getName()))
{
p=c.getValue();
}
}
}
%>
<form action="免输登录dologin.jsp" method="post">
用户名:<input type="text" name="uname" value="<%=u %>"><br/>
密码:<input type="text" name="pwd" value="<%=p%>"><br/>
<input type="submit" name="sub" value="登录">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String name=request.getParameter("uname");
String pwd=request.getParameter("pwd");
if("sa".equals(name.trim())&&"123".equals(pwd.trim()))
{
Cookie uname=new Cookie("uname",name.trim());
Cookie upwd=new Cookie("pwd",pwd.trim());
response.addCookie(uname);
response.addCookie(upwd);
uname.setMaxAge(300);
upwd.setMaxAge(300);
}
%>
</body>
</html>