Servlet入门第三个小例子
2016-01-19 17:09
204 查看
Refresh的响应头5秒后定时跳转
package com.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* 演示定时刷新 设置一个Refresh,它表示定时刷新! */ response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8"); /* * 下面是用来发送响应体! */ PrintWriter out = response.getWriter(); out.print("欢迎张三登录,5秒后跳转到主页"); /* * 设置名为Refresh的响应头 */ response.setHeader("Refresh","5;URL=/Servlet04/BServlet"); } }
package com.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class BServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); out.println("跳转成功,已成功跳转到主页"); } }
字符串转换成字节输出
package com.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 字符串转换成字节 */ String s="abcdefghijklmnopqwrestuvwxyz"; byte[] bytes=s.getBytes(); response.getOutputStream().write(bytes); } }
相关文章推荐
- JSP基础
- Android模拟器ROOT
- Apache Maven教程
- Js中sort()方法的用法
- Sim-panalyer简介
- Beta-Binomial 共轭
- java 7 forkjoin并行框架的源码详究
- 常用的安卓UI相关的工具集合
- RecyclerView
- 优雅的App完全退出方案(没有任何内存泄漏隐患)
- 多线程初探(八)
- 写给开发者:记录日志的10个建议
- Java内存模型FAQ(十)volatile是干什么用的
- Redis-3.0.6 集群部署集成SpringJava工程-----spring集成
- 详解在C++中显式默认设置的函数和已删除的函数的方法
- RecyclerView match_parent 不起作用的解决方法
- 2016-1-19(移动触摸事件以及事件的一些属性引申)
- 关于java hashmap的心得
- Java NIO概述
- poj1185炮兵阵地