java中response对象用法实例分析
本文实例讲述了java中response对象用法。分享给大家供大家参考,具体如下:
<jsp:forward>动作元素用于运行时在服务器端结束当前页面的执行,并从当前页面转向指定页面。
使用response对象的setHeader()方法可以设置页面的自动刷新时间间隔。实现每隔60秒重新加载本页面的语句为:
response.setHeader("refresh",60);
而实现3秒后浏览器加载新页面https://www.nhooo.com的语句为:
response.setHeader("refresh","3;URL=https://www.nhooo.com");
response的方法:voidsendRedirect(Stringurl),将页面重定向到指定的URL地址上。
实例:使用response实现用户登录功能
login.html为登录表单页面
login.jsp为信息处理页面,用来验证用户登录是否成功。
success.jsp为登录成功后的跳转页面。
login.html的源代码如下:
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"> <html> <head> <title>登录功能实例</title> <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> <metahttp-equiv="description"content="thisismypage"> <metahttp-equiv="content-type"content="text/html;charset=UTF-8"> <!--<linkrel="stylesheet"type="text/css"href="./styles.css">--> </head> <body> <center> <h1>登录界面</h1> <formaction="login.jsp"method="post"> 姓名:<inputtype="text"name="name"><br> 密码:<inputtype="password"name="pwd"><br> <inputtype="submit"name="submit"value="登录"> <inputtype="reset"name="reset"value="重置"> </form> </center> </body> </html>
login.jsp的源代码如下:
<%@pagelanguage="java"import="java.util.*"contentType="text/html;charset=UTF-8"%> <% Stringpath=request.getContextPath(); StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"> <html> <head> <basehref="<%=basePath%>"> <title>登录功能实例</title> <metahttp-equiv="pragma"content="no-cache"> <metahttp-equiv="cache-control"content="no-cache"> <metahttp-equiv="expires"content="0"> <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> <metahttp-equiv="description"content="Thisismypage"> <!-- <linkrel="stylesheet"type="text/css"href="styles.css"> --> </head> <body> <center> <h1>登录功能实例</h1> <% request.setCharacterEncoding("UTF-8"); Stringname=request.getParameter("name"); Stringpwd=request.getParameter("pwd"); if(name!=null&&pwd!=null&&name.equals("guanlin")&&pwd.equals("123")) { //response.sendRedirect("success.jsp"); %> <jsp:forwardpage="success.jsp"></jsp:forward> <%}else { out.println("<fontcolor='red'>用户名或密码错误,5秒钟回到登录页面,如果不想等待请点<ahref='response/login.html'>返回登录</a></font>"); response.setHeader("refresh","5;url=login.html"); } %> </center> </body> </html>
success.jsp的源代码如下:
<%@pagelanguage="java"import="java.util.*"contentType="text/html;charset=UTF-8"%> <% Stringpath=request.getContextPath(); StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"> <html> <head> <basehref="<%=basePath%>"> <title>登录功能实例</title> <metahttp-equiv="pragma"content="no-cache"> <metahttp-equiv="cache-control"content="no-cache"> <metahttp-equiv="expires"content="0"> <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> <metahttp-equiv="description"content="Thisismypage"> <!-- <linkrel="stylesheet"type="text/css"href="styles.css"> --> </head> <body> <center> <h1style="green">登录成功!</h1> <% request.setCharacterEncoding("UTF-8"); Stringname=request.getParameter("name"); Stringpwd=request.getParameter("pwd"); %> 登录的用户名为:<%=name%><br> 登录的密码为:<%=pwd%> </center> </body> </html>
希望本文所述对大家Java程序设计有所帮助。