java中response对象用法实例分析_Java教程-查字典教程网
java中response对象用法实例分析
java中response对象用法实例分析
发布时间:2016-12-28 来源:查字典编辑
摘要:本文实例讲述了java中response对象用法。分享给大家供大家参考,具体如下:动作元素用于运行时在服务器端结束当前页面的执行,并从当前页...

本文实例讲述了java中response对象用法。分享给大家供大家参考,具体如下:

<jsp:forward>动作元素用于运行时在服务器端结束当前页面的执行,并从当前页面转向指定页面。

使用response对象的setHeader()方法可以设置页面的自动刷新时间间隔。实现每隔60秒重新加载本页面的语句为:

复制代码 代码如下:response.setHeader("refresh",60);

而实现3秒后浏览器加载新页面http://www.jb51.net的语句为:

复制代码 代码如下:response.setHeader("refresh","3;URL=http://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"> <> </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>登录成功!</h1> <% request.setCharacterEncoding("UTF-8"); String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); %> 登录的用户名为:<%=name %><br> 登录的密码为:<%=pwd %> </center> </body> </html>

希望本文所述对大家Java程序设计有所帮助。

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Java学习
    热门Java学习
    编程开发子分类