jsp令我惊讶的Hashtable,可以在不同用户间记住会话的值

Registe.jsp:

<form action="Registe_Deal.jsp" method="post" >
        姓名:<input type="text" name="name" /><br/>
        密码:<input type="password" name="password" /><br/>
        <input type="submit" value="提交" />
    </form>

 

 

Registe_Deal.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'Registe_Deal.jsp' starting page</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">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  <body>

     <!-- 声明函数,必须加上!jsp真是怪异 -->
      <%!
        Hashtable<String,String> hashtable = new Hashtable<String,String>();
        public synchronized void putName(String s)
        {
            hashtable.put(s,s);
        }
       %>
    <%
        String strName = request.getParameter("name");
        String strPassWord = request.getParameter("password");
        if(null == strName || "" == strName.trim())
        {
            out.print("什么都不写是不可以的?");
        }
        else
        {
            byte[] b = strName.getBytes("ISO-8859-1");
            strName = new String(b,"UTF-8");

            //我在本地使用两个浏览器来测试,居然这段代码能正常工作,jsp的Hashtable真是够可以的
            if(!hashtable.containsKey(strName))
            {
                putName(strName);
                out.print("注册成功了!");
                out.print("您注册的名字是:" + strName);
            }
            else
            {
                out.print("这个名字您已经注册过了!");
            }
        }
        out.print("<br/><a href='Registe.jsp' >返回</a>");
     %>
  </body>
</html>

posted on 2011-09-27 11:15  冰危节奏  阅读(194)  评论(0)    收藏  举报

导航