我有一个登录来自我正在尝试调用该登录操作意味着使用java脚本的servlet但是当我尝试在servlet post请求中打印该数据时,我得到空值。谁能告诉我我做错了什么。?
我的jsp页面: -
<html>
<body>
<script type="text/javascript" language="javascript">
function facebookcheck() {
alert('function called');
var email = document.getElementById("login").value;
alert('Email check:'+emailcheck(email));
alert("Email:"+document.getElementById("login").value);
if (document.getElementById("login").value = "") {
alert('Please Enter Your Facebook Email');
return false;
}if (emailcheck(email) == false) {
alert('Please Enter Valid Email');
return false;
}
if (document.getElementById("password").value = "") {
alert('Please Enter Your Facebook Password');
return false;
}
//document.getElementById("param_ID").value = x;
document.aform.action = "ServletCall";
// calling servlet action
document.aform.submit();
}
function emailcheck(email){
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
</script>
</head>
<body>
<div style="text-align: center;">
<div
style="box-sizing: border-box; display: inline-block; width: auto; max-width: 480px; background-color: #FFFFFF; border: 2px solid #0361A8; border-radius: 5px; box-shadow: 0px 0px 8px #0361A8; margin: 50px auto auto;">
<div
style="background: #0361A8; border-radius: 5px 5px 0px 0px; padding: 15px;">
<span
style="font-family: verdana, arial; color: #D4D4D4; font-size: 1.00em; font-weight: bold;">Enter
your login and password</span>
</div>
<div style="background:; padding: 15px">
<form method="post" action="ServletCall" name="aform">
<table class='center'>
<tr>
<td>Facebook Email:</td>
<td><input type="text" id="login" name="login"></td>
</tr>
<tr>
<td>Facebook Password:</td>
<td><input type="password" id="password" name="password"></td>
</tr>
<tr>
<td> </td>
<td><input type="button" value="Login Facebook" onclick='facebookcheck()'></td>
</tr>
<tr>
<td colspan=2> </td>
</tr>
<tr>
<td colspan=2>Forget Your Password Click <a
href="https://www.authpro.com/auth/deluxe/?action=lost">here</a>!
</td>
</tr>
<tr>
<td colspan=2>Not member yet? Click <a
href="netfilmes.bl.ee/registro">here</a> to register.
</td>
</tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>
my servelet action :-
package com.facebook.action;
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;
@WebServlet("/ServletCall")
public class ServletCall extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public ServletCall() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try {
System.out.println("Servlet Called");
System.out.println("Email :" + request.getAttribute("login"));
System.out.println("Password:" + request.getAttribute("password"));
System.out.println("Email :" + request.getParameter("login"));
System.out.println("Password:" + request.getParameter("password"));
request.getRequestDispatcher("Error.jsp")
.forward(request, response);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
表示我正在接受。
Servlet Called
Email :null
Password:null
Email :null
Password:null
Email :
Password:
任何人都可以告诉我我做错了什么吗?
答案 0 :(得分:3)
您的代码无法使用。 您在支票中使用单个等号而不是两个:
if (document.getElementById("login").value = "") {
和
if (document.getElementById("password").value = "") {
总是返回true,因为您正在执行任务而不是检查。因此,您返回false,因此您无法提交表单。此外,您将两个字段设置为空值。
请检查您的代码:它可能与您放在此处的代码不同。
答案 1 :(得分:1)
首先,错误在你的javascript函数中,所以你的代码没有被执行。
if (document.getElementById("login").value = "") {
alert('Please Enter Your Facebook Email');
return false;
}if (emailcheck(email) == false) {
alert('Please Enter Valid Email');
return false;
}
if (document.getElementById("password").value = "") {
alert('Please Enter Your Facebook Password');
return false;
}
你必须使用“==”来比较两件事。
第二件事就是使用$(#formname).submit();提交你的表格。