java中response对象用法实例分析
程序员文章站
2024-03-08 19:09:04
本文实例讲述了java中response对象用法。分享给大家供大家参考,具体如下:
动作元素用于运行时在服务器端结束当前页面的执行,...
本文实例讲述了java中response对象用法。分享给大家供大家参考,具体如下:
<jsp:forward>动作元素用于运行时在服务器端结束当前页面的执行,并从当前页面转向指定页面。
使用response对象的setheader()方法可以设置页面的自动刷新时间间隔。实现每隔60秒重新加载本页面的语句为:
复制代码 代码如下:
response.setheader("refresh",60);
而实现3秒后浏览器加载新页面//www.jb51.net的语句为:
复制代码 代码如下:
response.setheader("refresh","3;url=//www.jb51.net");
response的方法:void sendredirect(string url),将页面重定向到指定的url地址上。
实例:使用response实现用户登录功能
login.html为登录表单页面
login.jsp为信息处理页面,用来验证用户登录是否成功。
success.jsp为登录成功后的跳转页面。
login.html的源代码如下:
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <title>登录功能实例</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <center> <h1>登录界面</h1> <form action="login.jsp" method="post"> 姓名:<input type="text" name="name"><br> 密码:<input type="password" name="pwd"><br> <input type="submit" name="submit" value="登录"> <input type="reset" name="reset" value="重置"> </form> </center> </body> </html>
login.jsp的源代码如下:
<%@ page language="java" import="java.util.*" contenttype="text/html;charset=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>登录功能实例</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> <h1>登录功能实例</h1> <% request.setcharacterencoding("utf-8"); string name = request.getparameter("name"); string pwd = request.getparameter("pwd"); if(name != null && pwd != null && name.equals("guanlin") && pwd.equals("123")) { //response.sendredirect("success.jsp"); %> <jsp:forward page="success.jsp"></jsp:forward> <%}else { out.println("<font color='red'>用户名或密码错误,5秒钟回到登录页面,如果不想等待请点<a href='response/login.html'>返回登录</a></font>"); response.setheader("refresh","5;url = login.html"); } %> </center> </body> </html>
success.jsp的源代码如下:
<%@ page language="java" import="java.util.*" contenttype="text/html;charset=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>登录功能实例</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> <h1 style="green">登录成功!</h1> <% request.setcharacterencoding("utf-8"); string name = request.getparameter("name"); string pwd = request.getparameter("pwd"); %> 登录的用户名为:<%=name %><br> 登录的密码为:<%=pwd %> </center> </body> </html>
希望本文所述对大家java程序设计有所帮助。