框架
index.jsp
< % @ page contentType= "text/html;charset=UTF-8" language= "java" % >
< html>
< head>
< title> 这是jsp界面< / title>
< % -- 导入jquery控件 . /同级目录-- % >
< script src= "./js/jquery-1.11.1.min.js" > < / script>
< / head>
< script type = "text/javascript" >
alert ( "这是我的web项目" )
function def ( ) {
alert ( "这是一个函数" )
var biaoshi2= $( "#idName" ) . val;
var biaoshi= $( "[name='biaoshi']" ) . val ( ) ;
alert ( "这个值是" + biaoshi)
}
< / script>
< body>
list.jsp
< % --
1. 什么是jquery
jquery其实就是一个Java script的一个类库
2. 有什么作用
简化了 java script的开发
3. jquery是一个框架或者说是一个控件 如果要使用的话 我们需要导入jquery控件
-- % >
< table>
< tr>
< td> 用户名< / td>
< td> < input type= "text" id= "idName" name= "biaoshi" > < / td>
< td> < input type= "button" value= "测试" onclick= "def()" > < / td>
< / tr>
< tr>
< td> 密码< / td>
< td> < input type= "text" id= "idName2" name= "biaoshi2" > < / td>
< td> < input type= "button" value= "测试" onclick= "def()" > < / td>
< / tr>
< tr>
< td> < input type= "button" value= "提交" onclick= "alert('你好啊')" > < / td>
< / tr>
< tr>
< td> 测试函数< / td>
< td> < input type= "button" value= "测试函数" onclick= "def()" > < / td>
< / tr>
< / table>
< / body>
< / html>
< % @ page contentType= "text/html;charset=UTF-8" language= "java" % >
< html>
< head>
< title> Title< / title>
< / head>
< % -- 导入jquery控件-- % >
< script src= "./js/jquery-1.11.1.min.js" > < / script>
< script>
function fun ( ) {
var idName= $( "[name='idName']" ) . val ( ) ;
$. ajax ( {
url: "/WebServlet?method=aaaaaa" ,
type: "post" ,
data: { idName: idName} ,
success: function ( flag) {
if ( flag) {
alert ( "后端已经接受到我传递的数据了" )
}
} ,
dateType: "json"
} )
}
< / script>
< body>
< input type= "text" name= "idName" id= "idName" >
< input type= "button" value= "提交" onclick= "fun()" >
< / body>
< / html>
webServet.java
package servlet_test;
import com. alibaba. fastjson. JSON;
import com. sun. javaws. IconUtil;
import javax. servlet. ServletException;
import javax. servlet. http. HttpServlet;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse;
import java. io. IOException;
public class WebServet extends HttpServlet {
@Override
protected void service ( HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method= req. getParameter ( "method" ) ;
String idName= req. getParameter ( "idName" ) ;
System. out. println ( "method的值为" + method+ "idName的值为" + idName) ;
boolean flag= true ;
String flagJson= JSON. toJSONString ( flag) ;
resp. getWriter ( ) . print ( flagJson) ;
}
}
web.xml
< ? xml version= "1.0" encoding= "UTF-8" ? >
< web- app xmlns= "http://xmlns.jcp.org/xml/ns/javaee"
xmlns: xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation= "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version= "4.0" >
< servlet>
< description> < / description>
< display- name> WebServlet< / display- name>
< servlet- name> WebServlet< / servlet- name>
< servlet- class > servlet_test. WebServet< / servlet- class >
< / servlet>
< servlet- mapping>
< servlet- name> WebServlet< / servlet- name>
< url- pattern> / WebServlet< / url- pattern>
< / servlet- mapping>
< / web- app>