i bit confused pattern design patterns web based applications. trying develop simple login example. have login.jsp form , home.jsp login success.
in actionfactory class have login in map , loginaction returns home. actioncontroller class on link above , mapped /pages/*. url should use calling loginaction? if use /login front controller doesn't matches , if use /pages/login matches when "home" comes frontcontroller actions null. should have action home.jsp also? in case how redirect achieved?
actionfactory
public class actionfactory { private static actionfactory instance; public actionfactory() { actions = new hashmap<>(); actions.put("login", new loginaction()); } public static actionfactory getinstance() { if (instance == null) { return new actionfactory(); } else { return instance; } } map<string, action> actions; public action getaction(httpservletrequest request) { return actions.get(request.getpathinfo().substring(1)); } }
servlet
@webservlet(name = "actioncontroller", urlpatterns = {"/pages/*"}) public class actioncontroller extends httpservlet { /** * processes requests both http <code>get</code> , <code>post</code> * methods. * * @param request servlet request * @param response servlet response * @throws servletexception if servlet-specific error occurs * @throws ioexception if i/o error occurs */ protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { try { action action = actionfactory.getinstance().getaction(request); string view = action.execute(request, response); if (view.equals(request.getpathinfo().substring(1))) { request.getrequestdispatcher("/" + view + ".jsp").forward(request, response); } else { response.sendredirect(view); // we'd fire redirect in case of view change result of action (prg pattern). } } catch (exception e) { throw new servletexception("executing action failed.", e); } } // <editor-fold defaultstate="collapsed" desc="httpservlet methods. click on + sign on left edit code."> /** * handles http <code>get</code> method. * * @param request servlet request * @param response servlet response * @throws servletexception if servlet-specific error occurs * @throws ioexception if i/o error occurs */ @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } /** * handles http <code>post</code> method. * * @param request servlet request * @param response servlet response * @throws servletexception if servlet-specific error occurs * @throws ioexception if i/o error occurs */ @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } /** * returns short description of servlet. * * @return string containing servlet description */ @override public string getservletinfo() { return "short description"; }// </editor-fold> }
loginaction
public class loginaction implements action{ @override public string execute(httpservletrequest request, httpservletresponse response) throws exception { return "home"; } }
i guess post you.
how come request.getpathinfo() in service method returns null?
you'd use httpservletrequest#getservletpath() , url /pages/login
easy fix: call page "login.jsp" ,the execute method of action should return string "login" , url test again "/pages/login"
Comments
Post a Comment