john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

servlet with javascript validation

//2012-03-19 johnpfeiffer  Right click Web Content -> New File -> name it javascript.js , add the pure javascript code

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Test extends HttpServlet
{
    private static final long serialVersionUID = 1L;
    private static String contextPath;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType( "text/html" );     // MIME type
        PrintWriter servletresponse = null;
        try{    servletresponse = response.getWriter();
        }catch( Exception e )
        {       System.err.println( "Unable to create a PrintWriter" );
                e.printStackTrace();
                System.exit( 1 );
        }
        contextPath = getContextPath( request );
        outputXHTMLHeader( servletresponse , "Javascript-Testing" );



        String input = request.getParameter( "userUsername" );
        if( input != null && !input.isEmpty() )
        {
            servletresponse.println( input );
        }else
        {
            displayForm( servletresponse , contextPath );
            javascriptFormFocus( servletresponse , "userUsername" );
        }
    }


    private static void javascriptFormFocus( PrintWriter servletresponse , String inputname )
    {
        servletresponse.println( "<script type='text/javascript' language='JavaScript'> " );
        servletresponse.println( "setFocus( '" + inputname + "' ); ");
        servletresponse.println( "</script>" );
    }

    private void displayForm( PrintWriter servletresponse , String contextPath )
    {
        String servletName =  getServletName();
        String submitTarget = contextPath + "/" + servletName;
        servletresponse.println( "<form id='getpassword' action='" + submitTarget + "' method='post' onsubmit='return validateForm()' >" );
        servletresponse.println( "User <input type='text' name='userUsername'/> <br />" );
        servletresponse.println( "Pass <input type='text' name='userPassword'/> <br />" );
        servletresponse.println( "<span><input type='submit' value='login'/></span>" );
        servletresponse.println( "</form>" );
    }

    private static void outputXHTMLHeader( PrintWriter servletresponse , String title )
    {
        servletresponse.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
        servletresponse.println( "<html xmlns=\"http://www.w3.org/1999/xhtml\">" );
        servletresponse.println( "<head><title>" + title + "</title>" );
        servletresponse.println( "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />" );
        servletresponse.println( "<script type='text/javascript' src='" + contextPath + "/javascript.js'></script>" );
        servletresponse.println( "</head>" );
    }

    private String getContextPath( HttpServletRequest request )
    {
        return request.getContextPath();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doGet( request, response);
    }
} //end class

  • « javascript.js
  • CommandLineParameterRequirementsTest »

Published

Mar 19, 2012

Category

java-servlet

~216 words

Tags

  • java-servlet 61
  • javascript 43
  • servlet 17
  • validation 8
  • with 29