Bài giảng Công nghệ Java: Bài 2.2 - Nguyễn Hữu Thể
Số trang: 14
Loại file: pdf
Dung lượng: 470.33 KB
Lượt xem: 15
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Bài giảng Công nghệ Java - Bài 2.2: Servlet. Bài này cung cấp cho người học những nội dung kiến thức về: Servlet init, web.xml, annotation, forward, redirect. Mời các bạn cùng tham khảo để biết thêm các nội dung chi tiết.
Nội dung trích xuất từ tài liệu:
Bài giảng Công nghệ Java: Bài 2.2 - Nguyễn Hữu ThểCÔNG NGHỆ JAVA Nguyễn Hữu Thể Bài 2: Servlet 1 Nội dung▪ Servlet init()▪ web.xml▪ Annotation▪ Forward▪ Redirect 2 Interface ServletMethod Description initializes the servlet. It is the life cycle method of servlet andpublic void init(ServletConfig config) invoked by the web container only once. provides response for the incomingpublic void service(ServletRequest request. It is invoked at eachrequest,ServletResponse response) request by the web container. is invoked only once and indicatespublic void destroy() that servlet is being destroyed.public ServletConfig getServletConfig() returns the object of ServletConfig. returns information about servletpublic String getServletInfo() such as writer, copyright, version etc. 3package com.javatech.tutorial.servlet;public class InitParamServlet extends HttpServlet { VD: InitParamServlet.java private static final long serialVersionUID = 1L; + init(): Khởi tạo giá trị cho private String email; email public InitParamServlet() { } + doGet(): Khởi tạo giá trị cho @Override email2 public void init(ServletConfig config) throws ServletException { super.init(config); // Lấy giá trị của tham số khởi tạo. Cấu hình trong web.xml. this.email = config.getInitParameter(email1); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Lấy giá trị của tham số khởi tạo theo một cách khác. String email2 = this.getServletConfig().getInitParameter(email2); ServletOutputStream out = response.getOutputStream(); out.println(); out.println(); out.println(Init Param); out.println(email1 = + this.email + ); out.println(email2 = + email2 + ); out.println(); out.println(); } 4} web.xmlJavaTech_Servlet Thiết lập thông tin cấu hình initParamServlet com.javatech.tutorial.servlet.InitParamServlet email1 abc@example.com email2 def@example.com initParamServlet /initParam index.html index.jsp 5 Sử dụng Annotation trong Servlet@WebServletTo declare a servlet.@WebInitParamTo specify an initialization parameter.@WebFilterTo declare a servlet filter.@WebListenerTo declare a WebListener 6 @WebServlet: VD1@WebServlet(/HelloServlet)public class HelloServlet extends HttpServlet { private static final long serialVersionUID = 1L; public HelloServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println(Hello, Good afternoon!); }} 7package com.javatech.tutorial.servlet; VD2: @WebServlet, @WebInitParam// Có thể cấu hình một hoặc nhiều URL pattern để truy cập vào Servlet này.@WebServlet(urlPatterns = { /annotationExample, /annExample }, initParams = { @WebInitParam(name = email1, value = abc@example.com), @WebInitParam(name = email2, value = xyz@example.com) })public class AnnotationExampleServlet extends HttpServlet { private static final long serialVersionUID = 1L; private String email1; public AnnotationExampleServlet() { } @Override public void init(ServletConfig config) throws ServletException { super.init(config); this.email1 = config.getInitParameter(email1); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String email2 = this.getServl ...
Nội dung trích xuất từ tài liệu:
Bài giảng Công nghệ Java: Bài 2.2 - Nguyễn Hữu ThểCÔNG NGHỆ JAVA Nguyễn Hữu Thể Bài 2: Servlet 1 Nội dung▪ Servlet init()▪ web.xml▪ Annotation▪ Forward▪ Redirect 2 Interface ServletMethod Description initializes the servlet. It is the life cycle method of servlet andpublic void init(ServletConfig config) invoked by the web container only once. provides response for the incomingpublic void service(ServletRequest request. It is invoked at eachrequest,ServletResponse response) request by the web container. is invoked only once and indicatespublic void destroy() that servlet is being destroyed.public ServletConfig getServletConfig() returns the object of ServletConfig. returns information about servletpublic String getServletInfo() such as writer, copyright, version etc. 3package com.javatech.tutorial.servlet;public class InitParamServlet extends HttpServlet { VD: InitParamServlet.java private static final long serialVersionUID = 1L; + init(): Khởi tạo giá trị cho private String email; email public InitParamServlet() { } + doGet(): Khởi tạo giá trị cho @Override email2 public void init(ServletConfig config) throws ServletException { super.init(config); // Lấy giá trị của tham số khởi tạo. Cấu hình trong web.xml. this.email = config.getInitParameter(email1); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Lấy giá trị của tham số khởi tạo theo một cách khác. String email2 = this.getServletConfig().getInitParameter(email2); ServletOutputStream out = response.getOutputStream(); out.println(); out.println(); out.println(Init Param); out.println(email1 = + this.email + ); out.println(email2 = + email2 + ); out.println(); out.println(); } 4} web.xmlJavaTech_Servlet Thiết lập thông tin cấu hình initParamServlet com.javatech.tutorial.servlet.InitParamServlet email1 abc@example.com email2 def@example.com initParamServlet /initParam index.html index.jsp 5 Sử dụng Annotation trong Servlet@WebServletTo declare a servlet.@WebInitParamTo specify an initialization parameter.@WebFilterTo declare a servlet filter.@WebListenerTo declare a WebListener 6 @WebServlet: VD1@WebServlet(/HelloServlet)public class HelloServlet extends HttpServlet { private static final long serialVersionUID = 1L; public HelloServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println(Hello, Good afternoon!); }} 7package com.javatech.tutorial.servlet; VD2: @WebServlet, @WebInitParam// Có thể cấu hình một hoặc nhiều URL pattern để truy cập vào Servlet này.@WebServlet(urlPatterns = { /annotationExample, /annExample }, initParams = { @WebInitParam(name = email1, value = abc@example.com), @WebInitParam(name = email2, value = xyz@example.com) })public class AnnotationExampleServlet extends HttpServlet { private static final long serialVersionUID = 1L; private String email1; public AnnotationExampleServlet() { } @Override public void init(ServletConfig config) throws ServletException { super.init(config); this.email1 = config.getInitParameter(email1); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String email2 = this.getServl ...
Tìm kiếm theo từ khóa liên quan:
Công nghệ Java Bài giảng Công nghệ Java Lập trình Java Kỹ thuật lập trình Interface servlet Annotation trong Servlet Servlet requestGợi ý tài liệu liên quan:
-
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 264 0 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 205 0 0 -
Giới thiệu môn học Ngôn ngữ lập trình C++
5 trang 194 0 0 -
Bài giảng Nhập môn về lập trình - Chương 1: Giới thiệu về máy tính và lập trình
30 trang 164 0 0 -
Luận văn: Nghiên cứu kỹ thuật giấu tin trong ảnh Gif
33 trang 153 0 0 -
Báo cáo thực tập Công nghệ thông tin: Lập trình game trên Unity
27 trang 118 0 0 -
Giáo trình về phân tích thiết kế hệ thống thông tin
113 trang 114 0 0 -
Excel add in development in c and c phần 9
0 trang 109 0 0 -
LUẬN VĂN: Tìm hiểu kỹ thuật tạo bóng cứng trong đồ họa 3D
41 trang 108 0 0 -
Bài giảng Kỹ thuật lập trình - Chương 10: Tổng kết môn học (Trường Đại học Bách khoa Hà Nội)
67 trang 106 0 0