java中response对象用法实例分析

 更新时间:2015年12月30日 15:55:49   作者:官林辉  
这篇文章主要介绍了java中response对象用法,结合实例形式分析了Java中response对象的功能及具体使用技巧,需要的朋友可以参考下

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

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

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

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

而实现3秒后浏览器加载新页面https://www.jb51.net的语句为:
复制代码 代码如下:
response.setHeader("refresh","3;URL=https://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程序设计有所帮助。

相关文章

  • 关于spring依赖注入的方式以及优缺点

    关于spring依赖注入的方式以及优缺点

    这篇文章主要介绍了关于spring依赖注入的方式以及优缺点,依赖注入,是IOC的一个方面,是个通常的概念,它有多种解释,这概念是说你不用创建对象,而只需要描述它如何被创建,需要的朋友可以参考下
    2023-07-07
  • 浅谈标签和JLabel类构造方法

    浅谈标签和JLabel类构造方法

    这篇文章主要介绍了标签和JLabel类构造方法,具有一定参考价值,需要的朋友可以参考下。
    2017-09-09
  • Spring @Order注解使用详解

    Spring @Order注解使用详解

    注解@Order或者接口Ordered的作用是定义Spring IOC容器中Bean的执行顺序的优先级,而不是定义Bean的加载顺序,Bean的加载顺序不受@Order或Ordered接口的影响
    2022-08-08
  • IntelliJ IDEA(2019)之mybatis反向生成的实现

    IntelliJ IDEA(2019)之mybatis反向生成的实现

    这篇文章主要介绍了IntelliJ IDEA(2019)之mybatis反向生成,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • Jenkins初级应用之Invoke Phing targets插件配置

    Jenkins初级应用之Invoke Phing targets插件配置

    这篇文章主要为大家介绍了Jenkins初级应用之Invoke Phing targets的插件配置,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪<BR>
    2022-04-04
  • Java集合List和Map互转的方法总结

    Java集合List和Map互转的方法总结

    有时候我们需要将给定的List转换为Map,或者Map转换为List,本文主要介绍了Java集合List和Map互转的方法总结,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09
  • java几种排序算法的实现及简单分析

    java几种排序算法的实现及简单分析

    这篇文章主要介绍了java几种排序算法的实现及简单分析,实例分析了插入排序、希尔排序、选择排序等常用排序算法,并分析了各个算法的优劣,需要的朋友可以参考下
    2015-05-05
  • java实现文件读写与压缩实例

    java实现文件读写与压缩实例

    这篇文章主要介绍了java实现文件读写与压缩实例,有助于读者加深对文件操作的理解,需要的朋友可以参考下
    2014-07-07
  • Java枚举之EnumSet详解

    Java枚举之EnumSet详解

    这篇文章主要介绍了Java枚举之EnumSet详解,使用时进行与或运算,但是定义多了之后,会很乱、臃肿,编写容易出错,EnumSet可以实现类似的功能,且使用起来很简洁,需要的朋友可以参考下
    2023-12-12
  • MyBatis 引入映射器的方法

    MyBatis 引入映射器的方法

    本文通过实例代码给大家分享mybatis 引入映射器的方法,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-09-09

最新评论