ĐẠI HỌC QUỐC GIA TPHCM
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN
Gvhd: Th.s Nguyễn Quang Minh
Location and purpose of web.xml
Custom URLs
Initialization parameters
Servlets
JSP Pages
Preloading pages
Welcome pages
Error pages
• Location
Eclipse: WebContent/WEB INF/web xml
Deployed: webAppName/WEB-INF/web.xml
install_dir/conf/web.xml is Tomcat-specific! Ignore it!
• When processed
Only required to be read when server (app) starts
Tomcat monitors web.xml and reloads Web app when web.xml
changes Eclipse redeploys app when web xml changes. Eclipse redeploys app when
web.xml changes.
• Basic format
Custom URLs
(Servlet Mappings)
Default servlet URL:
http://host/webAppPrefix/servlet/ServletName
Convenient during development, but wrong
for deployment
Init parameters, security settings, filters, etc.
are
associated only with custom URLs
Default URL is long and cumbersome
=> How to make that URL shorter ???
•
How to make The URL shorter ???
USE WEB.XML
USE @WEBSERVLET(“”)
Using web.xml:
Servlet code:
web.xml entry (in ...)
package myPackage;...
public class MyServlet extends HttpServet { ... }
Give name to servlet
MyNamemyPackage .MyServlet
Give address (URL mapping) to servlet
MyName/MyAddress
Result:
http://hostname/webappName/MyAddress
USING @WebServlet(“”):
Before “public class MyServlet extends
HttpServet”, add @WebServlet(“/MyAddress”)
Examble:
Note: in both solution, the address must start
with “/”
You should always use custom URLs on deployed
projects
URLs look cleaner and simpler and shorter.
URLs have more meaningful names– URLs have
more meaningful names
You don't expose possibly proprietary class file
names
You can use web.xml to assign init params laters
Does not work with …/servlet/myPackage.MyServlet
You can apply filters and security settings later
(via web xml) in a more predictable and
controllable manner
Init param
Init Param use to define data before the
servlet start
=>How to do that ???
1. Use WEB.XML
2. Use Constuctor
USING Web.xml
Aabc.A
nameDuyTaimail[email protected]A/A
In Servlet, to get the param, we use:
ServletConfig config = getServletConfig();
name =
config.getInitParameter(“param_name");
Examble:
Examble:
Result:
Init param for JSP page
Web.xml:
KhoiTao/first.jsp
nameDuyTaiKhoiTao/first.jsp
JSP file:
First name: <%= name %>
<%!
private String name;
public void jspInit()
{
}
ServletConfig config = getServletConfig();
name = config.getInitParameter("name");
if (name == null) { name = "No first name"; }
%>