简体   繁体   中英

How to make a JSP page NOT to forward to servlet without user's input?

Consider the following JSP :

<!-- Bank Application in JAVA -->
<!-- Updates : the DB now is using Hibernate for the SQL queries -->
<!-- 2014 version updates -->
<!-- This is a comment in JSP file   -->

<%@ page language="java" 
    contentType="text/html; charset=windows-1256"
    pageEncoding="windows-1256"
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head><title>Bank application</title>
<link rel="stylesheet"
      href="./css/styles.css"
      type="text/css"/>
</head>

<body>
<table class="title">
  <tr><th>Web Bank application</th></tr>
</table>

<br/>
<fieldset>
  <legend>Login Page - please enter your Username and Password</legend>
  <form action="loginPage"  > 

  <!-- note we use here a paragraph & font size -->
  <!-- Notice we use a Required field !!! -->

    <p style="font-size:15px">  <span style="color:red;font-weight:bold;">*</span> Username: <input type="text" name="username"><br> </p>
    <p style="font-size:15px"><span style="color:red;font-weight:bold;">*</span>  Password : <input type="password" name="password"><br> </p>
    <input type="submit" value="Login">

  </form>
</fieldset>

<br/>
<br/>
<br/>
<br/>
<br/><br/><br/><br/><br/><br/>
</body></html>

Visual look :

在此处输入图片说明

How can I make the JSP to stay put and not forward to the Login servlet (to check the correctness of the input) , until the user had entered both Username and Password ?

Use javascript in the onclick function of the login button to cancel the form submission, verify that the username and password fields are not empty, then submit the form

UPDATE:

In my code, I often use a button type instead of a <input type=submit> button. Then I do basic validation through javascript. Something like:

function checkFields(){
  var user = document.getElementById('username').value; 
  var pass = document.getElementById('password').value;

  if (user == "" || pass == ""){
    alert("Username and Password required");
  }
  else{
    document.getElementById("myForm").submit();
  }
}

then my form would be something like:

<form id="myForm">
  <input type="text" id="username"></input>
  <input type="password" id="password"></input>
  <button type="button" id="submitButton" onclick="checkFields()">Submit</button>
</form>

I have not tested any of this, this is just off the top of my head. But that is about what I do.

EDIT: You need to get the value of the username and password fields, not just the fields themselves, sorry for the typo

There's a couple of ways to do this, the simplest is probably to have the servlet check for the username and password parameters in the request and redirect back the the original JSP if they're missing (you should do this anyway, it's not just your page that can hit your servlet).

If you are adament that you don't what the request submitted until the username and password has been entered then you'll have to hook-up the onsubmit event on the form like this:

<form id="login" action="loginPage" onsubmit="checkUsernameAndPassword">    

And then write the checkUsernameAndPassword method in JavaScript like this:

function checkUsernameAndPassword()
{
  var username = document.forms["login"]["username"].value;
  var password = document.forms["login"]["password"].value;

  return !(username == null || username == '' || password == null || password == '')
}     

This method will return false if either the username or password have not been set cancelling the submission.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM