HttpServletResponse(一)
//重定向 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // //1.设置状态码 // response.setStatus(302); // //2.设置响应头location // response.setHeader("Location", "/web01/servlet03"); //开发中专门重定向方法 response.sendRedirect("/web01/servlet03");
//设置状态码 response.setStatus(404); response.getWriter().write("hello hello...");
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //定时刷新(5秒后跳转) response.setHeader("refresh", "5;url=http://www.baidu.com"); response.getWriter().write("hello dandan..."); }
jsp文件
<title>Insert title here</title> <script type="text/javascript"> var time =5; //全局加载完再执行 window.onload =function(){ var second =document.getElementById("second"); timer =setInterval(function(){ second.innerHTML =time; time--; if(time==0){ clearInterval(timer); location.href="http://www.baidu.com"; } }, 1000); } </script> </head> <body> 恭喜注册成功!<span style="color:red " id="second">5</span>秒后跳转 如果不跳,请点击<a href="http://www.baidu.com">百度</a> </body> </html>
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // //设置缓冲区的编码 // response.setCharacterEncoding("utf-8"); // //告知客户端用utf-8解码 // response.setHeader("Content-Type", "text/html;charset=utf-8"); //开发中专门解码方法 response.setContentType("text/html;charset=utf-8"); response.getWriter().write("中国");
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步