Generate an HTML Response in a Java Servlet

You normally forward the request to a JSP for display. JSP is a view technology which provides a template to write plain vanilla HTML/CSS/JS in and provides ability to interact with backend Java code/variables with help of taglibs and EL. You can control the page flow with taglibs like JSTL. You can set any backend … Read more

Pass variables from servlet to jsp

It will fail to work when: You are redirecting the response to a new request by response.sendRedirect(“page.jsp”). The newly created request object will of course not contain the attributes anymore and they will not be accessible in the redirected JSP. You need to forward rather than redirect. E.g. request.setAttribute(“name”, “value”); request.getRequestDispatcher(“page.jsp”).forward(request, response); You are accessing … Read more

How to force browser to download file?

You are setting the response headers after writing the contents of the file to the output stream. This is quite late in the response lifecycle to be setting headers. The correct sequence of operations should be to set the headers first, and then write the contents of the file to the servlet’s outputstream. Therefore, your … Read more

How to implement pagination in Spring MVC 3 [closed]

Have a look at PagedListHolder and other classes from org.springframework.beans.support. See the JPetstore in the samples for some examples, e.g. in SearchProductsController.java: public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { String keyword = request.getParameter(“keyword”); if (keyword != null) { if (!StringUtils.hasLength(keyword)) { return new ModelAndView(“Error”, “message”, “Please enter a keyword to search for, then … Read more

How to redirect in a servlet filter?

In Filter the response is of ServletResponse rather than HttpServletResponse. Hence do the cast to HttpServletResponse. HttpServletResponse httpResponse = (HttpServletResponse) response; httpResponse.sendRedirect(“/login.jsp”); If using a context path: httpResponse.sendRedirect(req.getContextPath() + “/login.jsp”); Also don’t forget to call return; at the end.

Where’s javax.servlet?

javax.servlet is a package that’s part of Java EE (Java Enterprise Edition). You’ve got the JDK for Java SE (Java Standard Edition). You could use the Java EE SDK for example. Alternatively simple servlet containers such as Apache Tomcat also come with this API (look for servlet-api.jar).

Servlet vs Filter

Use a Filter when you want to filter and/or modify requests based on specific conditions. Use a Servlet when you want to control, preprocess and/or postprocess requests. The Java EE tutorial mentions the following about filters: A filter is an object that can transform the header and content (or both) of a request or response. … Read more

Is there a static way to get the current HttpServletRequest in Spring

If you are using spring you can do the following: public static HttpServletRequest getCurrentHttpRequest(){ RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); if (requestAttributes instanceof ServletRequestAttributes) { HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest(); return request; } logger.debug(“Not called in the context of an HTTP request”); return null; }

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