hi,
my "addmeeting.jsp" page takes input from user and on submission passes these parameters to controller servlets. Controller servelet intantiate a handler bean to serve the request. Handler bean with the help of helper bean makes connection to db and inserts the reques parameters. After the insertion process completes controller servlet with the help of "request.getRequestDispatcher(URL).forward(request, response);"
again invokes "addmeeting.jsp".
Now the problem comes when i reload this "admeeting.jsp'" page. On reloading the whole process of passing request to controller servlet and making connection to db happens again.Which is unnecessary . on reloading what i want is the"admeeting.jsp" page should only be reloaded.
One solution to this is instead of using "request.getRequestDispatcher(URL).forward(request, response);" i can use "response.sendRedirect(URL); " .
But by using response.sendRedirect (URL) i lost request because sendRedirect creates a new request.
I can use session.setAttribute instead of request.setAttribute but i don't want to use that . Is there any method which allows me to use "request.getRequestDispatcher(URL).forward(request, response);" and reloads only the page "addmeeting.jsp" instead of the whole process happenning again and again.
i think i m clear abt my problem and for help here is my code
controllerservlet:
public class ControllerServlet extends HttpServlet
{
// Hash table of RequestHandler instance , keyed by request URL
private Map handlerHash = new HashMap();
// Initialize mappings: not implemented here
public void init() throws ServletException
{
ResourceBundle bundle = ResourceBundle.getBundle("Request");
Enumeration enum = bundle.getKeys();
String key = null;
while (enum.hasMoreElements())
{
key = (String) enum.nextElement();
try
{
handlerHash.put(key, Class.forName(bundle.getObject(key).toString()).newInstance());
}
catch (Exception e)
{
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGet(request, response);
}
/**
* Based on the USRL within our application , choose a RequestHandler
* to handle the request and deletgating processing to it.
* Return an HTTP error Code 404 (not found ) id there's no RequestHandler
* mapped to this URL
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
RequestHandler rh = (RequestHandler) handlerHash.get(request.getServletPath());
Set set = handlerHash.entrySet();
Iterator i = set.iterator();
PrintWriter out = response.getWriter();
if (rh == null)
{
out.println("No Request Handler For this Request");
}
else
{
//if we get to here, we have a handler for this request
String viewURL = rh.handleRequest(request, response);
if (viewURL == null)
{
//The RequestHandler has finished output: do nothing
out.println("Nothing to do");
}
else
{
// The RequestHandler told us the view to use
// Forward the response to it
request.getRequestDispatcher(viewURL).forward(request, response);
//response.sendRedirect(viewURL);
}
}
}
}
the request handler interface
public interface RequestHandler
{
/**
* @return the URL of the view that should render the response
* (probably a jsp) , or null to indicate that the response has been
* generated already and processing is complete.
*/
String handleRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException;
}
handler class
public String handleRequest(HttpServletRequest request,HttpServletResponse response)
{
HttpSession session = request.getSession(true);
Boolean validity =(Boolean)session.getAttribute("validity");
if(validity!=null&&validity.booleanValue()==true)
{
String mnostr = request.getParameter("meetingno");
String mplace = request.getParameter("meetingplace");
String mdate = request.getParameter("meetingdate");
String mtime = request.getParameter("meetingtime");
String buttontype = request.getParameter("buttontype");
request.setAttribute("buttontype",buttontype);
int mno=0;
if(mnostr!=null) mno=Integer.parseInt(mnostr);
try
{
helperpkg.AddMetting.addMeeting(mno,mplace,mdate,mtime);
request.setAttribute("queryResult","Meeting Successfully Added to Database");
}
catch (ClassNotFoundException e)
{
request.setAttribute("queryResult",e.toString());
}
catch (SQLException e)
{
request.setAttribute("queryResult",e.toString());
}
catch (Exception e)
{
request.setAttribute("queryResult",e.toString());
}
return "addmeeting.jsp";
}
else return "authentication.jsp";
}
}
and this is the helper class
public class AddMetting
{
public static void addMeeting(int mno ,String mplace,String mdate,String mtime)throws ClassNotFoundException, SQLException
{
DBLayer dbLayer = DBLayer.getInstance();
Connection conn = dbLayer.getConnection();
PreparedStatement preparedStatement = null;
preparedStatement = conn.prepareStatement("INSERT INTO MEETINGINFO(MTNO,MTPLACE,DATE,TIME)" +
" VALUES(?,?,?,?)");
preparedStatement.setInt(1,mno);
preparedStatement.setString(2,mplace);
preparedStatement.setString(3,mdate);
preparedStatement.setString(4,mtime);
preparedStatement.execute();
conn.commit();
preparedStatement.close();
}
}
Manish