JavaWeb创建一个公共的servlet,减去繁琐的doget、dopost,好好看好看学。

对于初学者来说,每次前端传数据过来就要新建一个类创建一个doget、dopost方法,其实铁柱兄在大学的时候也是这么玩的。后面铁柱兄开始认真了,就想着学习点容易的编程方式,其实说白了就是想偷懒。

新建一个Web项目,创建包、类。然后编写BaseServlet类。

package com.tiezhuxiong.base;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; 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 javax.servlet.http.HttpSession;
@WebServlet(name = "BaseServlet")
public class BaseServlet extends HttpServlet{ /**
* 创建一个公共的servlet类,所有从前端过来的请求都要走这个类
* 写一个公共的servlet,以后前端只需要把method送上来,
* 可以根据method送上来的值去找下面的方法名。减去了每次都dogetdopost方法
* 注:方法名一定要与method送上来的值相同。需要urlPatterns注解
* 如:提交的地址是test,则注解urlpatterns里要写/test
*
* 2019.12.3
*/
private static final long serialVersionUID = 1L; protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8");
resp.setContentType("html/text;charset=utf-8");
try {
// 获取前端传过来的method字段
String method = req.getParameter("method");
System.out.println(method);
// 获取到当前对象的字节码文件
Class clazz=this.getClass();
// 拿到字节码对象中的方法
Method clazzMethod = clazz.getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
// 执行方法
clazzMethod.invoke(this,req,resp);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}

然后开始编写LoginServlet类:

package com.tiezhuxiong.servlet;

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.tiezhuxiong.base.BaseServlet; @WebServlet(name="LoginServlet",urlPatterns="/login")
public class LoginServlet extends BaseServlet{
/**
*
*/
private static final long serialVersionUID = 1L; public void login(HttpServletRequest req,HttpServletResponse resp) throws IOException {
String username=req.getParameter("username");
String pwd=req.getParameter("pwd");
System.out.println(username+pwd); if(username!=""&&pwd!=""){
resp.sendRedirect("https://www.cnblogs.com/");//接收到值的话就跳转到博客园首页
}else{
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("没有接收到数据");//没接收到的提示
} }
}

前端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 action="login">
<input type="text" name="username">
<input type="text" name="pwd">
<input type="hidden" name="method" value="login"><!-- 后端根据value的值寻找方法 -->
<button>提交</button>
</form>
</body>
</html>

现在我们把项目跑起来看看

好了,本期的教程就到这里了,后续铁柱兄将继续给大家带来新的代码。谢谢观赏~

觉得有用的话,记得点个赞呦。

最新文章

  1. 聊一聊PV和并发
  2. 分布式集群搭建(hadoop2.6.0+CentOS6.5)
  3. 关于最近折腾的ubuntu12.10
  4. 74 partprobe-磁盘管理
  5. 夺命雷公狗-----React---8--react官方提供的组建实现双向绑定
  6. 数组json格式的字符串 转 list&lt;Bean&gt;
  7. 花神的数论题(数位dp)
  8. nginx源码学习资源(不断更新)
  9. 38. Count and Say
  10. Careercup - Facebook面试题 - 5188884744896512
  11. 继续畅通工程(kruskal prim)
  12. pdf转图片
  13. 关于前台主键输入错误对后台hibernate方法的影响
  14. Bresenham画椭圆算法
  15. idea 右键无java class选项
  16. WEB前端开发常见问题汇总
  17. Python&#160;关于xpath查找XML元素的一点总结
  18. Practical Node.js (2018版) 第8章:Building Node.js REST API Servers
  19. Vivado中debug用法
  20. 【Java】HashTable和HashMap区别

热门文章

  1. Selenium(七):选择框(radio框、checkbox框、select框)
  2. Scrapy框架-爬虫程序相关属性和方法汇总
  3. javaWeb核心技术第十二篇之分页和条件
  4. DevExpress的TreeList实现显示本地文件目录并自定义右键实现删除与重命名文件
  5. SSM框架之SpringMVC(2)参数绑定及自定义类型转换
  6. 前端知识体系-NodeJS相关】NodeJS基础知识全面总结
  7. JS 实现
  8. 编译原理之不懂就问-First集
  9. 前端如何快速定位问题传参 和false
  10. MYSQL 命令导出事件、存储过程、触发器