I'm getting a ViewExpiredException when I'm at my login page, it is not supposed to be. So I read a post (recommended by BalusC) on Filters. It might be that the page is loading from browser's cache and not from server. So I've implemented this code
#WebFilter(servletNames={"Faces Servlet"})
public class NoCacheFilter implements Filter {
#Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpReq = (HttpServletRequest) request;
HttpServletResponse httpRes = (HttpServletResponse) response;
if (!httpReq.getRequestURI().startsWith(httpReq.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) { // Skip JSF resources (CSS/JS/Images/etc)
httpRes.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
httpRes.setHeader("Pragma", "no-cache"); // HTTP 1.0.
httpRes.setDateHeader("Expires", 0); // Proxies.
}
chain.doFilter(request, response);
}
// ...
#Override
public void init(FilterConfig filterConfig) throws ServletException {
throw new UnsupportedOperationException("Not supported yet.");
}
#Override
public void destroy() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
but when I try to test it I get the following error:
The module has not been deployed.
at org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment.deploy(Deployment.java:210)
at org.netbeans.modules.j2ee.ant.Deploy.execute(Deploy.java:106)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.GeneratedMethodAccessor193.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:284)
at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:539)
at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:153)
You're throwing an exception during filter's initialization (and destroy). So filter's initialization will completely fail and cause the webapp's startup to be blocked. Remove those lines. You shouldn't purposefully throw an exception in there.
#Override
public void init(FilterConfig filterConfig) throws ServletException {
// NOOP.
}
#Override
public void destroy() {
// NOOP.
}
Read the filter's javadoc to learn about what those methods are for.
See also:
Our Servlet-Filters tag wiki page
Related
Is there a way to log the requests coming for static files (like index.html or js files) in JHipster.
Add a filter:
public class RequestLoggingFilter implements Filter {
private static final Logger log = Logger.getLogger(AuditFilter.class);
#Override
public void init(FilterConfig filterConfig) throws ServletException {
}
#Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
String requestUrl = httpRequest.getRequestURI();
String requestMethod = httpRequest.getMethod();
log.info("Received " + requestMethod + " request + " for " + requestUrl);
chain.doFilter(request, response);
}
#Override
public void destroy() {
}
}
I have a Spring + JSP application (MyFaces + PrimeFaces) and I've added a filter to it in order to add the same headers to all responses.
The application is working fine but if I restart the server and someone has an open page (with a now invalid or non-existing session) I start getting NullPointerException all the time, but it's happening outside of my code. How can I fix this?
The problem seems to be in org.primefaces.util.ResourceUtils:66 (getComponentResources(FacesContext context)) where it tries this:
List<UIComponent> resources = context.getViewRoot().getComponentResources(context, "head");
But context.getViewRoot() returns null so it fails.
If the client reloads the page, he is redirected to the login page correctly, a new session is created, and errors stop.
NoCacheFilter class:
#WebFilter(servletNames = { "Faces Servlet" })
public class NoCacheFilter implements Filter {
#Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
// Skip JSF resources (CSS/JS/Images/etc)
if (!req.getRequestURI().startsWith(req.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) {
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
res.setHeader("Pragma", "no-cache"); // HTTP 1.0.
res.setDateHeader("Expires", 0); // Proxies.
}
chain.doFilter(req, res);
}
#Override
public void init(FilterConfig filterConfig) throws ServletException {
}
#Override
public void destroy() {
}
}
Exception Trace:
java.lang.NullPointerException
at org.primefaces.util.ResourceUtils.getComponentResources(ResourceUtils.java:66)
at org.primefaces.context.PrimePartialResponseWriter.startMetadataIfNecessary(PrimePartialResponseWriter.java:280)
at org.primefaces.context.PrimePartialResponseWriter.startError(PrimePartialResponseWriter.java:107)
at org.apache.myfaces.shared.context.AjaxExceptionHandlerImpl.renderAjaxError(AjaxExceptionHandlerImpl.java:274)
at org.apache.myfaces.shared.context.AjaxExceptionHandlerImpl.handle(AjaxExceptionHandlerImpl.java:238)
at javax.faces.context.ExceptionHandlerWrapper.handle(ExceptionHandlerWrapper.java:61)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:217)
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:143)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at eu.calabacin.controller.NoCacheFilter.doFilter(NoCacheFilter.java:30)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at eu.calabacin.controller.GlobalFilter.doFilter(GlobalFilter.java:31)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Section of web.xml where GlobalFilter is defined:
<filter>
<filter-name>GlobalFilter</filter-name>
<filter-class>eu.calabacin.controller.GlobalFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>GlobalFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
GlobalFilter class:
public class GlobalFilter implements Filter {
private final static String LOGIN_PAGE = "/" + GlobalService.LOGIN_PAGE + ".xhtml";
#Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpSession session = ((HttpServletRequest) request).getSession(false);// don't create a session if there isn't one
if (session != null && !session.isNew()) {
chain.doFilter(request, response);
} else {
redirectToLogin(request, response, chain);
}
}
private void redirectToLogin(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
String pathInfo = httpRequest.getServletPath();
if (pathInfo == null || !pathInfo.equalsIgnoreCase(LOGIN_PAGE)) {
String newUrl = httpRequest.getContextPath() + LOGIN_PAGE;
httpResponse.sendRedirect(newUrl);
} else {
chain.doFilter(request, response);
}
}
#Override
public void init(FilterConfig filterConfig) throws ServletException {
}
#Override
public void destroy() {
}
}
Software versions:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring.version>4.2.0.RELEASE</spring.version>
<myfaces.version>2.2.8</myfaces.version>
<primefaces.version>6.0</primefaces.version>
<hibernate.version>5.0.0.Final</hibernate.version>
</properties>
I guess I could 'fix' the problem by changing the ResourceUtils class checking if context.getViewRoot() returns null before trying to use it, but I'm sure I must be doing something wrong somewhere that causes this to fail.
Anyone knows what I'm doing wrong or how I could fix this?
Thank you.
Today I ran into a similar problem (having a NullPointerException exactly at the same PrimeFaces line).
I solved the problem upgrading from Primefaces 6.0 to Primefaces 6.1 and correcting a wrong JPA query that was causing a previous exception (I'm not completely sure which action solved the problem, but I guess it was the upgrade).
I've tried to make autorisation form by using jsf 1.2 but I had some exceptions with redirect.
Bean code
public class UserAutorisationBean implements Serializable {
private Boolean isLogin = false;
private String login = "";
private String password = "";
public void autoriseLoginAndPass() throws IOException {
if (this.login == "user" && this.password == "password") {
this.isLogin = true;
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response =
(HttpServletResponse) context.getExternalContext().getResponse();
response.sendRedirect("web/HelloWorld.jsp");
} else {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response =
(HttpServletResponse) context.getExternalContext().getResponse();
response.sendRedirect("web/Error.jsp");
}
}
Filter code
public class Filter implements javax.servlet.Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
HttpSession session = null;
session = httpRequest.getSession(false);
if (session != null) {
UserAutorisationBean userAutorisationBean = null;
userAutorisationBean =
(UserAutorisationBean) session.getAttribute("userautorisationbean");
if (userAutorisationBean != null) {
if (!userAutorisationBean.isLoginIn()) {
httpResponse.sendRedirect(httpRequest.getContextPath() + "/Error.jsp");
}
}
chain.doFilter(request, response);
}
if (session == null) {
httpResponse.sendRedirect(httpRequest.getContextPath() + "/Error.jsp");
}
}
public void destroy() {
}
}
Stack:
message Cannot forward after response has been committed
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Cannot forward after response has been committed
javax.faces.webapp.FacesServlet.service(FacesServlet.java:256)
root cause
java.lang.IllegalStateException: Cannot forward after response has been committed
com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:414)
com.sun.faces.application.ViewHandlerImpl.executePageToBuildView(ViewHandlerImpl.java:455)
com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:139)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:108)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:266)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:159)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:245)
I've tried to find some advices by using search but it didn't help me to solve the problem.
I have implemented a Servlet Filter to do my system's authorization and where needed I redirect the user using this:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
...
if(!isLoggedIn(currentUser)){
if(isSecurePage(requestedPage)){
redirectUser(resp,USER_LOGIN_PAGE);
}
else{ // Carry on
chain.doFilter(request,response);
}
}
else{
if(!isSecurePage(requestedPage)){
redirectUser(resp,USER_WELCOME_PAGE);
}
else if(!canAccess(currentUser,requestedPage)){
redirectUser(resp,req.getContextPath() + USER_DENIED_PAGE);
}
else{ // Carry on
chain.doFilter(request,response);
}
}
}
private void redirectUser(HttpServletResponse response, String page) throws IOException {
response.sendRedirect(page);
}
The redirect happens correctly. My issue is that although ACCESS_DENIED_PAGE is displayed, the JSF action method behind the original request is still executed (a FacesMessage that it creates is displayed in the ACCESS_DENIED_PAGE, for example).
How to prevent this?
I am using the jsp-servlet in my application. and deployed the war on jboss 7.0.2 server. i have servlet have code related to database and that is being called many time in sec (say 500 times). but it is falling over for such many threads, jboss 7.0.2 will not able to handle this threads. server (jboss7.0.2) throws an exception.
java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Unknown Source)
Here is my servlet
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
public void processRequest(HttpServletRequest request,
HttpServletResponse response) {
Logger log=LoggerFactory.getLogger(Test.class);
/* here is my code to insert the data in database. */
TestClass testobj = new TestClass();
testobj.setparam("");
smsmanager1.add(sms);
smsmanager1 = null;
sms = null;
}
}
code for add method
public void add(T obj) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session=sessionFactory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.save(obj);
transaction.commit();
session.flush();
} catch (HibernateException e) {
if(transaction!=null){
transaction.rollback();}
e.printStackTrace();
} finally {
if(session!=null){
session.close();}
session = null;
transaction = null;
}
i have tested for the blank servlet that has the only one console printing statement. it works fine but it not work for above servlet.
am i on the right track here?
how the server will handle the such servlet for above 500-800 threads?