一、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 错误 解决方法
链接: 解决方法.