侧边栏壁纸
博主头像
王小木人个人技术文章记录

这是很长,很好的一生

  • 累计撰写 141 篇文章
  • 累计创建 43 个标签
  • 累计收到 10 条评论

目 录CONTENT

文章目录
js

jsp servlet ajax 提交表单

王小木人个人技术文章记录
2021-05-26 / 0 评论 / 0 点赞 / 1,907 阅读 / 1,390 字

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>Insert title here</title>
</head>
<body>
	<form id="myform">
		<input type="text" name="name" /> <input type="button" id="btn"
			value="提交" />
	</form>
	<script src="js/jquery-3.3.1.min.js" type="text/javascript"></script>
	<script type="text/javascript">
		$('#btn').click(function() {
			$.ajax({
				type : "POST",
				url : 'system/usercheck',
				data : $('#myform').serialize(),
				async : false,
				error : function(request) {
					alert("失败");
				},
				success: function(json) {
					alert(json);
				}
			});
		});
	</script>
</body>
</html>

​

servlet 代码

​
package com.org.learn;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/system/usercheck")
public class Student extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub

		System.out.println("get");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name=req.getParameter("name");
		PrintWriter out=resp.getWriter();
		out.print(name);
	
	}

}

​

web.xml配置

​
  <servlet>
      <servlet-name>Student</servlet-name>
      <servlet-class>com.org.learn.Student</servlet-class>
    </servlet>
    <servlet-mapping>
       <servlet-name>Student</servlet-name>
       <url-pattern>/Student</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

​
0

评论区