例如:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <% Date now = new Date(); %> <h1>现在的时间是:<%= now %></h1> </body> </html>所获取的时间是动态的
out:(JSPWriter):相当于respomse.getWriter()获取的对象,用于在页面中显示信息。 config:(ServletConfig):对应Servlet中的Servletconfig对象 page:(Object)对应当前Servlet对象,实际上就是this pageContext:(PageContext)当前页的上下文,也是一个域对象 exception:(Throwable)错误页中异常对象 request:(HttpServletResquest)HttpServletResquest对象 response:(HttpServletResponse)HttpServletResponse对象 application:(ServletContext)ServletContext对象 session:(HttpSession)HttpSession对象 重点掌握四个域对象
因此,可以对之前的小案例进行改动
package servlet; import bean.User; import dao.UserDAOImpl; import javax.servlet.RequestDispatcher; 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 java.io.IOException; import java.sql.SQLException; /* * 处理用户登录的Servlet * * */ @WebServlet(name = "MyServlet") public class MyServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("调用post"); // 获取用户名密码 String username = request.getParameter("username"); String userpassword = request.getParameter("userpassword"); UserDAOImpl userDAO = new UserDAOImpl(); System.out.println(username); System.out.println(userpassword); // 调用UserDAOImpl中的验证方法 try { boolean b = userDAO.checkUsernameAndPassword(username, userpassword); //用户名和密码正确(建议成功重定向,失败转发) if (b) { response.sendRedirect("pages/successful.jsp"); } else { // 用户名密码不正确(转发到登录页面) request.setAttribute("msg","用户名或密码错误"); RequestDispatcher requestDispatcher = request.getRequestDispatcher("index.jsp"); // 请求转发 requestDispatcher.forward(request,response); } } catch (SQLException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } } <%-- Created by IntelliJ IDEA. User: dell Date: 2020/10/5 Time: 15:49 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登陆界面</title> <style type="text/css"> body{ background-color: cornflowerblue; } </style> </head> <body> <h1>欢迎登陆</h1> <form action="MyServlet" method="post"> 用户名称:<input type="text" name="username"><span style="color:red"><%=request.getAttribute("msg") == null ? " ":request.getAttribute("msg") %></span> <br> 用户密码:<input type="password" name="userpassword"><br> <input type="submit" value="提交"> </form> </body> </html>这样当输入错误时,可以生成一句提示
1.EL是JSP内置的表达式语言,用以访问页面的上下文以及不同作用域中的对象,取得对象属性的值,或执行简单的运算或判断操作,EL在得到某个数据时,会自动进行数据类型的转换 2.EL表达式用于代替JSP表达式(<%=%>)在页面中做输出操作 3.EL表达式仅仅用来读取数据,而不能对数据进行修改 4.使用EL表达式输出数据时,如果有则输出数据,如果为null则什么也不输出 5.EL表达式语法:
${EL表达式(可完成取值、简单判断、简单运算等)}6.EL取值的四个域
<%@ page import="java.util.Date" %><%-- Created by IntelliJ IDEA. User: dell Date: 2020/10/9 Time: 8:53 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>EL</title> </head> <body> <%-- EL全称: Expression Language 表达式语言 作用:主要用来输出域对象中属性值 EL表达式查找规则: 先从page开始查找,找到后直接返回,不再去其他域进行查找,如果找不到再去request域中查找,以此类推... 如果在application域中仍找不到,则返回空串 EL给我们提供了四个Scope对象,用来精确获取指定域中属性值 pageScope 获取page域中属性值 requestScope 获取request域中属性值 sessionScope 获取session域中属性值 applicationScope 获取aoolication域中属性值 --%> <% Date date = new Date(); // 将当前时间放到page域中才能显示 pageContext.setAttribute("time",date); // 获取一个实体类的属性,实际上是调用了它的get方法,但是不需要写get,直接写get方法后的字母(首字母小写) %> 通过EL表达式显示 ${pageScope.time} </body> </html>由此,可以将那个小案例再修改一下 不用三元表达式
<%-- Created by IntelliJ IDEA. User: dell Date: 2020/10/5 Time: 15:49 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登陆界面</title> <style type="text/css"> body{ background-color: cornflowerblue; } </style> </head> <body> <h1>欢迎登陆</h1> <form action="MyServlet" method="post"> 用户名称:<input type="text" name="username"><span style="color:red">${requestScope.msg }</span> <br> 用户密码:<input type="password" name="userpassword"><br> <input type="submit" value="提交"> </form> </body> </html>