Interview Questions

Java Script Auto Submit form

The following JSP file automatically submits the form values when the page is loaded in browser.

Auto_Submit_Form.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>

<html>
<head>
<title>Auto Login Form</title>
</head>

<script type="text/javascript">
window.onload = formSubmit;

function formSubmit() {

      document.forms[0].submit();
}
</script>

<body>
    <form method="post" action="<c:url value="postDataReceiverServlet"/>">
        <input type="hidden" name="username" id="username" value="admin" />
        <input type="hidden" name="password" id="password" value="admin"/>
    </form>
</body>

</html>

The above file contains form values such as username and password. These values will be submitted to the action,specified in the form tag. So , when the JSP is accessed, user will be viewing the page as result of the submitted form.

This file should be placed out side WEB-INF folder,so that it can be accessed via the following URL.

http://localhost:8080/<applicationName>/Auto_Submit_Form.jsp