您的位置:首页 > 编程语言 > Java开发

java中response对象用法实例分析

2015-12-30 15:55 946 查看

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

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

复制代码 代码如下: response.setHeader("refresh",60);
而实现3秒后浏览器加载新页面//www.jb51.net的语句为:
复制代码 代码如下: response.setHeader("refresh","3;URL=//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 response