http://feeds.feedburner.com/SpendYourTimeHere-Once

Current Affiares

Showing posts with label VALIDATION. Show all posts
Showing posts with label VALIDATION. Show all posts

Monday, August 13, 2012

How to check the requested page has a valid user with filter class IN JAVA, JSF?

How to check the requested page has a valid user with filter class IN JAVA, JSF?

 // login validation method && set attribute
// UserBean.java

public String reuestedUrl() {
               boolean check = loginBo.validateLogin(getUsername(), getPassword(), testlabConstants.USER_TYPE_ADMIN);
        if (check) {
            ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
            HttpSession session = (HttpSession) ectx.getSession(false);
            session.setAttribute("VALID_USER", "YES");

            return "reuestedUrl?faces-redirect=true";
        } else {

            String msg = "Invalid Username/Password.";
            setLoginfailuremsg(msg);

            return "index?faces-redirect=true";

        }
    }


// filter class for every page & this will work for every page by include filter in web.xml file


RestrictPageFilter.java

package xyz.utils;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class RestrictPageFilter implements Filter{
    FilterConfig fc;
       @Override
    public void init(FilterConfig filterConfig) throws ServletException{
        fc=filterConfig;
    }
    @Override
    public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException,ServletException{
        HttpServletRequest req=(HttpServletRequest) request;
        HttpServletResponse resp=(HttpServletResponse) response;
        HttpSession session=req.getSession();
        String pageRequested=req.getRequestURL().toString();
           //  System.out.println("pageRequested::"+pageRequested);
      //  System.out.println("Valid user::"+session.getAttribute("VALID_USER"));
        if(session.getAttribute("VALID_USER")==null){
            RequestDispatcher rd=req.getRequestDispatcher("/faces/index.xhtml?faces-redirect=true");
            rd.forward(request, response);
            //resp.sendRedirect("/faces/index.xhtml?faces-redirect=true");
        }else{
            chain.doFilter(request, response);
        }
       
    }
    @Override
    public void destroy(){
       
    }
}





// web.xml file



<filter>
        <filter-name>restrict</filter-name>
        <filter-class>testlab.utils.RestrictPageFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>restrict</filter-name>
        <url-pattern>*.xhtml</url-pattern>
    </filter-mapping>

How to make the log off button working in java ?

How to make the log off button working in java ?


import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

public String logoff() {
        ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) ectx.getSession(false);
        session.invalidate();

        return "index?faces-redirect=true";
    }

// using the method with command button of primefaces
<p:commandButton type="submit" icon="ui-icon-power" value="Log-off" immediate="true" 
action="#{user.logoff()}" ajax="false"/>

How to validate email & uploaded file in java?

How to validate email in java?

import javax.faces.context.FacesContext;
import javax.faces.component.UIComponent;
import javax.faces.validator.ValidatorException; 

public void validateEmail (FacesContext context, UIComponent toValidate, Object value) throws ValidatorException {
        String eMail = (String) value;
        if (!"".equals(eMail)) {
            if (eMail.indexOf("@") < 0) {
                FacesMessage message = new FacesMessage("Invalid email address!");
                throw new ValidatorException(message);
            }
        }
    }


//Using the above method with primefaces input text field
<p:inputText id="memail" value="#{addmem.memail}"  validator="#{user.validateEmail}"  required="false" label="memail" size="35"  />

How to validate uploaded file or check file existence in disk in java?

import javax.faces.validator.ValidatorException;
import javax.faces.context.FacesContext;
import javax.faces.component.UIComponent; import javax.faces.context.ExternalContext;
import org.primefaces.model.UploadedFile;

public void validateFile(FacesContext context, UIComponent toValidate, Object value) throws ValidatorException {
 ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();

        UploadedFile uFile = (UploadedFile) value;

       String uploadfilename = uFile.getFileName().substring(uFile.getFileName().lastIndexOf("\\") + 1, uFile.getFileName().length());
       
       String serverfileaddress = servletContext.getRealPath("") + File.separator + testlabConstants.FOLDER_NAME_OF_UPLOADED_FILE + File.separator + uploadfilename;

         File f = new File(serverfileaddress);

            if (f.exists()) {
            FacesMessage message = new FacesMessage("File already exist !");
            throw new ValidatorException(message);
        }

    }


 // Using the above method with primefaces file upload component

  <p:fileUpload label="Upload File" id="uploadfile" validator="#{tdbean.validateFile}" required="true" value="#{tdbean.file}"   mode="simple"/> 

My Blog List

Popular Posts

All Rights Reserved To SYTHONCE. Ethereal theme. Powered by Blogger.