java中response对象用法实例分析

所属分类: 软件编程 / java 阅读数: 34
收藏 0 赞 0 分享

本文实例讲述了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程序设计有所帮助。

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多