Monday, 11 August 2014

setCharacterEncoding (String env): Set the Character Encoding

public void setCharacterEncoding(String env) throws UnsupportedEncodingException
Method sets the character encoding scheme. This method must be called before reading request parameters, otherwise it has no affect.

<!DOCTYPE html>
<html>
    <head>
        <title>User Registration</title>
    </head>
    <body>
        <noscript>Java Script functionality disabled, Please enable it</noscript>
        <form action="/servlet/Register" method="post">
            <table id="registrationTable">
                <caption>Please fill the details to Register</caption>
                <tr>
                    <td>Mail Id:<span class="required1"><sup>*</sup></span></td>
                    <td><input type="text" name="mailID" id="mailID" required="required" /></td>
                </tr>
                <tr>
                    <td>Password:<span class="required1"><sup>*</sup></span></td>
                    <td><input type="password" name="passwd" id="passwd" required="required" /></td>
                </tr>
                <tr>
                    <td>Confirm Password<span class="required1"><sup>*</sup></span></td>
                    <td><input type="password" name="confPasswd" id="confPasswd" required="required" /></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Register" id="register" /></td>
                    <td><input type="reset" value="Clear Data" /></td>             
                </tr>
            </table>
        </form>
    </body>
</html>

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

@WebFilter(filterName = "FilterEncodind", urlPatterns = {"/Register"})
public class FilterEncoding implements Filter {
    @Override
   public void init(FilterConfig filterConfig) throws ServletException{
       
   }
   
   @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException{
        req.setCharacterEncoding("ISO-8859-8");
        chain.doFilter(req, res);
    }
    
   @Override
    public void destroy(){
        
    }
}

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {"/Register"})
public class Register extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res)throws IOException{
        String encoding = req.getCharacterEncoding();
        try(PrintWriter out = res.getWriter()){
            out.println("<html><head><title>Get Encoding format</title><body><h1>");
            out.println("Encoding used is " + encoding);
            out.println("</h1></body></html>");
        }
        
    }
}

Output
Fill and submit the registration form, then you can see the output like below.





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment