只适合初学Web的小白哈,大神忽略即可
(1)首先新建一个Dynamic Web Project项目,我的项目名叫MyWebTest,(2)在WebContent->WEN-INF文件夹下新建一个web.xml配置文件,文件内容如下:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true">
其中
(3)在WebContent目录下新建一个index.jsp文件,下面列出index.jsp的表单的代码:
上面的代码就可以把表单提交给RegisterServlet这个类处理,action由两部分构成,第一部分是项目的名字,MyWebTest,特别不能少了这个,第二部分是在web.xml中的url-pattern中指定。
(4)错误总结:如果在表单中的action中少了第一部分,也就是项目的名字,那么浏览器会提示找不到请求的url,返回的404。
(5)下面是使用ajax提交请求到RegisterServlet处理的完整代码:
----web.xml中代码
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true">
---index.jsp中代码
*{
margin:0px;
padding: 0px;
}
#main{
margin:0 auto;
font-size:14px;
color:#000;
width:100%;
}
div label{
display: inline-block;
width:180px;
}
div input{
display: inline-block;
border:2px solid #blue;
width:200px;
font-weight:bold;
color: #000;
}
div{
margin:2px;
}
function validatePassword(){
var rePassword=document.getElementById("rePassword");
var password=document.getElementById("password");
if(rePassword.value===password.value){
//passwords are the same,you can do something here
}else{
//passwords are not equal,you also can do something here
alert("passwords are not the same!");
}
}
function validateUserName(){ //validate whether the username is exit
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function(){
console.log(1);
if(xhr.readyState===4){
console.log(4);
if(xhr.status===200){
console.log(200);
alert(xhr.responseText);
}else{
console.log(404);
alert("validate username error!");
}
}
};
//xhr.open("get","/MyWebTest/RegisterServlet?username="+document.getElementById("username").value,true);
//xhr.send(null);
//if you use the get method,the parameters you transport to server must be in the open function,and the send must be null
xhr.open("post","/MyWebTest/RegisterServlet",true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
xhr.send("username="+document.getElementById("username").value);
}
---RegisterServlet.java中代码,在com.myWebTest.servlet包名下
package com.myWebTest.servlet;
import java.io.Console;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@SuppressWarnings("serial")
public class RegisterServlet extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
String username=request.getParameter("username");
PrintWriter writer=response.getWriter();
HttpSession session=request.getSession();
Integer count=(Integer)session.getAttribute("count");
if(count==null){
count=0;
}
session.setAttribute("count", ++count);
if(username != null&&username.equals("mbj")){
writer.write("username is exit you visit "+count+" times");
}else{
writer.write("username is not exit you visit "+count+" times");
}
writer.close();
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
this.doGet(request, response);
}
}