JSP实验5:session和application的对象使用

一、session对象的使用

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

<%     
   int Num=0;
   String strNum=(String)session.getAttribute("Num");
   
   if(strNum!=null)//防止出现空指针的情况
      Num=Integer.parseInt(strNum)+1;
      
   session.setAttribute("Num", String.valueOf(Num));//重新设置
       
%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'sessionCount.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>
     <center><font size=5>session计数器</font></center>
     <br>
     <center>
        <font color=blue>您是第</font>
        <font color=red><%=session.getAttribute("Num")%></font>
        <font color=blue>个访问本站的用户</font>
     </center>
  </body>
</html>

二、application对象的使用

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
   int Num=0;
   String strNum=(String)application.getAttribute("Num");
   if(strNum!=null)
      Num=Integer.parseInt(strNum)+1;
      
   application.setAttribute("Num", String.valueOf(Num));
 %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'applicationCount.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>
    <center><font size=5>appication 对象示例</font></center>
    <br>
    <%=application.getRealPath("application.jsp")%>
    <br>
    <br>
    <font color=blue>您已经访问页面</font>
    <font color=red><%=Num%></font>
    <font color=blue></font>
  </body>
</html>

三、利用application制作留言板

1.inputMessage.jsp(输出模块)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'inputMessage.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">
	-->
     <style>
     #form2 input{
           color:green;
           font-weight:bold;
     }
     </style>
  </head>
  
  <body bgcolor="#abcdef">
      <form action="checkMessage.jsp" method="post">
       请输入姓名<input type="text" name="name"/>
       <br>
       请输入标题<input type="text" name="title"/>
       <br>
      请输入内容:
       <br> 
       <textarea rows="10" cols="40" name="message"></textarea>
      <br>
      <br>
      <br>
      <input type="submit" value="留言"/>
      </form>
      <br>
      <form id="form2" action="showMessage.jsp" method="post">
          <input type="submit" value="查看留言板"/>
      </form>
  </body>
</html>

2.checkMessage.jsp(处理存储模块)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'checkMessage.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><div><p><br></p></div>
  <%! Vector<String> v=new Vector<String>();%>
   <%   
     //预先设置
      String name="";
      String title="";
      String message="";
         
      
       request.setCharacterEncoding("utf-8");
       name=request.getParameter("name");
       title=request.getParameter("title");
       message=request.getParameter("message");
   
       if(name!="" && title!="" && message!="")//判断是否输入完全
       {
        try{
           if(v!=null)
             { 
              v.add("姓名:"+name+"."+"标题:"+title+"."+"内容:"+message);//为后面以逗号分隔做准备
              application.setAttribute("message",v);
              out.println("留言成功");
             }
     
           }
        catch(Exception e){
           out.println("存在错误");
        }        
       }
       else
       {
            out.println("留言失败");   
       }  
       %>
      
      <hr>
      <a href="showMessage.jsp">1.查看留言板</a>
      <br>
      <a href="inputMessage.jsp">2.继续留言</a>      
  </body>
</html>


3.showMessage.jsp(展示模块)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'showMessage.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">
	-->
    <style type="text/css">
        body{
        backGround:RGBA(255,255,255,1)
         }
        div{
          width:800px;
          border:1px solid RGBA(100,90,97,1);
          color:while;
        }
    </style>
  </head>
  
  <body>
     <div>
         <%
            Object o=application.getAttribute("message");
            if(o==null)     
               out.print("暂时还没有留言哩");
            else{
              Vector<String> v=(Vector<String>)o;//Vector表示一个容器
             for(int i=v.size()-1;i>=0;i--)//倒序展示
             {
               StringTokenizer st=new StringTokenizer(v.get(i),".");//表示"."以分割字符串成多个字符串       
               while(st.hasMoreElements())//遍历每一个片段
               {
                 out.println(st.nextToken()+"<br>");
               }
               out.println("<hr>");
             }
            }   
          %>
     </div>
  </body>
</html>

四、常见错误


1. jsp跳转时中文传递出现乱码

解决方法:在转跳页面加上: request.setCharacterEncoding(“utf-8”);

在这里插入图片描述


2.出现空指针错误:java.lang.NullPointerException

例如:以下的代码会发生错误

String userName = request.getParameter(“username”);
If (userName.equals(“root”))
{…}

原因:采用String对象保存用户提交的结果;在如果涉及对象的操作时,先检测其是否为空后,检查到对象为空后,可再选择进行以下任一种处理方式:

处理方式1:检查到对象为空时,设置对象值为空字符串或一个默认值;
处理方式2:检测到对象为空时,根本不执行某操作,直接跳转到其他处理中。
处理方式3: 检查到对象为空时,提示用户操作有错误。

方式1:

String userName = request.getParameter(“username”);
// 该变量值为空时,转化为默认空字符串
If (userName == null)
userName = “”;
If (userName.equals(“root”))
{…}

方式2:

String userName = request.getParameter(“username”);
// 该变量值为空时,转化为默认空字符串,不执行有关操作。
If (usreName != null)
{
If (userName.equals(“root”))
{…}
}

方式3:

String userName = request.getParameter(“username”);
// 该变量值为空时,转化为默认空字符串,不执行有关操作。
If (usreName == null)
{
// 提示用户输入信息为空
}


3.ThreadPoolExecutor$Worker.run()错误

原因:MyEclpise debug 模式的线程经常会停在ThreadPoolExecutor$Worker.run() 这是因未捕获异常导致的.

解决方法:在MyEclpise里设置如下,将Suspend execution on uncaughtexceptions前面的勾去


4.TOMCAT内存溢出问题Exception in thread "“http-bio-8080”-exec-6等等

解决方法
MyEclipse中打开window–>preferences–>Tomacat
-Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=128m
如果当前的信息就是上述的参数的话,那么可以适当的调大一些上面的值。

在这里插入图片描述

5.解决Destroying ProtocolHandler [“ajp-apr-8009”]

链接: 解决方法.


6.页面跳转错误

在这里插入图片描述
跳转success.jsp,跳转的函数或链接书写错误书写错误
在这里插入图片描述

7.JSP 使用报Duplicate local variable path 错误 解决方法

链接: 解决方法.

8.建文件是,应该建Web Project文件

在这里插入图片描述

8.跳转页面时,跳转文件名书写错误

page=“文件名”

在这里插入图片描述

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实验内置对象使用 一、实验目的 1、掌握各个内置对象的含义; 2、理解并熟练应用sessionapplication对象。 二、实验内容 1、设计聊天室,在聊天室中,需要通过JSP内置对象application来实时保存特定数量的当前聊天信息。 聊天室的设计包括:用户进行登录,选择聊天室,进行聊天,退出聊天室。 在聊天室中,用户只需输入一个用户名就可以进入聊天室,但是如果当前有人在使用该用户名,那么就必须换一个唯一的用户名。 具体要求:  用户登录成功后,程序会要求用户选择聊天室。可以不设置用户自行建立聊天室的功能,而且在聊天中途不能从一个聊天室切换到另一个聊天室。  进入聊天室后,用户可以从用户信息窗口看到该聊天室中所有用户的用户名,也可以在聊天窗口中看到随时更新的聊天信息。用户可以给所有人或某一个聊天用户发送公共的聊天信息,这个聊天内容大家都可以看到。用户也可以给某个用户发送私人的聊天信息,这种信息属于私聊信息,只有发送者和接收者可以看到。此外,聊天窗口还会出现一些系统公告,比如某某上站、某某离开等消息,另外用户还可以自己定义聊天信息和聊天用户信息刷新的时间间隔。  在用户单击“退出”按钮后,页面关闭,同时applicationsession中保存的信息都将丢失。 三、实验方法 1、用户登录信息使用request对象getParameter()方法得到用户登陆的一些信息; 2、公聊信息可以使用application对象,私聊信息使用session对象。 3、聊天的信息要不断刷新页面使用户实时看到聊天信息。 4、用户退出时,有两种情况需要考虑:一是用户点击“退出”按钮,二是关闭浏览器,强制退出窗口,可查阅windows感知浏览器关闭的事件的相应方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值