1、out对象
主要用来向客户端输出各种格式的数据,并且管理应用服务器上的输出缓冲区,out对象的基类是javax.servlet.jsp.jspwriter类。
out的主要方法:
| out.println(datatype);或out.print(datatype); |
实例:
| <%@page language="java" contenttype="text/html;charset=gb2312" import="java.util.*" %> <html> <head> <title> out对象使用实例</title> </head> <body> <% out.println(new date().tolocalestring()); out.print("<br>"); out.print("测试成功"); %> </body> </html> |
2、request对象
request.setattribute()与request.getattribute()方法实例(也可以使用forward建立请求关系):
主页面:
| <%@page language="java" contenttype="text/html;charset=gb2312" %> <%@ include file="1.jsp" %> 你刚才输入的内容是: <%=request.getattribute("gr")%> |
引入页面:
| <%@page language="java" contenttype="text/html;charset=gb2312" import="java.util.*" %> <html> <head> <title> request对象使用实例</title> </head> <body> <% request.setattribute("gr","123333"); %> </form> </body> </html> |
request.getparameter()方法实例
主页面
| <%@page language="java" contenttype="text/html;charset=gb2312" import="java.util.*" %> <html> <head> <title> request.getparameter()方法使用实例</title> </head> <body> <form method=post action="2.jsp"> <input type="text" name="gr1"><br> <input type="text" name="gr2"><br> <input type="text" name="gr3"><br> <input type="submit" name="submit" value="提交"> <input type="reset" name="reset" value="清除"> </form> </form> </body> </html> |
引入页面
| <%@page language="java" contenttype="text/html;charset=gb2312" %> 你刚才输入的内容是:<br> <%=request.getparameter("gr1")%><br> <%=request.getparameter("gr2")%><br> <%=request.getparameter("gr3")%><br> |
request.getparametername()方法实例
主页面
| <%@page language="java" contenttype="text/html;charset=gb2312" %> <html> <head> <title> request.getparametername()方法使用实例</title> </head> <body> <form method=post action="2.jsp"> <input type="text" name="gr1"><br> <input type="text" name="gr2"><br> <input type="text" name="gr3"><br> <input type="submit" value="提交"> <input type="reset" value="清除"> </form> </form> </body> </html> |
指向页面
| <%@page language="java" contenttype="text/html;charset=gb2312" import="java.util.*" %> 你刚才输入的内容是:<br> <% enumeration e=request.getparameternames(); while(e.hasmoreelements()){ string parametername=(string)e.nextelement(); string parametervalue=(string)request.getparameter(parametername); out.print("参数名称:"+parametername+"<br>"); out.print("参数内容:"+parametervalue+"<br>"); } %> |
request.getattributename()方法实例
主页面:
| <%@page language="java" contenttype="text/html;charset=gb2312" import="java.util.*" %> <html> <head> <title> request.getattributename()方法使用实例</title> </head> <body> <jsp:include page="2.jsp" flush="true"/> <% enumeration e=request.getattributenames(); while(e.hasmoreelements()){ string attributename=(string)e.nextelement(); string attributevalue=(string)request.getattribute(attributename); out.print("变量名称:"+attributename); out.print("变量内容:"+attributevalue+"<br>"); } %> </form> </form> </body> </html> |
转向页面
| <%@page language="java" contenttype="text/html;charset=gb2312" %> <% request.setattribute("gr1","111"); request.setattribute("gr2","222"); request.setattribute("gr3","333"); %> |
request.getremoteaddr()方法实例:
| <%@page language="java" contenttype="text/html;charset=gb2312" %> <html> <head> <title> request.getremoteaddr()方法使用实例</title> </head> <body> <b>你的ip地址:</b> <b><%=request.getremoteaddr()%></b> </form> </form> </body> </html> |
3、response对象
response.setheader()方法网页自动刷新实例:
| <%@page language="java" contenttype="text/html;charset=gb2312" import="java.util.*" %> <html> <head> <title> response刷新页面实例</title> </head> <body> <% response.setheader("refresh","3"); out.println(new date().tolocalestring()); %> </body> </html> |
4、application对象
在jsp服务器运行时刻,仅有一个application对象,它由服务器创建,也由服务器自动清除, 不能被用户创建和清除。我们只能创建这个appliation对象的同步拷贝。
setattribute(),getattribute()和removeattribute()方法实例:
| <%@page language="java" contenttype="text/html;charset=gb2312" %> <html> <head> <title> application对象方法实例</title> </head> <body> <% string username="rossini"; string password="126263"; application.setattribute("username",username); application.setattribute("password",password); out.println(application.getattribute("username")+"<br>"); out.println(application.getattribute("password")+"<br>"); application.removeattribute("password"); out.println(application.getattribute("password")+"<br>"); %> </body> </html> |
getattributenames()方法实例:
| <%@page language="java" contenttype="text/html;charset=gb2312" import="java.util.*" %> <html> <head> <title> application对象方法实例</title> </head> <body> <% string username="rossini"; string password="126263"; application.setattribute("username",username); application.setattribute("password",password); enumeration enum=application.getattributenames(); while(enum.hasmoreelements()){ string attrname=(string)enum.nextelement(); out.println(attrname+"----"+application.getattribute(attrname)+"<br>"); } %> </body> </html> |
5、session对象
当用户登陆网站,系统将为他生成一个独一无二的session对象,用以记录改用户的个人信息,一旦改用户退出网站,那么该session对象将会注销。session对象可以绑定若干个人信息或者java对象,不同session对象的同名变量是不会相互干扰的。
getvalue(string name)、putvalue(string name)、removevalue(string name)、getvaluenames() 、getcreationtime()、getid()、getlastaccessedtime()、getmaxinactiveinterval()、setmaxinactiveinterval()方法:
主文件:
| <%@page language="java" contenttype="text/html;charset=gb2312" %> <html> <head> <title> session主页面</title> </head> <body> <% string username="rossini"; string password="126263"; session.putvalue("username",username); session.putvalue("password",password); %> <a href="2.jsp">指向第二页</a> </body> </html> |
转向页面1
| <%@page language="java" contenttype="text/html;charset=gb2312" %> <html> <head> <title> session转向页面1</title> </head> <body> <% string usr=(string)session.getvalue("username"); string pwd=(string)session.getvalue("password"); %> <%=usr%><br> <%=pwd%><br> <%out.println("session create:"+session.getcreationtime());%><br> <%out.println("session id:"+session.getid());%><br> <%out.println("session last access:"+session.getlastaccessedtime());%><br> <%out.println("session 原来最大休眠时间:"+session.getmaxinactiveinterval());%><br> <%session.setmaxinactiveinterval(session.getmaxinactiveinterval()+1);%><br> <%out.println("session 最新最大休眠时间:"+session.getmaxinactiveinterval());%><br <% string []name=session.getvaluenames(); out.println("--------------"+"<br>"); for(int i=0;i<name.length;i++) { out.println(session.getvalue(name[i])+"<br>"); } %> <% session.removevalue("username"); %> <a href="3.jsp">指向第三页</a> </body> </html> |
转向页面2
| <%@page language="java" contenttype="text/html;charset=gb2312" %> <html> <head> <title> session转向页面2</title> </head> <body> <% string usr=(string)session.getvalue("username"); string pwd=(string)session.getvalue("password"); %> <%=usr%><br> <%=pwd%> </body> </html> |
invalidate()方法将会将会清除当前的session对象解除它和任何参数或者java对象的绑定关系
闽公网安备 35060202000074号