1.
<%@ page pageEncoding="UTF-8"%><%@ page import="java.io.*"%> <%@ page import="java.util.*"%> <%@ page import="java.math.*"%> <html> <head><title>网站计数器</title></head> <body> <%! BigInteger count = null ; //定义一个%> <%! public BigInteger load(File file){ BigInteger count = null ; // 接收数据 try{ //判断是否有存储数据的文件if(file.exists()){ Scanner scan = new Scanner(new FileInputStream(file)) ;if(scan.hasNext()){ count = new BigInteger(scan.next()) ;//将内容放到BigInteger类中} scan.close() ; } else { count = new BigInteger("0") ; // 从0开始save(file,count) ; // 保存为一个新的文件} }catch(Exception e){ e.printStackTrace() ; } return count ; } public void save(File file,BigInteger count){ //保存计数文件try{ PrintStream ps = null ;ps = new PrintStream(new FileOutputStream(file)) ;ps.println(count) ;ps.close() ; }catch(Exception e){ e.printStackTrace() ; } } %> <% String fileName = this.getServletContext().getRealPath("/") + "count.txt"; // 保存所有的计数结果 File file = new File(fileName) ; if(session.isNew()){ //同一次会话不重复计数synchronized(this){ count = load(file) ; count = count.add(new BigInteger("1")) ; // 再原本的基础上增加1。 save(file,count) ; } } %> <h2>您是第<%=count==null?0:count%>位访客!</h2> </body> </html>
2.
<%@ page language="java"contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta charset="UTF-8"></head><body><%String username = "";String password = "";// 获取cookieCookie[] cookies = request.getCookies();// 如果为空,则停留在该页面if(cookies == null){return ;// 不为空则获取用户名和密码}else{for(int i = 0; i < cookies.length; i++){if("username".equals(cookies[i].getName())){username = cookies[i].getValue();}if("password".equals(cookies[i].getName())){password = cookies[i].getValue();}}// 验证用户名和密码if("admin".equals(username) && "123".equals(password)){session.setAttribute("username", username);session.setAttribute("password", password);}}%><form action="test" method="post">用户名:<input type="text" name="username"/><br/>密 码:<input type="password" name="password" /><br /><input type="submit" value="登陆" /></form></body></html>
3.
Index.jsp
<%@ page pageEncoding="utf-8"%><html><body><iframe src="content.jsp" name="content" width="100%" height="80%"></iframe><form action="addinfo.jsp" target="content" id="chatform" method="post">昵称:<input type="text" name="nicheng" id="nicheng">发言:<input type="text" name="info" id="info"><input type="button" value="确定" onclick="check()"></form><script>function check(){var nicheng=document.getElementById("nicheng").value;var info=document.getElementById("info").value;if(nicheng==''||info==''){alert("昵称或留言内容不能为空");}else{document.getElementById("chatform").submit();}}</script></body></html>
Content.jsp
<%@ page pageEncoding="utf-8" import="java.util.*"%><%@ page import="org.apache.commons.lang3.StringUtils"%><html><head> <meta http-equiv="refresh" content="10"><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> </head><body><%request.setCharacterEncoding("UTF-8");%><%=application.getAttribute("infoList1") %></body></html>
Addinfo.jsp
<%@ page pageEncoding="utf-8" import="java.util.*,java.text.*"%><%@ page import="org.apache.commons.lang3.StringUtils"%><html><body><%request.setCharacterEncoding("UTF-8");String nicheng=request.getParameter("nicheng");String info=request.getParameter("info");String time=new SimpleDateFormat("yy-MM-dd kk:mm:ss").format(new Date());//当前时间List<String> infoList=new ArrayList<String>();String s=(String)application.getAttribute("infoList1");if(s==null){//第一次存储信息s="";}else{String[] a=s.split("<br>");int b=a.length;if(b==30){//够30条s="";}infoList.add(nicheng+"在"+time+"说:"+info+"<br>");s=s+StringUtils.strip(infoList.toString(),"[]");//添加}application.setAttribute("infoList1", s);response.sendRedirect("content.jsp");//重定向到content.jsp%></body></html>
- 否
GetNumer.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta charset="UTF-8"></head><body>程序随机产生了一个0-100间的整数,请猜一猜<br><%int n=(int)(Math.random()*100)+1;session.setAttribute("save", new Integer(n));%><form action="caishu.jsp" method="post"><input type="text" name="t1"/><input type="submit" value="guess"/></form></body></html>
Caishu.jsp
<%@ page pageEncoding="UTF-8" %><html><body><body>程序随机产生了一个0-100间的整数,请猜一猜<br><form action="caishu.jsp" method="get"><input type="text" name="t1"/><input type="submit" value="guess"/></form><%String t1=request.getParameter("t1");Integer integer=(Integer)session.getAttribute("save");int realnumber=integer.intValue();if(t1!=null){int t=Integer.parseInt(t1);if(t==realnumber){out.print("right,<a href='getNumber.jsp'>再来一次</a>");}else if(t<realnumber){out.print("too small");out.println(realnumber);}else{out.print("too large");out.println(realnumber);}}%></body></html>