Issue
I am trying to create a servlet in Vaadin 14 running on Spring boot. I have read the Application lifecycle page on Vaadin Application lifecycle and have tried creating a servlet with the help of that but have not succeeded. My webpage doesn't seem to recognize the URL. I have tried both VaadinServlet and SpringServlet, here is my code:
@WebServlet(value = "name", urlPatterns = {"customservlet"})
public class CustomServlet extends SpringServlet implements SessionInitListener, SessionDestroyListener {
public CustomServlet(ApplicationContext context, boolean forwardingEnforced) {
super(context, forwardingEnforced);
}
@Override
protected void servletInitialized() throws ServletException {
super.servletInitialized();
getService().addSessionInitListener(this);
getService().addSessionDestroyListener(this);
}
@Override
public void sessionInit(SessionInitEvent event)
throws ServiceException {
// Do session start stuff here
}
@Override
public void sessionDestroy(SessionDestroyEvent event) {
// Do session end stuff here
}
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
getService().addSessionInitListener((SessionInitListener) event -> event.getSession().addRequestHandler(globalRequestHandler()));
super.service(request, response);
}
public RequestHandler globalRequestHandler() {
return (session, vaadinRequest, response) -> {
if ("/customservlet".equals(vaadinRequest.getPathInfo())) {
response.setContentType("text/html;charset=UTF-8");
response.getWriter().append("some content");
response.getWriter().flush();
return true;
} else {
return false;
}
};
}
}
Edit: It worked with Leifs with solution. Here are the results if anyone wants to know:
@Route("yourroute")
@SpringComponent
public class MyInitListener implements Serializable, VaadinServiceInitListener {
@Override
public void serviceInit(ServiceInitEvent serviceEvent) {
serviceEvent.getSource().addSessionInitListener(initEvent -> {
initEvent.getSession().addRequestHandler(globalRequestHandler());
});
serviceEvent.getSource(). addSessionDestroyListener(initEvent -> {
initEvent.getSession().close();
});
}
public RequestHandler globalRequestHandler() {
return (session, vaadinRequest, response) -> {
if ("/yourroute".equals(vaadinRequest.getPathInfo())) {
response.setContentType("text/plain");
PrintWriter pw = response.getWriter();
pw.write("Some content");
pw.close();
return true;
} else {
return false;
}
};
}
}
Solution
I will try to give an answer to the thing you seem to want to achieve (adding session listeners) rather than the way you try to do that (creating a custom servlet).
The easiest way to add those listeners is with a VaadinServiceInitListener
. When using Spring, you can do that just by defining a bean that implements that interface.
@Component
public class MyInitListener implements VaadinServiceInitListener {
@Override
public void serviceInit(ServiceInitEvent serviceEvent) {
serviceEvent.getSource().addSessionInitListener(initEvent -> {
// Do session start stuff here
});
serviceEvent.getSource(). addSessionDestroyListener(initEvent -> {
// Do session end stuff here
});
}
}
Answered By - Leif Åstrand
Answer Checked By - Robin (JavaFixing Admin)