JSP一共预先定义了9个内置对象,依次为:request、response、session、application、out、pageContext、config、page、exception。
1、 request对象代表了客户端的请求信息,主要用于接收通过HTTP协议传送到服务器段的数据,包括头信息、系统信息、请求方式、请求参数等。
2、获取请求参数,解决中文乱码:
index.jsp
<a href="show.jsp?id=007&name=张三">获取请求参数的值</a>
show.jsp
ID参数值为:<%=request.getParameter("id") %>
姓名参数值为:
<%=new String(request.getParameter("name").getBytes("iso-8859-1"),"gbk") %>
运行结果:ID参数值为:007 姓名参数值为:张三
3、2秒后跳到指定页面
Response.setHeader(“refresh”,”2;URL=welcome.jsp”);
Response只在JSP页面内有效
4、Form表单信息的提交和获取
<form action="show.jsp" action="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>性别:</td>
<td>
<input type="radio" name="gender" value="男"/>男
<input type="radio" name="gender" value="女"/>女
</td>
</tr>
<tr>
<td>密码提示问题:</td>
<td>
<select name="question">
<option value="母亲生日">母亲生日</option>
<option value="宠物名称">宠物名称</option>
<option value="电脑配置">电脑配置</option>
</select>
</td>
</tr>
<tr>
<td>问题答案:</td>
<td><input type="text" name="key"/></td>
</tr>
<tr>
<td>爱好</td>
<td>
<table style="width:150px">
<tr><input type="checkbox" name="like" value="唱歌跳舞"/>唱歌跳舞</tr>
<tr><input type="checkbox" name="like" value="上网冲浪"/>上网冲浪</tr>
<tr><input type="checkbox" name="like" value="户外登山"/>户外登山</tr>
<tr><input type="checkbox" name="like" value="体育运动"/>体育运动</tr>
<tr><input type="checkbox" name="like" value="读书看报"/>读书看报</tr>
<tr><input type="checkbox" name="like" value="欣赏电影"/>欣赏电影</tr>
</table>
</td>
</tr>
<tr>
<td><input type="submit" value="提交"/></td>
</tr>
</table>
</form>
您的信息如下:
<table>
<tr>
<td>姓名</td>
<td><%=new String(request.getParameter("name").getBytes("ISO-8859-1"),"gbk") %></td>
</tr>
<tr>
<td>性别</td>
<td><%=new String(request.getParameter("gender").getBytes("ISO8859_1"),"gbk")%></td>
</tr>
<tr>
<td>密码提示问题</td>
<td><%=new String(request.getParameter("question").getBytes("ISO8859_1"),"gbk") %></td>
</tr>
<tr>
<td>问题答案</td>
<td><%=new String(request.getParameter("key").getBytes("ISO8859_1"),"gbk") %></td>
</tr>
<tr>
<td>爱好</td>
<td>
<%
String[] like=request.getParameterValues("like");
for(int i=0;i<like.length;i++)
{
%>
<%=new String(like[i].getBytes("ISO8859_1"),"gbk")+" " %>
<%
}
%>
</td>
</tr>
<tr>
<td><input type="submit" value="提交"/></td>
</tr>
</table>
5、获取客户端信息
<ul >
<li>客户端使用的协议 <%=request.getProtocol() %></li>
<li>客户端发送请求的方法 <%=request.getMethod() %></li>
<li>客户端请求路径 <%=request.getContextPath() %></li>
<li>客户机IP地址 <%=request.getRemoteAddr() %></li>
<li>客户机名称 <%=request.getRemoteHost() %></li>
<li>客户机请求端口号 <%=request.getRemotePort() %></li>
<li>接收客户信息的页面 <%=request.getServletPath() %></li>
<li>报头中的User-Agent值 <%=request.getHeader("user-agent") %></li>
<li>报头中的accept值 <%=request.getHeader("accept") %></li>
<li>报头中的Host值 <%=request.getHeader("host") %></li>
<li>报头中的accept-Encodeing值 <%=request.getHeader("accept-encoding") %></li>
<li>URI <%=request.getRequestURI() %></li>
<li>URL <%=request.getRequestURL() %></li>
</ul>
6、在作用域内管理属性
<strong>在作用域内管理属性</strong><hr/>
<%
request.setAttribute("date",new Date());
%>
获取date属性:<%=request.getAttribute("date") %><br/>
<%request.removeAttribute("date"); %>
删除后:<%=request.getAttribute("date") %><br/>
7、cookie的使用和管理
<%
String welcome="第一次访问";
String[] info=new String[]{"","",""};
Cookie[] cook=request.getCookies();
if(cook!=null)
{
for(int i=0;i<cook.length;i++)
{
if(cook[i].getName().equals("mrCookInfo"))
{
info=cook[i].getValue().split("#");
welcome="欢迎回来!";
}
}
}
%>
<%=info[0]+welcome %>
<form action="show.jsp" method="post">
姓名:<input type="text" name="name" value="<%=info[0]%>"/><br/>
出生日期:<input type="text" name="birthday" value="<%=info[1]%>"/><br/>
邮箱地址:<input type="text" name="email" value="<%=info[2]%>"/><br/>
<input type="submit" value="提交"/>"
</form>
<%
String name= new String(request.getParameter("name").getBytes("ISO8859_1"),"gbk");
String birthday=request.getParameter("birthday");
String email=request.getParameter("email");
Cookie myCook=new Cookie("mrCookInfo",name+"#"+birthday+"#"+email);
myCook.setMaxAge(60*60*24*365);
response.addCookie(myCook);
%>
表单提交成功
<hr/>
姓名<%=name %><br/>
出生日期<%=birthday %><br/>
电子邮件<%=email %><br/>
<a href="index.jsp">返回</a>
8、定时重定位页面
<%response.setHeader("refresh","10;URL=index.jsp"); %>
9、session对象
index.jsp
<%
String sessionMessage="session 练习";
session.setAttribute("message",sessionMessage);//使用session提交
out.print(sessionMessage);
%>
<form action="session.jsp" method="post">
名字:<input type="text" name="name"/><br/>
<input type="submit" value="提交"/>"
</form>
session.jsp
<%
String name=request.getParameter("name");//使用request获取
session.setAttribute("name",name);//使用session提交
%>
<form action="result.jsp" method="post">
你的名字:<%=name %><br/>
你想去的地方: <input type="text" name="place"/><br/>
<input type="submit" value="提交"/>"
</form>
result.jsp
<%
String name=(String)session.getAttribute("name");//从session读取
String place=request.getParameter("place");//从request读取
%>
你的名字:<%=name %><br/>
你想去的地方:<%=place %>
10、从目前会话删除绑定对象session.removeAttribute("message")
11、销毁session,session.invalidated()
12、session的有效期session.setMaxInactiveInterval(1000),有效期为1000秒
13、application对象
web.xml
<context-param>
<description>连接数据库URL</description>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/test/tang74</param-value>
</context-param>
<context-param>
<description>用户名</description>
<param-name>name</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<description>密码</description>
<param-name>password</param-name>
<param-value>xhp</param-value>
</context-param>
index.jsp
<%
String url=application.getInitParameter("url");
String name=application.getInitParameter("name");
String password=application.getInitParameter("password");
%>
<%=url %><br/>
<%=name %><br/>
<%=password %><br/>
14、网页计数器
index.jsp
<h4>application对象实现网页计数器</h4>
<%
out.println("设置数值"+"<br/>");
Integer intcount;
if(application.getAttribute("count")==null)
{
intcount=1;
}
else
{
intcount=(Integer.parseInt(application.getAttribute("count").toString()));
}
application.setAttribute("name","cdd");
application.setAttribute("count",intcount);
out.println("set name=cdd"+"<br/>");
out.println("set count="+intcount+"<br/>");
%>
<a href="gateppatter.jsp">计数器页面</a>
gateppatter.jsp
用户名:<%=application.getAttribute("name") %>
计数器:
<%
int mycount=Integer.valueOf(application.getAttribute("count").toString()).intValue();
out.println(mycount);
application.setAttribute("count",Integer.toString(mycount+1));
%>
15、表单提交验证
脚本
<script type="text/javascript" language="javascript">
function myCheck()
{
if(myForm.UserName.value=="")
{
alert("用户名不能为空");
myForm.UserName.focus();
return ;
}
if(myForm.PWD.value=="")
{
alert("密码不能为空");
myForm.PWD.focus();
return ;
}
if(myForm.YanZheng.value=="")
{
alert("验证码不能为空");
myForm.YanZheng.focus();
return ;
}
}
</script>
index.jsp
<form action="check.jsp" method="post" name="myForm">
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="UserName"/></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="PWD"/></td>
</tr>
<tr>
<td>验证码</td>
<td><input type="text" name="YanZheng"/></td>
<%
int intmethod1=(int)((Math.random()*11)-1);
int intmethod2=(int)((Math.random()*11)-1);
int intmethod3=(int)((Math.random()*11)-1);
int intmethod4=(int)((Math.random()*11)-1);
String intsum=intmethod1+" "+intmethod2+" "+intmethod3+" "+intmethod4;
%>
<td>
<img src=num/<%=intmethod1 %>.gif>
<img src=num/<%=intmethod2 %>.gif>
<img src=num/<%=intmethod3 %>.gif>
<img src=num/<%=intmethod4 %>.gif>
</td>
</tr>
<tr>
<td><input type="submit" value="提交" onClick="myCheck()"/></td>
<td><input type="reset" value="重置"/>
</tr>
</table>
</form>
check.jsp
<%
String name=request.getParameter("UserName");
String password=request.getParameter("PWD");
String message;
if((name.equals("xhp"))&&(password.equals("xhp")))
{
message="可以登录系统";
}
else
{
message="用户名或密码错误";
}
%>
<script type="text/javascript" language="javascript">
alert("<%=message%>")
window.location.href='index.jsp';
</script>
16、课后练习题
(1)
第一题 实现用户注册
<form action="check.jsp" method="post">
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td><input type="submit" value="提交"/></td>
</tr>
</table>
</form>
<%
String name=request.getParameter("name");
String password=request.getParameter("password");
if((name!="") && (password!=""))
{
out.println("注册成功!"+"<hr>");
out.println("您的用户名是 "+name+"<br>");
out.println("您的密码是 "+password+"<br>");
out.println("<hr>"+"请牢记!");
}
else
{
out.println("注册失败!"+"<hr>");
out.println("用户名和密码均不能为空!");
}
%>
(2)
留言版<hr>
<%
Date now=new Date();
String date=String.format("%tY-%tm-%td %tH:%tm:%tS",now,now,now,now,now,now);
session.setAttribute("time",date);
%>
<form action="result.jsp" method="post">
昵称:<input type="text" name="name" width="80px" ><br><br>
<textarea name="content" style="height:100px" ></textarea><br><br>
<input type="submit" name="submit" value="提交">
<input type="reset" name="reset" value="重填">
</form>
<%
String name=request.getParameter("name");
String content=request.getParameter("content");
%>
您是:<%=name %><br/>
您说:<%=content %><br/>
提交 时间:<%=session.getAttribute("time") %><br/>
<hr/>
(3)使用cookie对象实现自动登录
index.jsp
这是主页,欢迎光临!
<%
Cookie[] cook=request.getCookies();
if(cook!=null)
{
for(int i=0;i<cook.length;i++)
{
if(cook[i].getName().equals("huiCookie"))
{
response.sendRedirect("content.jsp");
}
}
out.print("有cookie,但是 您还没有登录,5秒后转入登录页面");
response.setHeader("refresh","5;URL=login.jsp");
}
else
{
out.print("您还没有登录,5秒后转入登录页面"+"<br/>");
response.setHeader("refresh","5;URL=login.jsp");
}
%>
<hr/>
login.jsp
<%
Cookie myCook=new Cookie("huiCookie","718");//只有717cookie
response.addCookie(myCook);
%>
<form action="index.jsp" method="post">
<input type="submit" value="注册cookie"/>"
</form>
content.jsp
2011年07月17日 星期日
► 迎接中国共产党建党90周年
► 欢迎报考中国农业大学
► 记录2011:毕业生就业
► 详细内容