Bài giảng Web technologies and e-Services - Bài 7, Phần 2: Java servlet - Trường Đại học Bách khoa Hà Nội

Java Servlet  
Objectives  
• Explain the nature of a servlet and its operation  
• Use the appropriate servlet methods in a web  
application  
• Code the extraction of environment entries within a  
servlet  
• Handle HTML forms within a servlet  
• Explain the significance of web application state  
• Explain the purpose and operation of HTTP cookies  
and their role in state management  
• Develop java web application with MVC model  
Free Servlet and JSP Engines (Servlet/JSP  
Containers)  
• Apache Tomcat  
• IDE: NetBeans, Eclipse  
• Some Tutorials:  
• Creating Servlet in Netbeans:  
• Java Servlet Example:  
Compiling and Invoking Servlets  
• Put your servlet classes in proper location  
• Locations vary from server to server. E.g.,  
• tomcat_install_dir/webapps/ROOT/WEB-INF/classes  
• Invoke your servlets (HTTP request)  
• Custom URL-to-servlet mapping (via web.xml)  
Purposes of Web Applications (A single  
WAR file)  
• Organization  
• Related files grouped together in a single directory  
hierarchy.  
• HTML files, JSP pages, servlets, beans, images, etc.  
• Portability  
• Most servers support Web apps.  
• Can redeploy on new server by moving a single file.  
• Separation  
• Each Web app has its own:  
• ServletContext, Class loader  
• Sessions, URL prefix, Directory structure  
Structure of a Web Application  
• JSP and regular Web content (HTML, style sheets,  
images, etc.):  
• Main directory or a subdirectory thereof.  
• Servlets:  
• WEB-INF/classes (if servlet is unpackaged – i.e. in default  
package)  
• A subdirectory thereof that matches the package name.  
• JAR files:  
• WEB-INF/lib.  
• web.xml:  
• WEB-INF  
• Tag Library Descriptor files:  
• WEB-INF or subdirectory thereof  
• Files in WEB-INF not directly accessible to outside clients  
Example Structure  
Java Servlets  
• A servlet is a Java program that is invoked by a web  
server in response to a request  
Server Platform  
Client  
Web  
Server  
Web Application  
Servlet  
Java Servlets  
• Together with web pages and other components,  
servlets constitute part of a web application  
• Servlets can  
• create dynamic (HTML) content in response to a request  
• handle user input, such as from HTML forms  
• access databases, files, and other system resources  
• perform any computation required by an application  
Java Servlets  
• Servlets are hosted by a servlet container, such as Apache  
Tomcat*  
The web server  
Server Platform  
handles the HTTP  
transaction details  
Web  
Server  
The servlet container  
provides a Java  
Virtual Machine  
for servlet execution  
Servlet  
Container  
*Apache Tomcat can be both a web server and a servlet container  
Environment For Developing and Testing  
Servlets  
• Compile:  
• Need Servlet.jar. Available in Tomcat package  
• Setup testing environment  
• Install and start Tomcat web server  
• Place compiled servlet at appropriate location  
Servlet Operation  
Servlet Methods  
• Servlets have three principal methods  
.init()  
invoked once, when the servlet is loaded by the servlet  
container (upon the first client request)  
.service(HttpServletRequest req,  
HttpServletResponse res)  
invoked for each HTTP request  
parameters encapsulate the HTTP request and response  
.destroy()  
invoked when the servlet is unloaded  
(when the servlet container is shut down)  
Servlet Methods  
• The default .service() method simply invokes method-  
specific methods  
• depending upon the HTTP request method  
.doGet()  
.doPost()  
.doHead()  
… etc.  
.service()  
Methods  
Methods  
HTTP Requests  
Comments  
doGet  
doPost  
GET, HEAD  
POST  
Usually overridden  
Usually overridden  
doPut  
PUT  
Usually not overridden  
Almost never overridden  
Almost never overridden  
doOptions  
doTrace  
OPTIONS  
TRACE  
Servlet Example 1  
• This servlet will say "Hello!" (in HTML)  
package servlet;  
import javax.servlet.http.*;  
public class HelloServlet extends HttpServlet {  
public void service(HttpServletRequest req,  
HttpServletResponse res) throws IOException {  
PrintWriter htmlOut = res.getWriter();  
res.setContentType("text/html");  
htmlOut.println("<html><head><title>" +  
"Servlet Example Output</title></head><body>" +  
"<p>Hello!</p>" + "</body></html>");  
htmlOut.close();  
}
}
Servlet Example 2  
import java.io.*;  
import javax.servlet.*;  
import javax.servlet.http.*;  
public class HelloWorld extends HttpServlet {  
public void doGet(HttpServletRequest request,  
HttpServletResponse response)  
throws ServletException, IOException {  
PrintWriter out = response.getWriter();  
out.println("Hello World");  
}
}
Servlet Configuration  
• The web application configuration file, web.xml,  
identifies servlets and defines a mapping from  
requests to servlets  
An identifying name for the servlet (appears twice)  
<servlet>  
<servlet-name>HelloServlet</servlet-name>  
<servlet-class>servlet.HelloServlet</servlet-  
class>  
The servlet's package  
and class names  
</servlet>  
<servlet-mapping>  
<servlet-name>HelloServlet</servlet-name>  
<url-pattern>/hello</url-pattern>  
</servlet-mapping>  
The pathname used to invoke the servlet  
(relative to the web application URL)  
Environment Entries  
• Servlets can obtain configuration information at run-  
time from the configuration file (web.xml)  
• a file name, a database password, etc.  
• in web.xml:  
<env-entry-description>password</env-entry-  
description>  
<env-entry>  
<env-entry-name>UserId</env-entry-name>  
<env-entry-value>Xy87!fx9*</env-entry-value>  
<env-entry-type>java.lang.String</env-entry-type>  
</env-entry>  
Environment Entries  
• in the init() method of the servlet:  
try {  
Context envCtx = (Context)  
(new InitialContext()).lookup("java:comp/env");  
password = (String) envCtx.lookup("password");  
} catch (NamingException e) {  
e.printStackTrace();  
} catch (ClassNotFoundException e) {  
e.printStackTrace();  
}
Tải về để xem bản đầy đủ
pdf 49 trang Thùy Anh 22560 Free
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Web technologies and e-Services - Bài 7, Phần 2: Java servlet - Trường Đại học Bách khoa Hà Nội", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pdfbai_giang_web_technologies_and_e_services_bai_7_phan_2_java.pdf