HelloServlet
来源:互联网 发布:last game 软件 编辑:程序博客网 时间:2024/05/02 01:38
package com.servlet.test;import java.io.*;import javax.servlet.http.*;import javax.servlet.*;import javax.servlet.ServletException;// 继承的类的可访问类型必须是public,否则servlet容器不能进行实例化public class HelloServlet extends HttpServlet{ // 1、servlet被装载后,Servlet容器创建一个Servlet实例public HelloServlet(){ super();System.out.println("create...");}// 2、Servlet实例化后,Servlet容器自动调用init()进行初始化,该方法在整个生命周期内仅被调用一次public void init() throws ServletException{ super.init();System.out.println("init...");}// 3、在发生请求时,Servlet容器调用service(request,response)方法,然后根据对应的请求去调用相应的doXXX方法public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException{System.out.println("service...");super.service(req, res);}// 4、servlet的结束// 当WEB应用被终止,或Servlet容器终止运行,或Servlet容器重新装载Servlet新实例时,// Servlet容器会先调用Servlet的destroy()方法,在destroy()方法中可以释放掉Servlet所占用的资源。public void destroy(){System.out.println("destroy...");super.destroy();}// 3.1、对请求的响应public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{System.out.println("do get...");doServer(request, response);}// 3.2、对请求的响应public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{System.out.println("do post...");doServer(request, response);}private void doServer(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{// 设置请求以及响应的内容类型以及编码方式,一般要写在对请求相应的第一行 response.setContentType("text/html;charset=GB2312"); // 设置为html类型,且编码格式为gb2312 request.setCharacterEncoding("GB2312");PrintWriter pwOut = response.getWriter();pwOut.print("<html><head><title>Test Servlet</title></head>");pwOut.print("<body>");pwOut.print("<p style = " + "color:red" + "><b>测试 servlet 02</b></p>");pwOut.print("</body>");pwOut.print("</html>");}}
说明:
1、如果用Notepad++进行代码编写,建议设置文件编码格式为ANSI;试了其它几种格式在显示中文的时候依然乱码;
0 0
- HelloServlet
- Servlet: HelloServlet 404错误
- Servlet初识-HelloServlet
- HTTP Status 404 - /servlet/helloservlet
- 用Ant实现HelloServlet程序
- Servlet学习之HelloServlet生命周期
- Allocate exception for servlet helloServlet
- Tomcat 8.x HelloServlet模板
- 第一个Servlet程序 HelloServlet
- 学习笔记-JavaEE HelloServlet 部署
- 解决"HTTP Status 404 - /servlet/HelloServlet"错误
- JSP&&SERVLET学习笔记(一):HelloServlet
- [Servlet]HelloServlet示例:Servlet的基本元素
- 创建第一个servlet程序--HelloServlet
- JSP&Servlet学习笔记(2)HelloServlet解析
- 我的第一个Servlet(二)——HelloServlet
- IllegalAccessException: Class StandardWrapper can not access HelloServlet class with modifier xxx
- 第一个Servlet小程序之HelloServlet详解,及form表单 action如何填写等问题。
- 关于Linux系统中“目录项”的解释
- android UI 开源组件
- SQL Server 2000性能测试分析 sql 表最大记录数
- c# 不用IDE编程
- java字符串全排列的实现方式
- HelloServlet
- source insight 里编辑的时候,每次粘贴后,光标停留在粘贴内容的左面
- Leetcode Binary Tree Zigzag Level Order Traversal
- 使用isolinux制作liveUSB
- 组织结构图 可以任意拖拽的div
- Struts2下载zip压缩文件-不生成临时文件
- Linux多节点互信配置
- Android源码结构
- C#清除.pdb和其他文件