欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

java中response对象用法实例分析

时间:2021-03-07 10:30:49|栏目:JAVA代码|点击:

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

上一篇:一文读懂JAVA中HttpURLConnection的用法

栏    目:JAVA代码

下一篇:springboot 配置日志 打印不出来sql的解决方法

本文标题:java中response对象用法实例分析

本文地址:http://www.codeinn.net/misctech/75926.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有