登陆成功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>
相关文章推荐
- AngularJs HTTP响应拦截器实现登陆、权限校验
- 响应头location 页面跳转
- window.location.href='$_SERVER[HTTP_REFERER]'; 跳转处理前一个页面的url
- dede织梦后台登陆成功后又跳转至登陆页面怎么办
- javascript 计算及倒计时 溫馨提示:登入成功後最少30秒才統計一次流覽數量,您已经登陆 "+minutes+" 分 "+seconds+" 秒
- http 响应头之location
- 2014.08.25 周一-查询数据没导入成功-http响应头
- 登陆成功后几秒中跳转
- 用javascript语言编写一个小程序:在一个文本框(用户名框)中按回车键时,跳转到另一个文本框(密码框)中,密码框回车之后,打一个登陆成功。点击button也是登陆成功。
- http 响应头之location
- Android利用CountDownTimer实现倒计时功能 Android实现停留5s跳转到登录页面
- JavaWeb登陆成功后跳转到上一个页面
- http协议:http请求、http响应、间隔时间跳转页面、禁用浏览器缓存
- http协议6---响应信息头案例一(请求重定向(相当于超链接跳转页面))
- http 响应头之location
- 登陆成功后自动计算秒数执行跳转
- 【转】http响应状态代码含义及跳转的类型
- http协议7---响应信息头案例二(实现定时刷新和隔n秒之后跳转到另外的资源)
- shiro登陆成功不跳转successUrl的问题
- 登陆成功后 自动查秒 跳转