Should one call .close() on HttpServletResponse.getOutputStream()/.getWriter()?

Normally you should not close the stream. The servlet container will automatically close the stream after the servlet is finished running as part of the servlet request life-cycle. For instance, if you closed the stream it would not be available if you implemented a Filter. Having said all that, if you do close it nothing … Read more

what is the difference between a portlet and a servlet?

Enhanced from Source: Servlets Vs Portlets Similarities Servlets and Portlets are web based components which use Java for their implementation. Portlets are managed by a portlet container just like servlet is managed by servlet container. Both static and dynamic content can be generated by Portlets and Servlets. The life cycle of portlets and servlets is … Read more

How to run a background task in a servlet based web application?

Your problem is that you misunderstand the purpose of the servlet. It’s intented to act on HTTP requests, nothing more. You want just a background task which runs once on daily basis. EJB available? Use @Schedule If your environment happen to support EJB (i.e. a real Java EE server such as WildFly, JBoss, TomEE, Payara, … Read more

Prevent user from seeing previously visited secured page after logout

You can and should not disable the browser back button or history. That’s bad for user experience. There are JavaScript hacks, but they are not reliable and will also not work when the client has JS disabled. Your concrete problem is that the requested page is been loaded from the browser cache instead of straight … Read more

Http Servlet request lose params from POST body after read it once

As an aside, an alternative way to solve this problem is to not use the filter chain and instead build your own interceptor component, perhaps using aspects, which can operate on the parsed request body. It will also likely be more efficient as you are only converting the request InputStream into your own model object … Read more

Get JSF managed bean by name in any Servlet related class

In a servlet based artifact, such as @WebServlet, @WebFilter and @WebListener, you can grab a “plain vanilla” JSF @ManagedBean @RequestScoped by: Bean bean = (Bean) request.getAttribute(“beanName”); and @ManagedBean @SessionScoped by: Bean bean = (Bean) request.getSession().getAttribute(“beanName”); and @ManagedBean @ApplicationScoped by: Bean bean = (Bean) getServletContext().getAttribute(“beanName”); Note that this prerequires that the bean is already autocreated by … Read more

Why does Spring MVC respond with a 404 and report “No mapping found for HTTP request with URI […] in DispatcherServlet”?

Your standard Spring MVC application will serve all requests through a DispatcherServlet that you’ve registered with your Servlet container. The DispatcherServlet looks at its ApplicationContext and, if available, the ApplicationContext registered with a ContextLoaderListener for special beans it needs to setup its request serving logic. These beans are described in the documentation. Arguably the most … Read more

doGet and doPost in Servlets

Introduction You should use doGet() when you want to intercept on HTTP GET requests. You should use doPost() when you want to intercept on HTTP POST requests. That’s all. Do not port the one to the other or vice versa (such as in Netbeans’ unfortunate auto-generated processRequest() method). This makes no utter sense. GET Usually, … Read more

java.lang.IllegalStateException: Cannot (forward | sendRedirect | create session) after response has been committed

A common misunderstanding among starters is that they think that the call of a forward(), sendRedirect(), or sendError() would magically exit and “jump” out of the method block, hereby ignoring the remnant of the code. For example: protected void doXxx() { if (someCondition) { sendRedirect(); } forward(); // This is STILL invoked when someCondition is … Read more

How to use Session attributes in Spring-mvc

If you want to delete object after each response you don’t need session, If you want keep object during user session , There are some ways: directly add one attribute to session: @RequestMapping(method = RequestMethod.GET) public String testMestod(HttpServletRequest request){ ShoppingCart cart = (ShoppingCart)request.getSession().setAttribute(“cart”,value); return “testJsp”; } and you can get it from controller like this … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)