0

I have an Jsp page that has the ID text field which takes the input from the user and that input is read in a servlet. I am also validating that input in servlet. If user enters an wrong input I need to give a text message like wrong input below that ID field only in the Jsp.

Here is the servlet code:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    // TODO Auto-generated method stub
    services2   reporteeservice = new services2();
    services3      jiraservice  = new services3();
    service4            empid   = new service4();
    String id                   = request.getParameter("ManagerId");
    int Number                  = 1;

    PrintWriter out1=response.getWriter();
    String n = ".*[0-9].*";
    String a = ".*[a-z].*";
    String c = ".*[A-Z].*";

    if( id.matches(n) && id.matches(a) || id.matches(c)) 
    {
      out1.println("valid input");
        try {
        //s.getList(id);
        ....
    }
    else
    { 
        out1.println("not a valid input");
    }

But if I give else part as above, it will redirect me to next page and prints not a valid upon giving the wrong input. But, I want that to be displayed in the same page under the ID field in the Jsp.

My JSP page:

<form name = "reportees" method = "GET" action="reportsto">
    <center>
    <h1><font size="20"> Enter the ID to get the Reportees</font></h1>
        <label><font size="+2">ID</font></label>
        <input name="ManagerId" ype="text" style="font-size:16pt"/>
        <input type="submit" value="submit"/>
        <button type="reset" value="Reset">cancel</button>
    </center>
</form>
keepAlive
  • 6,369
  • 5
  • 24
  • 39
RAMYA
  • 89
  • 1
  • 8
  • 1
    If you want to display a invalid text message in the same Jsp page don't you think you will need some kind of HTML tag for that; where is it? And, from the servlet you want to somehow "call" the same Jsp page again and update the invalid message in that tag. There are ways to do that. You need to do little bit of research on that. – prasad_ Jan 16 '19 at 13:19
  • The `PrintWriter`'s `println` writes to the servlet generated output (not the Jsp). But, you know that already. – prasad_ Jan 16 '19 at 13:27
  • If you want to "go" from one page (HTML, Jsp or servlet) to another what APIs are used in servlet and Jsp programming? – prasad_ Jan 16 '19 at 13:29

1 Answers1

0

See if this helps you.

<script>
function validateForm() {
  var mid = document.getElementById("ManagerId").value;
  // If manager id is empty 
  if(mid===""){
     alert("Please enter valid manager id.");
    return false;
  } 
}
</script>

<form name = "reportees" method = "GET" onsubmit="return validateForm()" action="reportsto">
    <center>
    <h1><font size="20"> Enter the ID to get the Reportees</font></h1>
        <label><font size="+2">ID</font></label>
        <input name="ManagerId" id="ManagerId" ype="text" style="font-size:16pt"/>
        <input type="submit" value="submit"/>
        <button type="reset" value="Reset">cancel</button>
    </center>
</form>
Prashant Zombade
  • 482
  • 3
  • 15