• Post Reply Bookmark Topic Watch Topic
  • New Topic

Access the web.xml  RSS feed

 
Gerry Morris
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a newb to the whole java scene and am learning as I go. I am programming a "blind" class of which I am currently not sure what kind of application will be using ( a servlet, JSP, etc). I have some config currently hard coded for testing which I need to move externally. Is it possible to access the web.xml for the entire project to retrieve an init or context paremter without knowing the application type? If not, does someone have an alternate suggestion on how to solve my quandary?

Thanks in advance!
 
Jonas Isberg
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your goal to remove any hard-coded values from your class?
In that case, can't you just pass the values into the constructor of your
class, or use setter methods?

Do you have any (compilable) code?
 
Gerry Morris
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example of the class i am working on, note that the values i need to get externally are host, user & pswd:

------------------------------------------------

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;


public class HelloMail {
public static void main(String args[]) throws Exception {
String host = "smtp.email.host";
String user = "mail_acct_un";
String pswd = "mail_acct_pw";

String from = "senders@email.addr"; ; //sender's email
String to = "recipients@email.addr" ; //receiver's email

Properties props = System.getProperties();
props.put("mail.smtp.host", host);

Session session=Session.getInstance(props,null);

MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(" My Test email ");
message.setText(" Here is the content ");
Transport transport = session.getTransport("smtp");
transport.connect(host,user,pswd);
transport.send(message);
}
}

-------------------------------------------

Please explain further and/or provide an example.

Thanks!
 
Senthil B Kumar
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why dont you go for a properties file.
 
Gerry Morris
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have an example of using a properties file?
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the class has access to the ServletContext of the application, you can get application scope parameters from that.

For properties, you could do something like this:



In this case props.properties must be present inside the com.example package directory as seen by the classpath. However I've heard that some security settings could prevent you from calling getResourceAsStream on the ClassLoader. There's probably a much simpler way of doing all this that I'm not aware of.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!