java - After ends method doPost() runs method doGet() without calling this method in StartServlet -


this question has answer here:

i want validate information user. if user has been entered , trying enter /login page, forward userpage.

as of moment have this:

startservlet

public class startservlet extends httpservlet {      @override     protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {         userdto userdto = (userdto) req.getsession().getattribute("userdto");         if (userdto == null) {             req.getrequestdispatcher(req.getcontextpath() + "/web-inf/pages/login.jspx").forward(req, resp);         } else {             req.getrequestdispatcher(req.getcontextpath() + "/checklogin").forward(req, resp);         }     } } 

validateservlet

public class validateservlet extends httpservlet {     @override     protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {         string email = req.getparameter("email");         string password = req.getparameter("password");         userdto userdto = (userdto) req.getsession().getattribute("userdto");          if (userdto == null) {             userdto = userservice.getinstance().validateuserdto(email, password);         }          if (userdto != null) {             req.getsession().setattribute("userdto", userdto);             if (userdto.getrole() == role.administrator || userdto.getrole() == role.superadministrator) {                 req.getrequestdispatcher(req.getcontextpath() + "/admin/catalog").forward(req, resp);             } else {                 req.getrequestdispatcher(req.getcontextpath() + "/web-inf/pages/user/catalog/catalog.jspx").forward(req, resp);             }         } else {             string errormsg = "email or password incorrect";             req.setattribute("error", errormsg);             req.getrequestdispatcher(req.getcontextpath() + "/web-inf/pages/login.jspx").forward(req, resp);         }     }      @override     protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {         dopost(req, resp);     } } 

admincatalogservlet

public class admincatalogservlet extends httpservlet {      @override     protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {         list<itemdto> itemsdto = itemservice.getinstance().getallitemsdto();         req.setattribute("itemsdto", itemsdto);         req.getrequestdispatcher(req.getcontextpath() + "/web-inf/pages/admin/catalog/catalog.jspx").forward(req, resp);     }      @override     protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {         dopost(req, resp);     } } 

web.xml

<servlet>         <servlet-name>startservlet</servlet-name>         <servlet-class>...startservlet</servlet-class>     </servlet>     <servlet-mapping>         <servlet-name>startservlet</servlet-name>         <url-pattern>/</url-pattern>         <url-pattern>/login</url-pattern>     </servlet-mapping>     <servlet>         <servlet-name>validateservlet</servlet-name>         <servlet-class>...validateservlet</servlet-class>     </servlet>     <servlet-mapping>         <servlet-name>validateservlet</servlet-name>         <url-pattern>/checklogin</url-pattern>     </servlet-mapping> <servlet>         <servlet-name>admincatalogservlet</servlet-name>         <servlet-class>...admincatalogservlet</servlet-class>     </servlet>     <servlet-mapping>         <servlet-name>admincatalogservlet</servlet-name>         <url-pattern>/admin/catalog</url-pattern>     </servlet-mapping> 

problem: starting startservlet, when calling / or /login. enter login, password form (method = post) , submit validateservlet.

after forward admincatalogservlet (from prospective of admin). forward jsp , our browser show page. (it 2 requests database) user , items. think, all, but...then tomcat run again method doget() in startservlet, take user session , forward validateservlet method get. , repeated request database items.

why happening? thanks.

update: if delete doget() method validateservlet or admincatalogservlet, problem goes away, don't work when trying enter /login when have logged in.

update2: have traced, advised in comments (maybe not right). first first request (post), set of additional data of bootstrap. @ end there second request on /validate get method.

update2: solve problem, have changed startservlet mapping / "". means request host:port/ redirected servlet. problem due fact that, since bootstrap connected, browser sent request favicon, css , etc. host:port/favicon. because startservlet mapped on /, received request , method get started. more information can read defaultservlet in tomcat

trying make , post same thing in servlets counterproductive. don't conflate , post, each has distinct role.

use retrieving information, displaying pages. gets bookmarkable , browser can cache them. admincatalogservlet executes query , stashed result in request attribute, forwards jsp. should using only.

use post receiving form data posted server client. validation should code accepts data, opposed being in separate servlet.

when post something, don't forward jsp. instead have servlet redirect method. redirecting lets user have valid bookmarkable url. redirecting method lets servlet implementing take responsibility request data needed jsp fetched consistently.

if use logging in user's userid , password in url , logged web server , every router handles request.

also can use servlet filter send user login page. lot less trouble making servlets checking , forwarding.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -