Danh mục

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    
Hoai.2512

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 ...

Tài liệu được xem nhiều: