Java web creates a public servlet

Keywords: Java Programming JSP

Java web creates a public servlet, subtracts the tedious doget and dopost, and takes a good look at it.

For beginners, each time the front-end data comes, they need to create a new class to create a doget and dopost method. In fact, Tiezhu played the same way when he was in college. Later brother Tiezhu began to be serious. He wanted to learn some easy programming methods. In fact, he wanted to be lazy.

Create a new Web project, create package and class. Then write the BaseServlet class.




package com.tiezhuxiong.base;

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{

     * Create a public servlet class, which is used for all requests from the front end
     * Write a public servlet. In the future, the front end only needs to send the method,
     * You can find the following method name according to the value sent by method. Subtract the dogetdopost method every time
     * Note: the method name must be the same as the value sent by method. urlPatterns annotation required
     * For example, if the submitted address is test, the comment urlpatterns should write / test
     * 2019.12.3
    private static final long serialVersionUID = 1L;

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            try {
                // Get from the front end method field
                String method = req.getParameter("method");
                // Get bytecode file of current object
                Class clazz=this.getClass();
                // How to get the bytecode object
                Method clazzMethod = clazz.getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
                // Execution method
            } catch (NoSuchMethodException e) {
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {




Then start writing the LoginServlet class:

package com.tiezhuxiong.servlet;


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

import com.tiezhuxiong.base.BaseServlet;

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");
                 resp.sendRedirect("");//If you receive the value, you will jump to the homepage of blog park
                 resp.getWriter().write("No data received");//Tips not received

Front end jsp interface:

<%@ page language="java" contentType="text/html; charset=UTF-8"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<form action="login">
    <input type="text" name="username">
    <input type="text" name="pwd">
    <input type="hidden" name="method" value="login"><!-- Backend basis value Value finding method of -->

Now let's run the project and have a look



















OK, this is the end of the tutorial. Brother Tiezhu will continue to bring you new code. Thank you for watching~

If you think it's useful, remember to like it.

Posted by RSmiroldo on Wed, 04 Dec 2019 16:23:11 -0800