Configuring a web application

Download Report

Transcript Configuring a web application

Configuring web servers and
web applications
Configuring web servers and web
applications
1
Server configuration vs. application
configuration
• A web server may run several web application
• Server configuration
– Affects all web application running on the server
– Port number
– Reference to username/password database
• Application configuration
–
–
–
–
–
–
Affects only one web application
Welcome files
Mapping of Servlet names
Pages to handle errors
Setting session timeout
Securing the web application
Configuring web servers and web
applications
2
Server configuration
• No standard
– Different web servers are configured in different ways
• Tomcat
– Configuration described in the file server.xml
– TOMCAT_DIR/conf/server.xml
• C:\Program Files\netbeans-5.5\enterprise3\apache-tomcat5.5.17\conf\server.xml
– Used if you run Tomcat outside NetBeans
• C:\Documents and Settings\anders\.netbeans\5.5\apachetomcat-5.5.17_base\conf\server.xml
– Used if you run Tomcat inside NetBeans
Configuring web servers and web
applications
3
Web application configuration
• Standard
– All Java enabled web servers should do it the
same way
• Configuration described in the file WEBINF/web.xml
• C:\andersb\wspj\login\web\WEB-INF\web.xml
• C:\Program Files\netbeans-5.5\enterprise3\apachetomcat-5.5.17\webapps\VerySimple\WEB-INF\web.xml
Configuring web servers and web
applications
4
The structure of web.xml
• The file web.xml is an XML file
– Elements are tagged
– The structure of the file is described in a DTD
(Document Type Definition) file
•
•
•
•
It’s not free format!
Names on elements
Sub-elements
Sequence of elements
• Web.xml can be edited
– Using any text editor
• Easy to make mistakes
– Using NetBeans
• NetBeans guides you
Configuring web servers and web
applications
5
Editing web.xml with NetBeans
Configuring web servers and web
applications
6
Assigning names to URLs
• Servlets are Java classes
– First letter in the Servlet name should be capital
– Servlet class should be in a Java package
• Problem
– URL will look like …/package/ServletName
• Package name is ugly
• Capital letter
• Solution
– Assign the Servlet a virtual name in web.xml
– <servlet>
<servlet-name>virtualname</servlet-name>
<servlet-class>package.ClassName</servlet-class>
</servlet>
– NetBeans does this when you create a new Servlet
Configuring web servers and web
applications
7
Custom URLs
• Sometimes you want to make a virtual
URL for a Servlet (or JSP)
– Usually shorter than the original URL
– <servlet-mapping>
<servlet-name>virtualname</servlet-name>
<url-pattern>/someurl</url-pattern>
</servlet-mapping>
– NetBeans does this when you create a new
Servlet
Configuring web servers and web
applications
8
Remapping the /servlet URL
pattern
• Sometimes you don’t want users to call
Servlets directly
– Only through custom URLs
– Make a special Servlet to say “sorry, no
entrance”
– Redirect all request for /servlet/* to this
special Servlet
– OH Listing 5.5 (page 290), web.xml, disabling
the invoker servlet.
Configuring web servers and web
applications
9
Initialization parameters
• Servlets (and JSP) may need initialization
parameters
–
–
–
–
Usually read and used in the init() method
The init() method is executed ONCE.
The doGet() method is executed at each request
Initialization parameters are (name, value) pair
• Example
– OH Listing 5.7 InitServlet.java
– OH Listing 5.8 web.xml, initialization parameters
Configuring web servers and web
applications
10
Welcome pages
• If a URL does not refer to a file
– http://laerer.rhs.dk/andersb/
– Which file to send in the response?
• Web application holds a prioritized list of
welcome files
– Typically: index.html, index.jsp, etc.
– <welcome-file-list>
–
<welcome-file>index.jsp</welcome-file>
–
<welcome-file>index.html</welcome-file>
– </welcome-file-list>
Configuring web servers and web
applications
11
Pages to handle errors
• Two types of errors
– HTTP status codes (not 2xx)
• Called error codes in the Java terminology
– Java exceptions
• No caught by the Servlets
• Errors can be caught and special pages send as
response
– Like a global try block
– OH Listing 5.13 + 5.14, page 307
– OH Listing 5.17 + 5.18, page 310
Configuring web servers and web
applications
12
Session timeout
• When a session has been inactive for
some time it should be deleted
– To save memory in the server machine
– <session-config>
– <session-timeout>
–
30
–
</session-timeout>
– </session-config>
– Time unit is minutes
Configuring web servers and web
applications
13
Deployment
• The file web.xml is called
– “Web application deployment descriptor”
• Deployment
– Military: Inserting material and people into a
battle
– Application: Moving software into a new
environment
• Moving a web application into a new web server
Configuring web servers and web
applications
14
Deploying a web application
• When you build a web application you
produce a WAR file
– WAR: Web Application Archive
• JAR: Java Application Archive
– Zip file holding class files, web.xml, images,
etc. But not conf/server.xml
• WAR files can be moved to other servers
– Put in the SERVER_HOME/webapps catalog
Configuring web servers and web
applications
15