目录
1、servlet类的创建(不使用注解)
2、servlet类的创建(使用注解)
3.Servlet 生命周期
4.HttpServletRequest接口(request)
4.1、作用
4.2、常用方法
5.HttpServletResponse接口(response)
1、servlet类的创建(不使用注解)
1.创建普通类,继承HttpServlet类,重写doGet和doPost方法
public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("服务器接收到客户端get请求"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("服务器接收到post请求"); } }
2.web.xml的配置
2、servlet类的创建(使用注解) name lijia /index.html /index.jsp /defualt.html /defualt.jsp MyServlet com.li.web.MyServlet MyServlet /my1 404 /error/error404.jsp java.lang.Exception /error/error500.jsp
@WebServlet():括号中给一个值默认为访问路径
@WebServlet("/MyServlet001") public class MyServlet001 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } } 3.Servlet 生命周期
-
加载、实例化、初始化init() (第一次访问时,只加载一次)
-
服务阶段 service() (处理请求,执行doXX() )
-
销毁 destroy() (服务器关闭)
public class MyServlet extends HttpServlet { public MyServlet(){ System.out.println("对象被创建了。。。"); } @Override public void init(ServletConfig config) throws ServletException { System.out.println("init初始化方法被调用。。。"); System.out.println("name的值为"+config.getServletContext().getInitParameter("name")); } @Override public void destroy() { System.out.println("对象被销毁了。。。。。"); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("服务器接收到客户端get请求"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("服务器接收到post请求"); } } 4.HttpServletRequest接口(request) 4.1、作用
4.2、常用方法1.提取客户端请求信息(表单信息,HTTP请求报头信息,cookie)
2.修改字符集
3.在服务器端保存值
4.WEB资源跳转——请求转发
1.String getParameter(String name) 获取请求中参数为name的参数值。
2.String[] getParameterValues(String name) 获取请求中参数名为name的参数值的集合
3.Enumeration getParameterNames() 获取请求中所有参数名的集合
4.ServletInputStream getInputStream() 获取客户端请求的数据
5.String getMethod() 获取HTTP请求方法
6.String getRemoteAddr() 获取客户端的IP
7.setCharacterEncoding() 修改网页提交过来数据的字符编码方式
8.getRequestDispatcher(xxx.jsp).forward(request,response) 请求转发
@WebServlet("/MyServlet001") public class MyServlet001 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置页面提交数据的编码格式 request.setCharacterEncoding("UTF-8"); //获取页面表单数据 String username = request.getParameter("username"); System.out.println("姓名:"+username); //获取多选参数 String sex = request.getParameter("sex"); System.out.println("性别:"+sex); String[] addrs = request.getParameterValues("addr"); System.out.println(Arrays.toString(addrs)); //获取参数列表名 Enumeration5.HttpServletResponse接口(response)names = request.getParameterNames(); while (names.hasMoreElements()){//判断是否有枚举值 System.out.println(names.nextElement());//获取 } request.setAttribute("username","xxx"); request.getRequestDispatcher("text.jsp").forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }
常用方法
1.void setContentType(String type) 设置输出内容(MIME)类型
2.void setContentLength(int length) 设置响应报文的长度
3.PrintWriter getWriter() 返回可以向客户端发送字符数据的PrinterWriter对象
4.ServletOutputStream getOutputStream() 返回可以向客户端发送二进制数据的输出流对象
5.void addcookie(cookie c) 向报头插入一个cookie
6.void sendRedirect() 重定向
7.setCharacterEncoding() 修改响应到网页的字符编码方式
resp.sendRedirect("/demo001/index.jsp?login=1");
常用的MIME类型
- text/plain 纯文本
- text/html HTML文档
- image/图像格式 图片,如image/gif
- application/msword Microsoft Word 文档
- application/vnd.ms-excel Excel电子表格
- application/octet-stream 未经识别或二进制的数据
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //响应文档类型 resp.setContentType("text/plain"); //响应字符集 resp.setCharacterEncoding("UTF-8"); //获得输出流 PrintWriter out = resp.getWriter(); out.println(""); out.println("zyw response "); out.println(""); out.println("hello"); out.println(""); out.println(""); out.flush(); out.close(); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)