1, using the config object
1) xml file (web.xml)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <welcome-file-list> <welcome-file>/showConfig.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>showConfig</ The servlet-name > <-! Path jsp file -> < jsp-File > /showConfig.jsp </ jsp-File > <-! Initial Parameter List -> < the init-param > <! - - parameter name - stored value -> < param-name > the param1 </ param-name > < param-value > . 1 </ param-value > </ the init-param > </ the servlet > < the servlet-Mapping > <! - 定义servlet的访问路径--> <servlet-name>showConfig</servlet-name> <url-pattern>/showConfig.jsp</url-pattern> </servlet-mapping> </web-app>
2) jsp files (showConfig.jsp)
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/9/24 Time: 19:28 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>展示config中的初始参数</title> </head > < body > <% - get the initial values of parameters from the name - %> <% = " the param1 apos value = " + config.getInitParameter ( " the param1 " ) %> < br /> <% - Get the servlet name - %> <% = " the servlet name: " + config.getServletName () %> </ body > </ HTML >
2, using the session object
1)jsp 文件(index.jsp、setSession.jsp 和 welcome.jsp)
①index.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/9/24 Time: 19:41 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>提交用户信息</title> </head> <body> <form action="setSession.jsp" method="post"> 用户名:<input type="text" name="username" /> 密码:<input type="password" name="userpwd"/> <input type="submit" value="提交"/> </form> </body> </html>
②setSession.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/9/24 Time: 19:44 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>设置session属性的界面</title> </head> <body> <% String username = request.getParameter("username"); String userpwd = request.getParameter("userpwd"); if (username != null && userpwd != null) { if (userpwd.equals("admin")) { session.setAttribute("username", username); response.sendRedirect("welcome.jsp"); } else { response.sendRedirect("index.jsp"); } } else { response.sendRedirect("index.jsp"); } %> </body> </html>
③welcome.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/9/24 Time: 19:47 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>欢迎登陆</title> </head> <body> <% out.print("欢迎 " + session.getAttribute("username")); out.print(" 登陆!"); %> </body> </html>