javaEE7

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 = "";

        // 获取cookie

        Cookie[] 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/>

        密&nbsp;&nbsp;&nbsp;码:<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>

  • 10
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值