开发者

Java web application web.xml parameter into POJO

I have an email utility class that is shared by all my intranet web apps which emails an employee their forgotten password (well the class is copied into each webapp). I need to brand the email with appropriate Subject Line, ReplyTo Point Of Contact, Application Name, etc that matches the app that is calling it.

I'm able to pass these as parameters but the way I do it is by including an initialize.jsp in my header on the login webpage.

<% request.setAttribute("siteEmail", "Commitment@<domain>.com");
   request.setAttribute("siteName", "Commitment Report Database");
   request.setAttribute("sitePOCEmail", "smith@<domain>.com");
%>

These request parameter are then stored into a DTO by a Struts similar Action

public class STKRequestPasswordAction implements ControllerAction {
public STKRequestPasswordAction() {}

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

ApplicationConfig ac = new ApplicationConfig();
ac.setAppEmail(request.getAttribute("siteEmail").toString());
ac.setAppName(request.getAttribute("siteName").toString());
ac.setPocEmail(request.getAttribute("sitePOCEmail").toString());
request.setAttribute("ac", ac);

userForm.requestPassword(Logi开发者_C百科nUser, ac);

The userForm.requestPassword method

public void requestPassword(STKUser loginUser, ApplicationConfig ac) {

validates the inputed userID requesting the password, builds the StringBuffer message, and then calls the Email Utility class.

EmailUtils.sendEMailByBadge(loginUser.getBadge(), strFrom, strReplyTo, strSubject, emailBody.toString(), strSMTP, true);

I'd like to refactor my initializing parameters out of the included jsp and put them into the web.xml, but I am not sure where I should be reading the values. I tried this in the POJO

String strFrom = getServletContext().getInitParameter("siteEmail"); but there is no servletContext in the POJO. I would imagine it could go in my servlet but my servlet uses a mapping as follows:

actionMap.put(null, new STKLoginSubmitAction());
actionMap.put("chkpass", new STKLoginSubmitAction());
actionMap.put("sqlReset", new STKRequestPasswordAction());
String op = request.getParameter("method");
ControllerAction action = (ControllerAction) actionMap.get(op);

I plan to move ApplicationConfig ac to the servlet and change the method signature for STKRequestPasswordAction to pass `ac'. Is this the best approach or am I missing the picture?


In your web.xml:

<env-entry>
    <env-entry-name>myEntryName</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>myEntryValue</env-entry-value>
</env-entry>

In your Java code:

// Do once
Context env = (Context) new InitialContext().lookup("java:comp/env");

// Do for every entry
String entryValue = (String) env.lookup("myEntryName");

Using JNDI you can read your settings every where in your webapp and also keep your configs out of your web.xml if you need, in context.xml for Tomcat or jetty-env.xml for Jetty for example (though format is different).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜