2016-05-09 121 views
0

这是JSP文件:提交按钮不起作用

<%@page import="java.util.*"%> 
<%@page import="java.io.*"%> 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<html lang="en"> 
<head> 
<title>FIKT | Sign Up</title> 
<meta charset="utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
    <link rel="stylesheet" href="CSS/index.css"> 
    <link href="http://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet"> 
    <link rel="icon" type="image/x-icon" href="Images/icon.ico"> 
    <script> 
    $(document).ready(function(){ 
    $("#buttonTeacher").click(function(){ 
     $(".teacher").css("display", "block"); 

     $(".signUp").css("display", "inline"); 
     $(".student").hide(); 
    }); 

    $("#buttonStudent").click(function(){ 
     $(".student").css("display", "block"); 
     $(".signUp").css("display", "inline"); 

     $(".teacher").hide(); 
    }); 
}); 
</script> 
</head> 
<body data-spy="scroll" data-target=".navbar" data-offset="50"> 

<nav class="navbar navbar-default navbar-fixed-top"> 
    <div class="container"> 
    <div class="navbar-header"> 
     <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar"> 
     <span class="icon-bar"></span> 
     <span class="icon-bar"></span> 
     <span class="icon-bar"></span> 
     </button> 
     <a class="navbar-brand" href="Login.jsp">FIKT</a> 
    </div> 
    <div class="collapse navbar-collapse" id="myNavbar"> 
     <ul class="nav navbar-nav navbar-right"> 
     <li><a href="About.jsp">ABOUT</a></li> 
     <li><a href="Login.jsp">SIGN IN</a></li> 
     <li><a href="Register.jsp">SIGN UP</a></li> 
     <li><a href="Contact.jsp">CONTACT</a></li> 
     </ul> 
    </div> 
    </div> 
</nav> 

<div class="container" id="signInDiv"> 
<div class="row"> 
<div class="col-sm-12"> 
     <div class="panel panel-default text-center"> 
     <div class="panel-heading"> 
      <h1>SIGN UP</h1> 
     </div> 
     <div class="panel-body"> 
      <form class="form-horizontal" role="form" action="/Hello_World/RegisterServlet" method="get"> 
      <div class="form-group"> 
     <h4>Please choose:</h4><br> 
     <div class="panel-footer"> 
     <button class="btn btn-lg" id="buttonTeacher">Teacher</button> 
     <button class="btn btn-lg" id="buttonStudent">Student</button> 
     </div>  
     </div> 
    <div class="form-group teacher" style="display:none;"> 
     <label class="control-label col-sm-2" for="first">First:</label> 
     <div class="col-sm-4"> 
     <input type="text" class="form-control" id="firstName" name="firstName" placeholder="Enter your first name" required> 
     </div> 
     <label class="control-label col-sm-1" for="last">Last:</label> 
     <div class="col-sm-4"> 
     <input type="text" class="form-control" id="lastName" name="lastName" placeholder="Enter your last name" required> 
     </div> 
    </div> 
    <div class="form-group teacher" style="display:none;"> 
     <label class="control-label col-sm-2" for="idTeacher">Teacher ID:</label> 
     <div class="col-sm-4"> 
     <input type="text" class="form-control" id="teacherId" name="teacherId" placeholder="Enter your teacher ID" required> 
     </div> 
     <label class="control-label col-sm-1" for="pass">Password:</label> 
     <div class="col-sm-4"> 
     <input type="password" class="form-control" id="password" name="password" placeholder="Enter your password" required> 
     </div> 
    </div> 
    <div class="form-group teacher" style="display:none;"> 
     <label class="control-label col-sm-2" for="email">E-mail:</label> 
     <div class="col-sm-9">   
     <input type="email" class="form-control" id="email" name="email" placeholder="Enter your email" required> 
     </div> 
    </div> 





    <div class="form-group student" style="display:none;"> 
     <label class="control-label col-sm-2" for="first">First:</label> 
     <div class="col-sm-4"> 
     <input type="text" class="form-control" id="firstName" name="firstName" placeholder="Enter your first name" > 
     </div> 
     <label class="control-label col-sm-1" for="last">Last:</label> 
     <div class="col-sm-4"> 
     <input type="text" class="form-control" id="lastName" name="lastName" placeholder="Enter your last name" > 
     </div> 
    </div> 
    <div class="form-group student" style="display:none;"> 
     <label class="control-label col-sm-2" for="idStudent">Student index:</label> 
     <div class="col-sm-4"> 
     <input type="text" class="form-control" id="teacherId" name="studentId" placeholder="Enter your index" > 
     </div> 
     <label class="control-label col-sm-1" for="pass">Password:</label> 
     <div class="col-sm-4"> 
     <input type="password" class="form-control" id="password" name="password" placeholder="Enter your password" > 
     </div> 
    </div> 
    <div class="form-group student" style="display:none;"> 
     <label class="control-label col-sm-2" for="email">E-mail:</label> 
     <div class="col-sm-9">   
     <input type="email" class="form-control" id="email" name="email" placeholder="Enter your email" > 
     </div> 
    </div> 



     <div class="panel-footer"> 
      <button class="btn btn-lg signUp" type="submit" style="display:none;">Sign Up</button> 
     </div> 
     </form> 
     </div> 
    </div> 
    </div> 
</div> 




<footer class="container-fluid text-center"> 
    <a href="#myPage" title="To Top"> 
    <span class="glyphicon glyphicon-chevron-up"></span> 
    </a> 
    <p>Bootstrap Theme Made By <a href="http://www.kddesign.comlu.com" title="Visit KDdesign">&copy KDdesign All rights reserved 2016</a></p> 
</footer> 

</body> 
</html> 

和Java Servlet的处理它:

package gui; 

import java.io.IOException; 

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

/** 
* Servlet implementation class Helloworld 
*/ 
@WebServlet("/RegisterServlet") 
public class RegisterServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    /** 
    * @see HttpServlet#HttpServlet() 
    */ 
    public RegisterServlet() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 

    /** 
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     // TODO Auto-generated method stub 


     String teacherId = request.getParameter("teacherId"); 
     String studentId = request.getParameter("studentId"); 

     String firstName = request.getParameter("firstName"); 
     String lastName = request.getParameter("lastName"); 
     String password = request.getParameter("password"); 
     String email = request.getParameter("email");  

     DbConnector db = new DbConnector(); 
     db.Connect(); 
     System.out.println(firstName+" "+lastName+" "+teacherId); 

     System.out.println(studentId); 

     if (firstName !=null && password !=null && !db.validateUser(firstName, password)){ 
      if (teacherId!=null){ 
       System.out.println("teachers"); 
       db.addUser(teacherId, firstName, lastName, password, email,"teacher"); 
      } 
      else { 
       System.out.println("studenti"); 
       db.addUser(studentId, firstName, lastName, password, email,"student"); 
      } 
       request.setAttribute("user", firstName); 
       request.getRequestDispatcher("Success.jsp").forward(request, response); 
     } 
     else 
      System.out.print("FAILED!"); 


    } 

    /** 
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     // TODO Auto-generated method stub 


     doGet(request, response); 
    } 

} 

当学生的div被选中,点击注册该按钮不运行servlet RegisterServlet.Why我的按钮只为老师div提交而不是两个..?

回答

0

我不能肯定地说我的回答是正确的,但没有在GET请求中看到表单数据,但我怀疑学生的价值观没有被赞成教师价值 - 即使它们为空/空 - 因为在input这些字段中,您的老师和学生的input字段只有一个form,并且具有相同的name属性。这意味着,firstNamelastName提交了两次,它会是这个样子,当学生尝试提交表单:

firstName :   [Teacher] 
firstName : "John" [Student] 
lastName :   [Teacher] 
lastName : "Doe" [Student] 

,因为他们首先出现教师值可以使用,但如果你填在学生的表格中,他们将是空的,所以它不会工作。

最好的情况是,两个值都会被服务器端代码推送到同一个属性的数组中,然后这个行为已经意外。也许像firstName = ["","John"],但老实说,我不知道JSP如何处理它,因为我没有开发JSP,来自Perl背景。正如我所说的那样,JSP可能只是在表单数据中找到的属性的第一个值。

这同样适用于所有的复制name属性,即密码,电子邮件等

为了避免这个问题,老师和学生input元素firstName分为2个独立form元素,所以只有一组值和lastName在有人尝试注册/登录时提交给服务器,而不是两个集合,如果有意义的话。对投入要素的id属性也应该是唯一的,所以在您input元素当前可能共享相同的id="firstName"应该id="teacherFirstName"id="studentFirstName"防止解析/ HTML有效性的问题。

此外,我可以看到您正在使用DOCTYPE的HTML4 Transitional。您的input字段包含requiredplaceholder属性,它们是HTML5功能,而且您的body元素具有data-属性,这些属性也是HTML5功能。我推荐使用HTML5 DOCTYPE(<!DOCTYPE html>)或考虑修改代码以删除HTML5功能,以防止针对您定位的任何浏览器解析错误。使用HTML5 DOCTYPE可能会更好,因为Bootstrap会执行很多data-属性操作。

我还可以看到您指定了两个不同的字符集:第一个ISO-8859-1通过JSP和meta http-equiv标记声明,然后UTF-8通过meta charset="utf-8"。最好不要同时提供并使用您需要的任何字符集。 meta http-equiv标签也应该包含在您的<head>元素中,否则HTML解析器可能会忽略它。

您还需要添加更多的</div>关闭<div class="panel-body">&copy&copy;以防止可能的语法上的问题。

请参见下面的代码片段通过JSP中我建议的赔偿生成的HTML(如果UTF-8是罚款)

JSP序言:

<%@ page import="java.util.*" %> 
<%@ page import="java.io.*" %> 
<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8" %> 

HTML输出:

<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <title>FIKT | Sign Up</title> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
 
    <link rel="stylesheet" href="CSS/index.css"> 
 
    <link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet"> 
 
    <link rel="icon" type="image/x-icon" href="Images/icon.ico"> 
 
    <script> 
 
    $(document).ready(function(){ 
 
    $("#buttonTeacher").click(function(){ 
 
     $(".teacher").css("display", "block"); 
 
     $(".student").hide(); 
 
    }); 
 

 
    $("#buttonStudent").click(function(){ 
 
     $(".student").css("display", "block"); 
 
     $(".teacher").hide(); 
 
    }); 
 
    }); 
 
    </script> 
 
</head> 
 
<body data-spy="scroll" data-target=".navbar" data-offset="50"> 
 

 
<nav class="navbar navbar-default navbar-fixed-top"> 
 
    <div class="container"> 
 
    <div class="navbar-header"> 
 
     <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar"> 
 
     <span class="icon-bar"></span> 
 
     <span class="icon-bar"></span> 
 
     <span class="icon-bar"></span> 
 
     </button> 
 
     <a class="navbar-brand" href="Login.jsp">FIKT</a> 
 
    </div> 
 
    <div class="collapse navbar-collapse" id="myNavbar"> 
 
     <ul class="nav navbar-nav navbar-right"> 
 
     <li><a href="About.jsp">ABOUT</a></li> 
 
     <li><a href="Login.jsp">SIGN IN</a></li> 
 
     <li><a href="Register.jsp">SIGN UP</a></li> 
 
     <li><a href="Contact.jsp">CONTACT</a></li> 
 
     </ul> 
 
    </div> 
 
    </div> 
 
</nav> 
 

 
<div class="container" id="signInDiv"> 
 
    <div class="row"> 
 
    <div class="col-sm-12"> 
 
     <div class="panel panel-default text-center"> 
 
     <div class="panel-heading"> 
 
      <h1>SIGN UP</h1> 
 
     </div> 
 
     <div class="panel-body"> 
 
      
 
      <div class="form-group"> 
 
       <h4>Please choose:</h4><br> 
 
       <div class="panel-footer"> 
 
       <button class="btn btn-lg" id="buttonTeacher">Teacher</button> 
 
       <button class="btn btn-lg" id="buttonStudent">Student</button> 
 
       </div> 
 
      </div> 
 
      
 
      <form class="form-horizontal teacher" role="form" action="/Hello_World/RegisterServlet" method="get"> 
 
      <div class="form-group teacher" style="display:none;"> 
 
       <label class="control-label col-sm-2" for="first">First:</label> 
 
       <div class="col-sm-4"> 
 
       <input type="text" class="form-control" id="firstName" name="firstName" placeholder="Enter your first name" required> 
 
       </div> 
 
       <label class="control-label col-sm-1" for="last">Last:</label> 
 
       <div class="col-sm-4"> 
 
       <input type="text" class="form-control" id="lastName" name="lastName" placeholder="Enter your last name" required> 
 
       </div> 
 
      </div> 
 
      <div class="form-group teacher" style="display:none;"> 
 
       <label class="control-label col-sm-2" for="idTeacher">Teacher ID:</label> 
 
       <div class="col-sm-4"> 
 
       <input type="text" class="form-control" id="teacherId" name="teacherId" placeholder="Enter your teacher ID" required> 
 
       </div> 
 
       <label class="control-label col-sm-1" for="pass">Password:</label> 
 
       <div class="col-sm-4"> 
 
       <input type="password" class="form-control" id="password" name="password" placeholder="Enter your password" required> 
 
       </div> 
 
      </div> 
 
      <div class="form-group teacher" style="display:none;"> 
 
       <label class="control-label col-sm-2" for="email">E-mail:</label> 
 
       <div class="col-sm-9"> 
 
       <input type="email" class="form-control" id="email" name="email" placeholder="Enter your email" required> 
 
       </div> 
 
      </div> 
 
      <div class="panel-footer"> 
 
       <button class="btn btn-lg teacherSignUp" type="submit" style="display:none;">Sign Up</button> 
 
      </div> 
 
      </form> 
 
      
 
      <form class="form-horizontal student" role="form" action="/Hello_World/RegisterServlet" method="get"> 
 
      <div class="form-group student" style="display:none;"> 
 
       <label class="control-label col-sm-2" for="first">First:</label> 
 
       <div class="col-sm-4"> 
 
       <input type="text" class="form-control" id="studentFirstName" name="firstName" placeholder="Enter your first name" > 
 
       </div> 
 
       <label class="control-label col-sm-1" for="last">Last:</label> 
 
       <div class="col-sm-4"> 
 
       <input type="text" class="form-control" id="studentLastName" name="lastName" placeholder="Enter your last name" > 
 
       </div> 
 
      </div> 
 
      <div class="form-group student" style="display:none;"> 
 
       <label class="control-label col-sm-2" for="idStudent">Student index:</label> 
 
       <div class="col-sm-4"> 
 
       <input type="text" class="form-control" id="studentId" name="id" placeholder="Enter your index" > 
 
       </div> 
 
       <label class="control-label col-sm-1" for="pass">Password:</label> 
 
       <div class="col-sm-4"> 
 
       <input type="password" class="form-control" id="studentPassword" name="password" placeholder="Enter your password" > 
 
       </div> 
 
      </div> 
 
      <div class="form-group student" style="display:none;"> 
 
       <label class="control-label col-sm-2" for="email">E-mail:</label> 
 
       <div class="col-sm-9"> 
 
       <input type="email" class="form-control" id="studentEmail" name="email" placeholder="Enter your email" > 
 
       </div> 
 
      </div> 
 
      <div class="panel-footer"> 
 
       <button class="btn btn-lg studentSignUp" type="submit" style="display:none;">Sign Up</button> 
 
      </div> 
 
      </form> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div> 
 

 
<footer class="container-fluid text-center"> 
 
    <a href="#myPage" title="To Top"> 
 
    <span class="glyphicon glyphicon-chevron-up"></span> 
 
    </a> 
 
    <p>Bootstrap Theme Made By <a href="http://www.kddesign.comlu.com" title="Visit KDdesign">&copy; KDdesign All rights reserved 2016</a></p> 
 
</footer> 
 

 
</body> 
 
</html>

Code diff