Demand:
Input parameters to the Web page, pass the parameters to the server via Ajax, and receive on the server side
1, create a new Web project, Name: Ajax
2. Modify Index.jsp
The code is as follows
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<%@ page contenttype= "text/html; Charset=utf-8 "%>
<TITLE>WWW.MLDNJAVA.CN,MLDN high-end Java Training </title>
<body>
<script language= "JavaScript" >
var xmlHttp; Ajax Core Object Name
function Createxmlhttp () {//Create XMLHttpRequest Core Object
if (window. XMLHttpRequest) {//To determine the type of browser currently in use
XmlHttp = new XMLHttpRequest (); The browser that is currently using the Firefox kernel
} else {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
}
}
function Checkuserid () {
Createxmlhttp (); Establish XMLHTTP Core objects
Set up a request to pass the UserID to the JSP through address rewriting
var UserID = document.getElementById ("userid"). Value;
Xmlhttp.open ("POST", "ajax/checkservlet?userid=" + userid);
Xmlhttp.send (NULL); Send a request without passing any arguments
document.getElementById ("msg"). InnerHTML = "Transmission End";
}
</script>
User id:
<input type= "text" id= "userid" >
<span id= "MSG" ></span>
<br>
<button onclick= "Checkuserid ()" > Transmission </button>
</body>
3. Create Checkservlet.java
Package Com.ajax;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse; public class Checkservlet extends HttpServlet {<span style= "white-space:pre" > </span>public void doGe T (httpservletrequest request, <span style= "White-space:pre" > </span>httpservletresponse response) throws
Servletexception, IOException {<span style= "White-space:pre" > </span>this.dopost (Request, response); <span style= "White-space:pre" > </span>} <span style= "White-space:pre" > </span>public void DoPost (httpservletrequest request, <span style= "White-space:pre" > </span>httpservletresponse) Throws Servletexception, IOException {<span style= "White-space:pre" > </span>
Request.setcharacterencoding ("UTF-8"); <span style= "White-space:pre" > </span>response. setContentType ("text/html"); Set the MIME <span of the response style= "White-space:pre" > </span>string userid = Request.getparameter ("userid");
Receive validated userid <span style= "White-space:pre" > </span>system.out.println (userid);
<span style= "White-space:pre" > </span>}
4, in the Web.xml configuration
<?xml version= "1.0" encoding= "UTF-8"?> <web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http:// Xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd "id=" webapp_id "version=" 3.1 "> <display-name>ajax</ display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file >index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file> Default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file> default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>checkservlet</ Servlet-name> <servlet-class>com.ajax.CheckServlet</servlet-class> </servlet> < Servlet-mapping> <servlet-name>CheckServlet</servlet-name> <url-pattern>/ajax/checkserVlet</url-pattern> </servlet-mapping> </web-app>
This allows you to receive the parameters that the front end sends over the background.