session内置对象应用

session内置对象作业

  • userlogin界面
    -----------------------------这个界面没有什么重要的------------------------
    session内置对象应用_第1张图片
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 align="center">用户登录</h1>
<form action="messinput.jsp" method="post" >
 <table align="center" bgcolor="red">
  <tr>
   <td>用户名:</td><td><input type="text" name="username" /></td><tr>
   <td>密码:</td><td><input type="password" name="password" /></td>
  </tr>
  <tr colspan="2">
   <td colspan="2" align="center" ><input type="submit" value="提交"/><input type="reset" value="重置"/></td>
  </tr>
 </table>
</form>
</body>
</html>
  • messinput界面
    -----------------------------这个界面没有也什么重要的------------------------

session内置对象应用_第2张图片

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Insert title here</title>
</head>
<body>
<%
 request.setCharacterEncoding("utf-8") ;
 int a=0;
 String username="";
 username=request.getParameter("username");
%>
<form action="messsave.jsp" method="post" name="f1">
 用户名:<input type="text" name="Name" value="<% if(username!=null) out.print(username);%>"/><br><br>
 留言标题:<input type="text" name="Title"/><br><br>
 留言内容:<textarea name="messages" rows="6" cols="50"></textarea><br><br>
 <input type="submit" value="提交留言"/>
</form>
</body>
</html>
  • messsave界面
    -----------------------------这个界面很重要重要的------------------------
    获取上一个页面的用户名、留言和标题
    把获取的内容生成一条记录,每一个信息用分隔符隔开
    最后保存到全局变量中
    在这里插入图片描述
<%@ page language="java" import="java.util.*" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Insert title here</title>
</head>
<body>
<%!
 Vector v=new Vector();
 int i=0;
 ServletContext application;
 synchronized void sendMessage(String s){
     
  application=getServletContext();
  i++;
  v.add("NO"+i+s);
  application.setAttribute("Mess",v);//所有的信息都保存在Mess Mess相当于是一个容器 名字叫Mess, V=key-value;
  //setAttribute(name,value);name是String类型,value是Object类型;往域对象里面添加数据,添加时以key-value形式添加
  //上面的解释好像错了:"Mess"=key,v=value;
 }
%>
<%
 String name=request.getParameter("Name");
 String title=request.getParameter("Title");
 String mess=request.getParameter("Messages");
 String time=new Date().toString();//系统时间
 String s="#"+name+"#"+title+"#"+mess+"#"+time+"#";//一条记录
 sendMessage(s);//函数调用
 out.print("您的信息已提交");
 out.print("-----------------");
%>
<a href="showmess.jsp">查看留言板</a>
</body>
</html>

重点一、Vector
Vector是可实现自动增长的对象数组。
重点二、ServletContext application;
相当于一个全局变量(不仅仅是针对于声明的页面,而是适用于需要调用ServletContext application的每一个界面),可以获取全局配置参数

重点三、synchronized
方法前加synchronized,保证线程安全性,使得每一个线程的操作都是原子性的,打印出的结果都是当前线程完成后立即更新的值,但多线程具有竞争性,此代码不能保证是哪一个线程该优先执行,所以线程执行是乱序的。具体详情参见引用 ;方法前未加synchronized 则线程不安全

  • messshow
    在这里插入图片描述将保存在application中的数组取出来 通过解析分隔符将字段依次取出再顺序输出
<%@ page language="java" import="java.util.*" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Insert title here</title>
</head>
<body>
<table width="700" align="center" border="2">
<tr>
 <td bgcolor="pink">序号</td>
 <td bgcolor="pink">留言这姓名</td>
 <td bgcolor="pink">留言标题</td>
 <td bgcolor="pink">留言时间</td>
 <td bgcolor="pink">留言内容</td>
</tr>
<%
 Vector v=(Vector)application.getAttribute("Mess");//Vector就是一个数组 长度自动变化
  for(int i=0;i<v.size();i++){
     
  %>
  <tr>
   <%
    String message=(String)v.get(i);//表示第i条留言
    StringTokenizer st=new StringTokenizer(message,"#");
    int num=st.countTokens();
    for(int k=1;k<=num;k++){
     
     String str=st.nextToken();
     %>
      <td bgcolor="pink"><%=str%></td>
     <%
    }
    
   %>
  </tr>
  <%
}
%>
</table>
</body>
</html>

为什么每输出一个内容就会有粉红色的框呢?因为输出的结果包含在一个td表格内,把表格的长度会随着上边的表格的变化而变化

你可能感兴趣的:(javascript,javascript)