Welcome to my blog, hope you enjoy reading
RSS

Thursday 25 April 2013

How to get context-param from web.xml in struts app


How to get context-param from web.xml in struts app

The visibility of Context-param from web.xml is in servlet.In Struts the servlet is the ActionServlet. so in ActionServlet  class to get the context param values

example:

ServletContext cg=getServlet().getServletContext();

String driver = cg.getInitParameter("driver");

program:

package com.java.struts.Login;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.DynaValidatorForm;

public class UserLoginAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request,HttpServletResponse response)throws java.lang.Exception
{
String driver,url,user,pwd1;
DynaValidatorForm dform=(DynaValidatorForm) form;
HttpSession session=request.getSession(true);
String result="failed";
try
{
if(session!=null)
{
ServletContext cg=getServlet().getServletContext();
driver = cg.getInitParameter("driver");
url = cg.getInitParameter("url");
user = cg.getInitParameter("user");
pwd1 = cg.getInitParameter("password");
System.out.println(driver+" "+url+" "+user+" "+pwd1);
String username=dform.get("username").toString();
String pwd=dform.get("password").toString();
if(username!=null&&pwd!=null)
{
System.out.println(username+" "+pwd);
System.out.println("success");
}
}
}catch(Exception e)
{
System.out.println(e.toString());
}
return mapping.findForward(result);
}
}

0 comments: