Saturday, January 12, 2019

If two method have same request mapping in controller then which method should be execute?


Can an abstract class have a constructor?

Yes, an abstract class can have a constructor. Consider this:
abstract class Product { 
    int multiplyBy;
    public Product( int multiplyBy ) {
        this.multiplyBy = multiplyBy;
    }

    public int mutiply(int val) {
       return multiplyBy * val;
    }
}

class TimesTwo extends Product {
    public TimesTwo() {
        super(2);
    }
}

class TimesWhat extends Product {
    public TimesWhat(int what) {
        super(what);
    }
}

Spring can you autowire inside an abstract class?

I have that kind of spring setup working
an abstract class with an autowired field
public abstract class AbstractJobRoute extends RouteBuilder {

    @Autowired
    private GlobalSettingsService settingsService;
and several children defined with @Component annotation.

Monday, January 7, 2019

Can we use Filter instead of Servlet?

A filter is an object that can transform the header and content (or both) of a request or response. Filters differ from web components in that filters usually do not themselves create a response. Instead, a filter provides functionality that can be “attached” to any kind of web resource.

How do I automatically redirect the currently opened page to the login page when the session expires?

<meta http-equiv="refresh" content="${pageContext.session.maxInactiveInterval};url=sessionexpired.jsp">

Session validation filter which logs off the user when session is expired?

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;
    HttpSession session = request.getSession(false);

    if (session == null || session.getAttribute("user") == null) {
        response.sendRedirect("login"); // No logged-in user found, so redirect to login page.
    } else {
        chain.doFilter(req, res); // Logged-in user found, so just continue request.
    }
}

How to create and destroy session in Spring REST Webservice called from Mobile client?

My Web-service Method is and i want to access this method by mobile client :- 


   @RequestMapping(value = "/login", method = RequestMethod.POST, 
headers="Accept=application/json")
     public @ResponseBody List<LogInStatus> getLogIn(@RequestBody LogIn person ,  
HttpServletRequest request) {   
              // Call service here
             List<LogInStatus> lList = logInService.getUser(person);
//service method and then in DAO database method is there

             return lList;
     }
 

sol: 

You don't need to create session manually - this is done by servlet container.
You can obtain session from HttpServletRequest
HttpSession session = request.getSession(); or just add it as a method parameter, and Spring MVC will inject it for you:
public @ResponseBody List<LogInStatus> getLogIn(@RequestBody LogIn person , HttpServletRequest request, HttpSession httpSession) You then can save user details in session via setAttribute()/getAttribute().
 
 
 
 
 
 
 
 

Spring Annotations