简体   繁体   中英

Redirect to same Html page with error

I am trying to redirect to the same HTML page with an error message saying all fields are required if any of the fields are left empty. I am using Servlets, MYSQL, and HTML to build this project, I am able to store data in MYSQL but stuck at this point, Please help me.

You can use RequestDispatcher to dispatch your HTML resource. But you need to handle displaying error messages in client side (using JS). But it's better to use JSP, so you can achieve it easily

Well, since you didn't show any code, the whole idea is:

  1. Using client side: You can use JavaScript in the client to confirm if an HTML field is empty or not

validate.js

var field = document.forms["form_name"]["user_input"].value;

if (field==null) {
    alert("all fields are required");
    return false;
}
  1. Use server side I'm not a servlet guy but in PHP you can do this:

validate.php

if(isset($_POST["user_input])){
   $field = $_POST["user_input"];
   if(empty($field){
      echo "error, all fields are required"
   }

Hope this helps

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