/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println( "MyProtectedServlet.processRequest " + request.getRequestURI() + " " + request.getQueryString()); String myUrl = request.getRequestURI(); if (myUrl.indexOf("login") >= 0) { login(request, response); return; } else if (myUrl.indexOf("redirect") >= 0) { redirect(request, response); return; } if (request.getRemoteUser() == null) { String callUrl = request.getRequestURI(); String query = request.getQueryString(); if (query != null) { callUrl = callUrl + "?" + query; } String nextEncUrl = java.net.URLEncoder.encode(callUrl); String redirectUrl = request.getContextPath() + "/application/redirect?nextencurl=" + nextEncUrl; response.sendRedirect(redirectUrl); } else { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet MyProtectedServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet MyProtectedServlet at " + request.getContextPath() + "</h1>"); out.println("</body>"); out.println("</html>"); out.close(); } }
@Override public void service(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html"); String path = request.getRequestURI(); // use routes if present if (this.routes != null) { String route = this.routes.getProperty(path); if (route != null) { path = route; // then we also need to replace the HttpServletRequest.getRequestURI method // request = getRequestWrapper(request, path); } } if (path.endsWith("/")) path += "index"; String source = null; // check for changes first if (this.debug && contextCache.checkForChanges()) { if (this.pageInfoCache != null) this.pageInfoCache.clear(); } ScriptContext context = contextCache.getContext(); try { serve(request, response, context, path, null); } catch (Exception e) { int status = getStatus(e); response.setStatus(status); response.setContentType("text/plain"); System.out.println(e.toString()); if (errorPath != null && status >= 500) { try { serve(request, response, context, errorPath, e); } catch (Exception f) { e.printStackTrace(response.getWriter()); response.getWriter().write("ERROR IN ERROR HANDLER PAGE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); f.printStackTrace(response.getWriter()); } } else if (debug && status == 500) { e.printStackTrace(response.getWriter()); } else if (e instanceof JavaScriptException) { JavaScriptException je = (JavaScriptException) e; response.getWriter().write(je.getValue().toString()); } else { response.getWriter().write(e.getMessage()); } } finally { contextCache.returnContext(context); } }
private String extractServletPath(HttpServletRequest pReq) { return pReq.getRequestURI().substring(0, pReq.getContextPath().length()); }