<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<title>Insert title here</title>
</head>
<body>
<label for="name">用户名:</label>
<input type="text" id="username" />
<br/>
<label for="password">密码:</label>
<input type="text" id="password" />
<br/>
<label for="againPassword">确认密码:</label>
<input type="text" id="againPassword" />
<br/>
<!-- 下拉列表 -->
<label for="country">国家:</label>
<select id="country">
<option value="China">中国</option>
<option value="USA">美国</option>
<option value="UK">英国</option>
</select>
<br/>
<input type="button" value="注册" onclick="regist()"/>
</body>
<script>
function regist(){
var username = $("#username").val();
var password = $("#password").val();
var againPassword = $("#againPassword").val();
var country = $("#country").val();
$.ajax({
type:"get",
url:"/BigData02/RegistServlet",
data:{"username":username,"password":password,"againPassword":againPassword,"country":country},
success:function(data){
console.log(data);
}
})
}
</script>
</html>
package com.bigdata;
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 RegistServlet
*/
@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RegistServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String againPassword = request.getParameter("againPassword");
String country = request.getParameter("country");
System.out.println(username + " " + password + " " + againPassword + " " + country);
}
/**
* @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);
}
}
这是一个简单的HTML文件,用于用户注册。页面包括一些表单元素,用于收集用户的用户名、密码、确认密码和国家信息。用户填写完这些信息后,点击“注册”按钮会触发一个名为regist()的JavaScript函数。该函数使用jQuery发起一个AJAX请求到一个名为RegistServlet的Servlet,将用户填写的信息作为数据发送到服务器。服务器成功处理请求后,会在控制台输出返回的数据。
这段代码实现了一个简单的用户注册功能,通过填写表单信息并点击注册按钮,将用户数据发送到服务器。