标签:spl prot closed coding 客户 存储 exce 本质 amp
一、简述
JSP是一种动态网页技术标准,其本质就是Servlet。
JSP文件在用户第一次请求时会被编译成Servlet,之后再由该Servlet处理用户的请求。
二、基础
<!-- 声明 --> <%! String scheme; String serverName; int serverPort; %> <!-- 代码段 --> <% scheme = request.getScheme(); serverName = request.getServerName(); serverPort = request.getServerPort(); %> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <%=scheme%><br/> <%=serverName%><br/> <%=serverPort%><br/> <%=String.format("%s://%s:%d/", scheme, serverName, serverPort)%> </body> </html>
编译指令用于设计JSP程序的属性以及生成的Servlet的属性。
动作是一组内置标签,用于控制JSP的行为。
//index.jsp <jsp:include page="result.jsp"> <jsp:param name="number" value="100"/> </jsp:include> //result.jsp <% String str = request.getParameter("number"); int number = Integer.parseInt(str); int result = 0; while (number != 0) { result += number; number--; } out.print(result); %>
JSP页面在执行时会出现两类异常:
三、内置对象
内置对象简述:
//index.jsp <form action="session.jsp" method="post"> <table align="center"> <tr> <td align="center">用户:</td> <td align="left"> <input type="text" name="user"/> </td> </tr> <tr> <td align="center">密码:</td> <td align="left"> <input type="password" name="pass"/> </td> </tr> <tr> <td align="center"> <input type="submit" value="登陆"/> </td> </tr> </table> </form> //session.jsp <% session.setMaxInactiveInterval(60);//最大有效时间 String user = request.getParameter("user"); String pass = request.getParameter("pass"); user = new String(user.getBytes("ISO-8859-1"), "UTF-8");//防止中文乱码 pass = new String(pass.getBytes("ISO-8859-1"), "UTF-8"); session.setAttribute("user", user); session.setAttribute("pass", pass); response.sendRedirect("message.jsp"); %> //message.jsp <% out.println("Session Id:" + session.getId()); out.println("UserName:" + session.getAttribute("user")); out.println("Password:" + session.getAttribute("pass")); %>
Http是一种无状态协议,只负责请求与响应而不对客户端进行区分。Session跟踪用于区分客户端,主要的方式如下
Cookie使用示例:
//index.jsp <% String user = ""; String pass = ""; Cookie[] cookies = request.getCookies(); for (int i = 0; cookies != null && i < cookies.length; i++) { if (cookies[i].getName().equals("testCookie")) { String[] strings = cookies[i].getValue().split("@"); user = strings[0]; pass = strings[1]; } } if (user.isEmpty() || pass.isEmpty()) out.print("Welcome guest"); else out.print("Welcome " + user); %> //cookie.jsp <% String user = request.getParameter("user"); String pass = request.getParameter("pass"); user = new String(user.getBytes("ISO-8859-1"), "UTF-8"); pass = new String(pass.getBytes("ISO-8859-1"), "UTF-8"); Cookie cookie = new Cookie("testCookie", String.format("%s@%s", user, pass)); response.addCookie(cookie); %>
HttpSession使用示例:
//ServletSession public class ServletSession extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session = req.getSession(); resp.setContentType("text/html;charset=UTF-8"); PrintWriter out = resp.getWriter(); String message; int accessCount = 0; if (session.isNew()) { message = "Welcome here!"; } else { message = "Welcome back!"; Integer oldAccessCount = (Integer) session.getAttribute("accessCount"); if (oldAccessCount != null) accessCount = oldAccessCount + 1; } session.setAttribute("accessCount", accessCount); String content = "<!DOCTYPE html>" + "<html>" + "<head>" + "<title>servlet session</title>" + "</head>" + "<body>" + message + "<br/>" + "会话编号:" + session.getId() + "<br/>" + "创建时间:" + session.getCreationTime() + "<br/>" + "最后访问:" + session.getLastAccessedTime() + "<br/>" + "</body>" + "</html>"; out.println(content); out.flush(); out.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } //web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> <servlet-name>ServletSession</servlet-name> <servlet-class>ServletSession</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletSession</servlet-name> <url-pattern>/ServletSession</url-pattern> </servlet-mapping> </web-app>
标签:spl prot closed coding 客户 存储 exce 本质 amp
原文地址:https://www.cnblogs.com/arseneyao/p/9319878.html