Servlet实现登录功能

流程

html

登录
<form action="login" >
    用户名:<input name="username" placeholder="请输入用户名"> <br>
    密码:<input name="password" placeholder="请输入密码" type="password"><br>
    <input type="submit" value="登录">
</form>

servlet

import java.io.IOException;

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( urlPatterns="/login")
public class LoginServlet extends HttpServlet{

    /**
     * HttpServletRequest req:封装请求信息(数据),如:封装提交来的用户名、密码
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取提交来的数据
        String uname=req.getParameter("username");
        String pass=req.getParameter("password");

        System.out.println("接收到登录请求!用户名:"+uname+",密码:"+pass);

    }
}

原文出处:http://malaoshi.top/show_1IX2chaNBTDI.html