当我调用方法request.getParameter(" username")时,我得到null,为什么我没有得到正确的json字符串格式?我也尝试使用JSON.stringify(),但结果是一样的。我希望有人知道出了什么问题。
LoginServlet.java
package org.SalonNamestaja.Controller.Ajax;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.SalonNamestaja.Services.LoginService;
import org.apache.catalina.User;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
List<User> users = new LinkedList<User>();
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
String jsonUsername = request.getParameter("username");
String jsonPassword = request.getParameter("pass");
PrintWriter out = response.getWriter();
out.print("Hello " + jsonUsername + " " + jsonPassword );
}
}
login_user.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Prijava korisnika</title>
<script src="javascript/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script >
$(document).ready(function(){
var $name = $('#userName');
var $pass = $('#password');
$('#form').submit(function() {
$.post("/SalonNamestaja/login", {username:$('#userName').val(), pass:$('#password').val()},function(data){
alert(data);
});
});
});
</script>
</head>
<body>
<form id="form" action="/SalonNamestaja/login" method="post">
Korisnicko ime:<input id="userName" type="text" name="userName"><br>
Sifra:<input id="password" type="password" name="password"><br>
<input type="submit" value="Prijavi se">
</form>
<div id="welcometext">
</div>
</body>
</html>
答案 0 :(得分:0)
我认为你认为javascript正在以php运行。 当文档准备就绪时 - 当您打开浏览器时,您将获取变量并发送$ .post。我假设您想在表单提交上执行此操作。将脚本修改为:
<script>
function sendForm() {
// PUT YOUR CODE HERE
}
</script>
并修改您的表单,您的提交按钮标记应如下所示:
<input type="submit" value="Prijavi se" onclick="sendForm()">
我不确定这是否会解决您的问题,您可以通过迭代request.getParameterNames
检查是否发送了任何参数