protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { List<Product> products = productService.list(); // Obtain all products. request.setAttribute("products", products); // Store products in request scope. request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response); // Forward to JSP page to display them in a HTML table. } catch (SQLException e) { throw new ServletException("Retrieving products failed!", e); } }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); User user = userService.find(username, password); if (user != null) { request.getSession().setAttribute("user", user); // Login user. response.sendRedirect("home"); // Redirect to home page. } else { request.setAttribute("message", "Unknown username/password. Please retry."); // Store error message in request scope. request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response); // Forward to JSP page to redisplay login form with error. } }
public class LoginAction implements Action { public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception { String username = request.getParameter("username"); String password = request.getParameter("password"); User user = userDAO.find(username, password); if (user != null) { request.getSession().setAttribute("user", user); // Login user. return "home"; // Redirect to home page. } else { request.setAttribute("error", "Unknown username/password. Please retry."); // Store error message in request scope. return "login"; // Go back to redisplay login form with error. } } }
public ActionForward enrolInCycleCourseGroupInvalid(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { request.setAttribute("cycleEnrolmentBean", getCycleEnrolmentBeanFromViewState()); request.setAttribute("withRules", request.getParameter("withRules")); return mapping.findForward("chooseCycleCourseGroupToEnrol"); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String pro_code = request.getParameter("pro_code"); String name = request.getParameter("name"); String price = request.getParameter("price"); // do something usefull with the parameters here // expose the results to the jsp like so: request.setAttribute("pro_code", pro_code); request.setAttribute("name", name); request.setAttribute("price",price); RequestDispatcher rd = request.getRequestDispatcher("/studentSearch.jsp"); rd.forward(request, response); }
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException { if (request.getAttribute("test2") != null) { throw new ServletException("Wrong interceptor order"); } request.setAttribute("test1", "test1"); request.setAttribute("test1x", "test1x"); request.setAttribute("test1y", "test1y"); return true; }
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException { if (request.getAttribute("test1x") == null) { throw new ServletException("Wrong interceptor order"); } if (request.getParameter("abort") != null) { return false; } request.setAttribute("test2", "test2"); request.setAttribute("test2x", "test2x"); request.setAttribute("test2y", "test2y"); return true; }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setAttribute("myDate", new Date()); request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request, response); }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // fetch the username that was sent in the request String username = request.getParameter("username"); // TODO: verify if the username is taken in the database // based on the results set the value request.setAttribute("isUsernameTaken", "true"); RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/register.jsp"); dispatcher.forward(request, response); }
@Test public void includeOnAttribute() throws Exception { given(request.getAttribute(View.PATH_VARIABLES)).willReturn(null); given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn("somepath"); given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url)); view.setUrl(url); // Can now try multiple tests view.render(model, request, response); assertEquals(url, response.getIncludedUrl()); model.forEach((key, value) -> verify(request).setAttribute(key, value)); }
protected void validateParamsAndContinue(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) { if (entry.getValue() != null && entry.getValue().length >0) { for (String s : entry.getValue()) { if (hasText(s) && s.contains(NULL_STRING)) { response.setStatus(400); request.setAttribute("error_message_code", "request.invalid_parameter"); request.getRequestDispatcher("/error").forward(request,response); return; } } } } chain.doFilter(request, response); }
request.removeAttribute(attrName); else if (attrValue != request.getAttribute(attrName)) { request.setAttribute(attrName, attrValue);
final HttpServletResponse mockResponse = createMock(HttpServletResponse.class); expect(requestMock.getAttribute(A_KEY)).andReturn(A_VALUE); requestMock.setAttribute(REQUEST_DISPATCHER_REQUEST, true); requestMock.removeAttribute(REQUEST_DISPATCHER_REQUEST); dispatcher.forward(requestMock, mockResponse);
public class AccountServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Account> accounts = getAccountListFromSomewhere(); String url="..."; //relative url for display jsp page ServletContext sc = getServletContext(); RequestDispatcher rd = sc.getRequestDispatcher(url); request.setAttribute("accountList", accounts ); rd.forward(request, response); } }
request.setAttribute("typedEntitySection", typedEntitySection); wrapper.getRequestDispatcher(wrapper.getServletPath()).forward(wrapper, response); return true;
public ActionForward invalid(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) { final Student student = FenixFramework.getDomainObject(request.getParameter("studentOID")); request.setAttribute("student", student); request.setAttribute("schemaName", request.getParameter("schemaName")); request.setAttribute("manageStatuteBean", getRenderedObject()); return mapping.findForward("manageStatutes"); }
private void sendToAuthorizePage(HttpServletRequest request, HttpServletResponse response, OAuthAccessor accessor) throws IOException, ServletException { String callback = request.getParameter("oauth_callback"); if (callback == null || callback.length() <= 0) { callback = "none"; } String consumer_description = (String)accessor.consumer.getProperty("description"); request.setAttribute("oauthParam_CONSUMER_DESCRIPTION", consumer_description); request.setAttribute("oauthParam_CALLBACK_URL", callback); request.setAttribute("oauthParam_REQUEST_TOKEN", accessor.requestToken); request.getRequestDispatcher("/WEB-INF/aps/jsp/oauth/authorize.jsp").forward(request, response); }
@WebServlet("/product") public class ProductServlet extends HttpServlet { @EJB private ProductService productService; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Product product = productService.find(request.getParameter("id")); request.setAttribute("product", product); // Will be available as ${product} in JSP request.getRequestDispatcher("/WEB-INF/product.jsp").forward(request, response); } }
protected String extractToken(HttpServletRequest request) { // first check the header... String token = extractHeaderToken(request); // bearer type allows a request parameter as well if (token == null) { logger.debug("Token not found in headers. Trying request parameters."); token = request.getParameter(OAuth2AccessToken.ACCESS_TOKEN); if (token == null) { logger.debug("Token not found in request parameters. Not an OAuth2 request."); } else { request.setAttribute(OAuth2AuthenticationDetails.ACCESS_TOKEN_TYPE, OAuth2AccessToken.BEARER_TYPE); } } return token; }
@Override public String resolveThemeName(HttpServletRequest request) { // Check request for preparsed or preset theme. String themeName = (String) request.getAttribute(THEME_REQUEST_ATTRIBUTE_NAME); if (themeName != null) { return themeName; } // Retrieve cookie value from request. String cookieName = getCookieName(); if (cookieName != null) { Cookie cookie = WebUtils.getCookie(request, cookieName); if (cookie != null) { String value = cookie.getValue(); if (StringUtils.hasText(value)) { themeName = value; } } } // Fall back to default theme. if (themeName == null) { themeName = getDefaultThemeName(); } request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, themeName); return themeName; }