J2EE11 servlet AJAX练习 validate验证 layer

这篇博客介绍了在J2EE11环境下,结合servlet和AJAX进行表单数据的validate验证,并且利用layer组件提升用户体验。通过导入依赖包,设置basedao包,详细阐述了在servlet和jsp中如何实现验证及使用layer弹层组件的方法。
摘要由CSDN通过智能技术生成

1.导入依赖包
在这里插入图片描述
2.basedao包
在这里插入图片描述

servlet:

package com.test.homework;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

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

import com.alibaba.fastjson.JSON;
import com.test.dao.BaseDao;
import com.test.pojo.User;
import com.test.userdao.UserDao;

@WebServlet("/HwServlet")
public class HwServlet extends HttpServlet {
   

	UserDao userdao = new UserDao();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   

		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		String method = req.getParameter("method");

		try {
   
			if ("toAjax".equals(method)) {
   
				toAjax(req, resp);
			} else if ("refresh".equals(method)) {
   
				refresh(req, resp);
			}

		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) {
   
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void toAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException,
			ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
   
		// TODO Auto-generated method stub
		List<User> list = userdao.selectAll();
		req.setAttribute("list", list);

		req.getRequestDispatcher("/WEB-INF/page/indexAJAX.jsp").forward(req, resp);

	}

	private void refresh(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
   
		// TODO Auto-generated method stub
		PrintWriter printWriter = resp.getWriter();
		String id = req.getParameter("id");

		User user = userdao.selectById(Integer.parseInt(id));

		printWriter.append(JSON.toJSONString(user));
	}
}

Ajax.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值