我正在尝试创建一个伪登录页面,该页面将在登录或签入后将我们链接到主页,我使用的是Ajax和servlet,因为这是他们要求在学校使用的。
问题是,当我单击“登录”时,它在我的Ajax中输入了错误,我到处都是,但找不到错误。
这是我的Javascript代码;
$(function() {
$('#connect').on('click',function() {
var json = formToJSON($('.form'));
$.ajax({
url: "/conn",
type: "POST",
data:{form:JSON.stringify(json)},
success: function(response, textStatus, jqXHR) {
location.href = "http://localhost:8080/home.html";
},
error: function(e) {
console.log(e.message);
}
});
});
$('#login').on('click',function() {
var json = formToJSON($('.form'));
$.ajax({
url: "/inscr",
type: "POST",
data: {form:JSON.stringify(json)},
success: function(response, textStatus, jqXHR) {
location.href = "http://localhost:8080/home.html";
},
error: function(e) {
console.log(e.message);
}
});
});
});
var formToJSON = function(form) {
var data = {};
data["username"] = $("#username").val();
data["password"] = $("#password").val();
console.log(data);
return data;
}
这是我的Java servlet(我们使用Jetty): MAIN.java;
import javax.servlet.http.HttpServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;
public class Main {
public static void main(String[] args)throws Exception{
Server server = new Server(8080);
WebAppContext context = new WebAppContext();
context.setResourceBase("www");
context.setContextPath("/");
HttpServlet ds=new DefaultServlet();
HttpServlet c=new Connection();
HttpServlet i=new Inscrir();
context.addServlet(new ServletHolder(i), "/inscr");
context.addServlet(new ServletHolder(c), "/conn");
context.addServlet(new ServletHolder(ds), "/");
server.setHandler(context);
server.start();
}
}
Inscrir.java;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.auth0.jwt.JWTSigner;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.JWTVerifyException;
import com.owlike.genson.Genson;
import com.owlike.genson.GensonBuilder;
public class Inscrir extends HttpServlet {
private static String SECRET = "jkdh lqjk lcx wuich liuhza";
Genson genson = new GensonBuilder().create();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
System.out.println("ok");
Map<String, Object> claims = new HashMap<String, Object>();
String json = req.getParameter("form");
User user = genson.deserialize(json, User.class);
claims.put(user.getName(), user);
String token = new JWTSigner(SECRET).sign(claims);
System.out.println("Chaîne JWT=" + token);
/* cette chaîne peut être mise en cookie par exemple : */
Cookie cookie = new Cookie("user", token);
cookie.setPath("/");
cookie.setMaxAge(60 * 60 * 24);
resp.addCookie(cookie);
Map<String, Object> decodedPayload = new JWTVerifier(SECRET).verify(token);
System.out.println("Contenu=" + decodedPayload);
} catch (SignatureException signatureException) {
System.err.println("Invalid signature!");
} catch (IllegalStateException illegalStateException) {
System.err.println("Invalid Token! " + illegalStateException);
} catch (InvalidKeyException e) {
System.err.println("Invalid Key! " + e);
} catch (NoSuchAlgorithmException e) {
System.err.println("Invalid Algorithm! " + e);
} catch (IOException e) {
e.printStackTrace();
} catch (JWTVerifyException e) {
System.err.println("JWT Verify Exception! " + e);
}
}
}
User.java;
public class User {
private String name;
private String psw;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPsw() {
return psw;
}
public void setPsw(String psw) {
this.psw = psw;
}
public User(String name, String psw) {
super();
this.name = name;
this.psw = psw;
}
}
当我单击“登录”时,控制台中也会出现未定义的内容。