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

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>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值