先来看看效果~~
这里为了方便查看,密码框并没有使用password输入框,而是使用了text框
总体来说思路还是比较简单
1.先来个form,配置好action和method,代码如下
1 <form action="login" method="get"> 2 用户名:<input type="text" name="usr"><br/> 3 密码:<input type="text" name="psd"><br/> 4 <input type="submit" value="登录"> 5 </form>
2.然后编写判断逻辑代码--LoginCheckServlet,就几个判断总体来说还是比较简单,主要是注意跳转welcome时传入值进去
public class LoginCheckServlet extends HttpServlet { private final static String USR = "peter"; private final static String PSD = "admin"; public LoginCheckServlet() { super(); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String usr = req.getParameter("usr"); String psd = req.getParameter("psd"); if (null != usr && null != psd) { if (!usr.equals(USR)) { resp.sendRedirect("/web_war_exploded/"); } else if (!psd.equals(PSD)) { resp.sendRedirect("/web_war_exploded/"); } else { System.out.println("登陆成功"); req.setAttribute("usr", usr); req.setAttribute("psd", psd); req.getRequestDispatcher("/welcome.jsp").forward(req, resp); } } } }
3.跳转后的welcome.jsp,用EL表达式将值显示出来就差不多了
<%-- Created by IntelliJ IDEA. User: eddy Date: 19-3-30 Time: 下午4:52 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Welcome</title> <h1> Welcome to this page!! </h1> 用户名: ${usr} <br/> 密码:${psd} </head> <body> </body> </html>
原文:https://www.cnblogs.com/eddy-s/p/10628190.html