您的位置:首页 > 理论基础 > 计算机网络

登陆成功5s后倒计时跳转(http响应头refresh,location)

2017-10-26 13:24 811 查看






package javapack;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class httpservlet extends HttpServlet {

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");//解决向页面输出中文乱码,而且还可以写HTML
final long serialVersionUID=1L;
//接收表单参数
String username=request.getParameter("username");//通过input的name值获得输入的username
String password=request.getParameter("password");
//封装到实体对象
User user=new User();
user.setUsername(username);//将username与password封装到user对象中
user.setPassword(password);
//调用业务层处理数据
userservice us=new userservice();
try {
User existuser=us.login(user);//将user传入到login方法判断用户是否在数据库中存在
//根据处理结果显示信息(页面跳转)
if(existuser==null){
response.getWriter().println("<h1>登录失败</h1>");
}
else{
response.getWriter().println("<h1>登录成功,你好,"+existuser.getUsername()+"</h1></br>");
response.getWriter().println("<h1>5s后页面跳转。。。。</h1>");
response.setHeader("Refresh","5;url=1.html");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request, response);//post方式和get方式执行同一方法
}

}方法2,使用页面定向技术,现将页面定向到success。html,然后再从HTML文件中编写js语句,使得页面倒计时跳转
package javapack;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class httpservlet extends HttpServlet {

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");//解决向页面输出中文乱码,而且还可以写HTML
final long serialVersionUID=1L;
//接收表单参数
String username=request.getParameter("username");//通过input的name值获得输入的username
String password=request.getParameter("password");
//封装到实体对象
User user=new User();
user.setUsername(username);//将username与password封装到user对象中
user.setPassword(password);
//调用业务层处理数据
userservice us=new userservice();
try {
User existuser=us.login(user);//将user传入到login方法判断用户是否在数据库中存在
//根据处理结果显示信息(页面跳转)
if(existuser==null){
response.getWriter().println("<h1>登录失败</h1>");
}
else{
/*response.getWriter().println("<h1>登录成功,你好,"+existuser.getUsername()+"</h1></br>");
response.getWriter().println("<h1>5s后页面跳转。。。。</h1>");
response.setHeader("Refresh","5;url=1.html");*/
response.setStatus(302);//状态码重定向
response.setHeader("Location", "success.html");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request, response);//post方式和get方式执行同一方法
}

}
<!DOCTYPE html>
<html>
<head>
<title>success.html</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">
<meta http-equiv="refresh" content="5; url=1.html">
<!-- 编写http响应头refresh,使得页面5s后跳转到1.html -->
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
//使得页面倒计时5s
var time = 5;
window.onload = function() {
setInterval('changetime()', 1000);
}
function changetime() {
time--;
document.getElementById("s").innerHTML = time;
}
</script>
</head>
<body>
<h1>登录成功</h1>
<h1>
界面将在<span id="s">5</span>s后跳转
</h1>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: