Tutorials to .com

Tutorials to .com » Jsp » Xml » Environment variable with the deployment of custom applications on the Web to discuss the scope of layer properties

Environment variable with the deployment of custom applications on the Web to discuss the scope of layer properties

Print View , by: iSee ,Total views: 12 ,Word Count: 1833 ,Date: Mon, 20 Apr 2009 Time: 11:49 PM

Environment variable with the deployment of custom applications <br> sometimes, when we prepared j2ee Web applications, application deployment, we want to provide some flexibility. For example, perhaps you want the application to provide several different versions, each version of the user to meet specific needs. Perhaps some part of the code needs the host name and port information, which will be known when the only deployment. Perhaps you just want to show the data on how the deployment of staff to the application of some flexibility.
You can use environment variables to increase this flexibility. Environment variable can be deployed in the component definition file descriptor parameters. Application components through JNDI to find the environment variables by name, the value of using environment variables or custom applications, said.
All types of application components can use environment variables. Servlet, Enterprise Bean, jsp
Tag pages and user environment variables can be used. Environment variable must be the appropriate components in the deployment descriptor definition file, for example, Web components are defined in the web.xml file, enterprise Bean in the ejb-jar.xml file definition.
For example, suppose you want to e-business applications for the preparation of a servlet. The servlet to send email to customers, to inform the customer has received an order. Servlet you need to authorize the SMTP server specified host, port, login name and password. As a component (servlet) developer, you do not have to know this information, as long as the deployment of those who know it. However, the functional requirements eamil this information, how to provide these servle message?
One way is to use the servlet deployment descriptor file (web.xml) in the environment variables. First of all, for the host name, port, login name, password environment variable definition. Then, write code from the environment through the JNDI environment variables to these values, in the code to use these environment variable values. When deployed in the deployment of the deployment of tools to use for these environment variables to fill in the appropriate value. Procedures for the deployment of run-time extraction of these values are set, use these values to access the server access.

The definition of environment variables <br> components using xml in the Deployment Descriptor file defined in environment variables. If you use the deployment tools (such as the J2EE Reference Implementation procedures brought about by the deployment tool), you can use GUI mode to determine the deployment descriptor. However, we still assume that the following with a text editor and manually edit the deployment descriptor.
Environment variable has four parts:
Description: definition scription> tag within a string.
Name: <env-entry-name> tag in the definition of an string.
Value: <env-entry-value> tag defined in the a value.
Class Name: <env-entry-type> tag defined in the environment variable of the type described is optional text description of the deployment tools in the user interface. It tells the deployment environment entity in reference to determine what to do. In other words, it is a readable description of the deployment of those who tell them how to fill in other values. It is also the deployment of those who tell whether this environment variable is optional.
Environment variable name is relative to the context JNDI name "jndi: comp / env", the components used to find the environment variable name. All environmental variables containers by their registration in the JNDI context.
Environment variable value is the environment variable value should be taken, the format is a string. In addition to the type of characters that a single java.lang.Character outside, allowing for all types of environment variables have the string as a parameter to the constructor. Env-entry-value tag included in the constructor for the value of the string.
The type of environment variable environment variable value is the type of the class name. Must be one of the following types:
l java.lang.Boolean
l java.lang.Byte
l java.lang.Character
l java.lang.Double
l java.lang.Float
l java.lang.Integer
l java.lang.Long
l java.lang.Short
l java.lang.String

SMTP host of examples of environment variables can be as following:
Enter the host name for sending email
</ description>
<env-entry-name> SMTP Host Name </ env-entry-name>
</ env-entry-value>
<env-entry-type> java.lang.String </ env-entry-type>
</ env-entry>

<description> SMTP port number for email
</ description>
<env-entry-name> SMTP Port </ env-entry-name>
<env-entry-value> 2101 </ env-entry-value>
<env-entry-type> java.lang.Integer </ env-entry-type>
</ env-entry>

User authentication for SMTP server
</ description>
<env-entry-name> SMTP User </ env-entry-name>
<env-entry-value> bart </ env-entry-value>
<env-entry-type> java.lang.String </ env-entry-type>
</ env-entry>

Password for SMTP user
</ description>
<env-entry-name> SMTP Password </ env-entry-name>
<env-entry-value> D'oh! </ env-entry-value>
<env-entry-type> java.lang.String </ env-entry-type>
</ env-entry>

<br> To use environment variables in the code is very simple to use environment variables, environment variables to use JNDI to find on the list. Context.lookup attention to the results of methods with the appropriate type of match. As follows:

try (
InitialContext ic = new InitialContext ();
Context ctx = ic.lookup ( "java: comp / env");
String hostname =
(String) (ctx.lookup ( "SMTP Host"));
Integer port = (Integer) (ctx.lookup ( "SMTP Port"));
String user = (String) (ctx.lookup ( "SMTP User"));
String password =
(String) (ctx.lookup ( "SMTP Password"));

sendEmail (
emailText, port, hostname, user, password);
) Catch (NamingException nex) (

Environment variables and servlet initialization parameters <br> in Web applications, servlet initialization parameter can use environment variables in place of the conduct of custom servlet. Servlet developers used in the web.xml definition of init-param tag initialization servlet parameters, servlet code used in the method of servlet initialization parameters javax.servlet.GenericServlet.getInitParameter visit. servlet initialization parameter is the definition of the use of its servlet.
In that case, the custom is for the specific, how the environment variables and servlet initialization parameters to make a choice? The answer to this question depends on the natural range of customization. Role as the scope of global variables is the same as the name of the program space, the role of environmental variables is the JNDI namespace scope. This will result in unnecessary components dependence. When the impact of a custom servlet only when, servlet initialization parameter is the best choice. When the custom involves a number of components, consider the use of environment variables.
Code example of this technique are the two parts of sample code. The first part is a servlet, print the application of all environmental variables. The servlet method Context.listBindings final with a list of all bindings in JNDI context java: com / env the environment variables. Selection of the following code snippet in the source code of the servlet:
public void printEnvEntries (HttpServletRequest req,
HttpServletResponse res)
throws IOException, ServletException (

res.setContentType ( "text / html");
PrintWriter out = res.getWriter ();

try (
InitialContext ic = new InitialContext ();

NamingEnumeration ne =
ic.listBindings ( "java: comp / env");

out.println (
"<HTML> <HEAD> <TITLE> Environment Entries </ TITLE> </ HEAD>");
out.println (
"<BODY> <TABLE BORDER=1> <TR> <TH> Entry </ TH>" +
"<TH> Value </ TH> </ TR>");

while (ne.hasMore ()) (
Binding ncp = (Binding) ne.next ();
String objName = ncp.getName ();
Object objObj = ncp.getObject ();

out.println ( "<TR> <TD>" + objName + "</ TD>");
out.print (
"<TD>" + ObjObj.toString () + "</ TD> </ TR>");
out.println ( "</ TABLE> </ BODY> </ HTML>");

) Catch (Exception e) (
throw new ServletException (e);
This method of java: com / env iteration of each object, and print out the form table for each environment variable name and the text said. Try to deploy the application, take a look at the definition in the deployment descriptor file environment variables. "Run the sample code," a guide on how to deploy your application.
Examples of this technique of the second part of the code is a custom tag, DateTag.java. Explains how to use environment variables to make components (in this case is a custom tag) can be customized.
DataTag is a simple tag, page developers can use this tag to print the date and time server. When used alone ( "<t:date/>"), it uses a standard format to print the date and time. If the format attribute tag defines the format, print on the definition of the use of the format. (Standard definition of the format type SimpleDateFormat syntax).
Can be deployed through the symbols of the definition of environment variables with a date / time format. If the format attribute value DateTag to $ started, then find the tag on the name of the attribute value of environment variable format. For example, the following environment variable is defined in web.xml:
<env-entry-name> LongTimeDateFormat </ env-entry-name> <

Java and XML Articles

Can't Find What You're Looking For?

Rating: Not yet rated


No comments posted.