[关闭]
@nalan90 2018-06-29T11:16:39.000000Z 字数 8190 阅读 533

二、JSP内置对象

JAVAEE开发


JSP内置对象

内置对象简介
  1. <%
  2. int[] value = {60, 70, 80};
  3. for (int i: value) {
  4. out.println(i);
  5. }
  6. %>

JSP内置对象

out对象
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>out对象</title>
  5. </head>
  6. <body>
  7. <%
  8. out.println("<h2>静夜思</h2>");
  9. out.println("床前明月光<br>");
  10. out.println("疑是地上霜<br>");
  11. out.flush();
  12. //out.clear(); //会抛出异常
  13. out.println("举名望明月<br>");
  14. out.println("低头思故乡<br>");
  15. %>
  16. buffer size: <%=out.getBufferSize()%> bytes<br>
  17. remain buffer size: <%=out.getRemaining()%> bytes<br>
  18. auto clear buffer: <%=out.isAutoFlush()%><br>
  19. </body>
  20. </html>

request对象
  1. ## reg.jsp
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <html>
  4. <head>
  5. <title>注册</title>
  6. </head>
  7. <body>
  8. <h1>用户登录</h1>
  9. <hr>
  10. <form action="request.jsp" name="loginForm" method="post">
  11. 用户名: <input type="text" name="username"><br>
  12. 爱好: <input type="checkbox" name="fav" value="read">读书&nbsp;&nbsp;
  13. <input type="checkbox" name="fav" value="music">音乐&nbsp;&nbsp;
  14. <input type="checkbox" name="fav" value="movie">电影&nbsp;&nbsp;
  15. <input type="checkbox" name="fav" value="swimming">游泳
  16. <br>
  17. <input type="submit" value="login">
  18. </form>
  19. <br>
  20. <br>
  21. <a href="request.jsp?username=张爽">测试URL传参</a>
  22. </body>
  23. </html>
  24. ##request.jsp
  25. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  26. <html>
  27. <head>
  28. <title>注册主页</title>
  29. </head>
  30. <body>
  31. <h1>request内置对象</h1>
  32. <%
  33. //解决中文乱码问题,无法解决url传递的中文乱码
  34. request.setCharacterEncoding("utf-8");
  35. request.setAttribute("password", "123456");
  36. %>
  37. 用户名: <%=request.getParameter("username")%>
  38. <br>
  39. 爱好:
  40. <%
  41. if (request.getParameterValues("fav") != null) {
  42. String[] favs = request.getParameterValues("fav");
  43. for (String fav: favs) {
  44. out.println(fav+"&nbsp;&nbsp;");
  45. }
  46. }
  47. %>
  48. <br>
  49. <%=request.getContextPath()%><br>
  50. <%=request.getPathInfo()%><br>
  51. <%=request.getServletPath()%><br>
  52. <%=request.getContentType()%><br>
  53. <%=request.getPathInfo()%><br>
  54. <%=request.getProtocol()%><br>
  55. <%=request.getServerName()%><br>
  56. <%=request.getServerPort()%><br>
  57. <%=request.getRemoteAddr()%><br>
  58. <%=request.getAttribute("password")%>
  59. </body>
  60. </html>

注意:解决URL传递中文乱码问题如下:

  1. 1、编辑tomcat conf/server.xml文件,添加 URIEncoding="utf-8"
  2. <Connector port="8080" protocol="HTTP/1.1"
  3. connectionTimeout="20000"
  4. redirectPort="8443" URIEncoding="utf-8"/>
  5. 2、重启tomcat

response对象
  1. <%@ page contentType="text/html;charset=UTF-8" import="java.io.*" language="java" %>
  2. <%
  3. response.setContentType("text/html;charset=utf-8");
  4. out.println("<h1>response内置对象</h1>");
  5. out.println("<hr>");
  6. //out.flush();
  7. PrintWriter pw = response.getWriter();
  8. pw.println("大家好,我是response对象生成的输出流out对象");
  9. //response.sendRedirect("reg.jsp");
  10. //response.sendRedirect("request.jsp"); //请求重定向
  11. request.getRequestDispatcher("request.jsp").forward(request, response); //请求转发
  12. %>

session对象
  1. ##session.pageone.jsp
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <%@ page import="java.text.SimpleDateFormat" %>
  4. <%@ page import="java.util.Date" %>
  5. <html>
  6. <head>
  7. <title>session.pageone.jsp</title>
  8. </head>
  9. <body>
  10. <h1>session对象</h1>
  11. <%
  12. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  13. Date d = new Date(session.getCreationTime());
  14. session.setAttribute("username", "zhangshuang");
  15. session.setAttribute("password", "123456");
  16. session.setAttribute("age", "28");
  17. //session.setMaxInactiveInterval(10);
  18. %>
  19. session创建时间: <%=sdf.format(d)%><br>
  20. sessionID编号: <%=session.getId()%><br>
  21. session中获取用户名: <%=session.getAttribute("username")%><br>
  22. <%
  23. //session.invalidate(); //销毁session
  24. %>
  25. <a href="session.pagetwo.jsp" type="_blank">跳转到session.pagetwo.jsp</a>
  26. </body>
  27. </html>
  28. ##session.pagetwo.jsp
  29. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  30. <html>
  31. <head>
  32. <title>session.pagetwo.jsp</title>
  33. </head>
  34. <body>
  35. sessionID编号: <%=session.getId()%><br>
  36. session中获取用户名: <%=session.getAttribute("username")%><br>
  37. </body>
  38. </html>

application对象
  1. <%@ page import="java.util.Enumeration" %>
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <html>
  4. <head>
  5. <title>application对象</title>
  6. </head>
  7. <body>
  8. <%
  9. application.setAttribute("city", "北京");
  10. application.setAttribute("postcode", "100000");
  11. application.setAttribute("email","admin@admin.com");
  12. %>
  13. 城市:<%=application.getAttribute("city")%><br>
  14. application所有属性名称:
  15. <%
  16. Enumeration attributes = application.getAttributeNames();
  17. while (attributes.hasMoreElements()) {
  18. out.println(attributes.nextElement()+"<br>");
  19. }
  20. %>
  21. <br>
  22. JSP引擎信息<%=application.getServerInfo()%>
  23. </body>
  24. </html>

综合案例
  1. ## login.jsp
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <html>
  4. <head>
  5. <title>登录页面</title>
  6. </head>
  7. <body>
  8. <h1>用户登录</h1>
  9. <hr>
  10. <form action="dologin.jsp" name="loginForm" method="post">
  11. 用户名: <input type="text" name="username"><br>
  12. 密码:<input type="password" name="password"><br>
  13. <input type="submit" value="login">
  14. </form>
  15. </body>
  16. </html>
  17. ## dologin.jsp
  18. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  19. <%
  20. request.setCharacterEncoding("utf-8");
  21. String username = "";
  22. String password = "";
  23. username = request.getParameter("username");
  24. password = request.getParameter("password");
  25. if ("admin".equals(username) && "admin".equals(password)) {
  26. session.setAttribute("loginUser", username);
  27. request.getRequestDispatcher("login_success.jsp").forward(request, response);
  28. } else {
  29. response.sendRedirect("login_fail.jsp");
  30. }
  31. %>
  32. ##login_success.jsp
  33. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  34. <html>
  35. <head>
  36. <title>登录成功</title>
  37. </head>
  38. <body>
  39. <%
  40. String loginUser = "";
  41. if (session.getAttribute("loginUser") != null) {
  42. loginUser = session.getAttribute("loginUser").toString();
  43. }
  44. %>
  45. 欢迎您<%=loginUser%>
  46. </body>
  47. </html>
  48. ##login_fail.jsp
  49. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  50. <html>
  51. <head>
  52. <title>登录失败</title>
  53. </head>
  54. <body>
  55. <h3>登录失败</h3>
  56. <a href="login.jsp">重新登录</a>
  57. </body>
  58. </html>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注