JSP基础——内置对象(1)

最近在学习慕课网的课程《Java遇见HTML——JSP篇》,简单做些记录。

课程网址为:http://www.imooc.com/learn/166

 

一、 out 对象

(1) 

 

(2) 实例代码

 

[html] view plain copy
 
  1. <%@ page language="java" import="java.util.*"  
  2.     contentType="text/html; charset=UTF-8"%>  
  3. <%  
  4.     String path = request.getContextPath();  
  5.     String basePath = request.getScheme() + "://"  
  6.             + request.getServerName() + ":" + request.getServerPort()  
  7.             + path + "/";  
  8. %>  
  9.   
  10. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  11. <html>  
  12. <head>  
  13. <base href="<%=basePath%>">  
  14.   
  15. <title>My JSP 'index.jsp' starting page</title>  
  16. <meta http-equiv="pragma" content="no-cache">  
  17. <meta http-equiv="cache-control" content="no-cache">  
  18. <meta http-equiv="expires" content="0">  
  19. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  20. <meta http-equiv="description" content="This is my page">  
  21. <!-- 
  22.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  23.     -->  
  24. </head>  
  25.   
  26. <body>  
  27.     <h1>JSP 内置对象</h1>  
  28.     <%  
  29.         out.println("<h2>唐诗</h2>");  
  30.         out.println("窗前明月光<br>");  
  31.         out.println("疑似地上霜<br>");  
  32.         out.flush();  
  33.         //out.clear(); // 这里会抛出异常  
  34.         out.clearBuffer(); //这里不会抛出异常  
  35.         out.println("举头望明月<br>");  
  36.         out.println("低头思故乡<br>");  
  37.     %>  
  38.     缓冲区大小:<%=out.getBufferSize()%>byte <br>   
  39.     缓冲区剩余大小:    <%=out.getRemaining()%>   byte <br>   
  40.     是否自动清空缓冲区:<%=out.isAutoFlush()%>  <br>  
  41. </body>  
  42. </html>  

 

 

二、 get与post的区别

(1)

(2) 示例代码

 

[html] view plain copy
 
  1. <%@ page language="java" import="java.util.*"  
  2.     contentType="text/html; charset=UTF-8"%>  
  3. <%  
  4.     String path = request.getContextPath();  
  5.     String basePath = request.getScheme() + "://"  
  6.             + request.getServerName() + ":" + request.getServerPort()  
  7.             + path + "/";  
  8. %>  
  9.   
  10. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  11. <html>  
  12. <head>  
  13. <base href="<%=basePath%>">  
  14.   
  15. <title>My JSP 'login.jsp' starting page</title>  
  16.   
  17. <meta http-equiv="pragma" content="no-cache">  
  18. <meta http-equiv="cache-control" content="no-cache">  
  19. <meta http-equiv="expires" content="0">  
  20. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  21. <meta http-equiv="description" content="This is my page">  
  22. <!-- 
  23.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  24.     -->  
  25.   
  26. </head>  
  27.   
  28. <body>  
  29.     <h1>登录</h1>  
  30.     <form action="dologin.jsp" name"loginForm" method="post">   
  31.         <table>  
  32.             <tr>  
  33.                 <td>用户名:</td>  
  34.                 <td><input type="text" name="username"></td>  
  35.             </tr>  
  36.             <tr>  
  37.                 <td>密码:</td>  
  38.                 <td><input type="password" name="password"></td>  
  39.             </tr>  
  40.             <tr>  
  41.                 <td colspan="2"><input type="submit" value="登录"></td>  
  42.             </tr>  
  43.         </table>  
  44.     </form>  
  45. </body>  
  46. </html>  

 

另外再新建一个dologin.jsp页面,不做任何操作,显示“登录成功”即可。

运行后发现,如果method的方法为get,则页面跳转后,所输入的用户名和密码会显示在地址栏中。

但是如果method是post则不会把用户名和密码显示出来,所以post方法更安全。

 

三、 request对象

 

代码: reg.jsp

 

[html] view plain copy
 
  1. <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'reg.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.   <h1>用户注册</h1>  
  27.     <form action="request.jsp" method="post">  
  28.         <table>  
  29.             <tr>  
  30.                 <td>用户名:</td>  
  31.                 <td><input type="text" name="username"></td>  
  32.             </tr>  
  33.             <tr>  
  34.                 <td>爱好:</td>  
  35.                 <td>  
  36.                     <input type="checkbox" name="favorite" value="read">读书  
  37.                     <input type="checkbox" name="favorite" value="music">音乐  
  38.                     <input type="checkbox" name="favorite" value="movie">电影  
  39.                     <input type="checkbox" name="favorite" value="sports">运动  
  40.                 </td>  
  41.             </tr>  
  42.             <tr>                 
  43.                 <td colspan="2"><input type="submit" value="提交"></td>  
  44.             </tr>  
  45.         </table>  
  46.     </form>  
  47.     <br>  
  48.     <br>  
  49.     <href="request.jsp?username=李四">测试URL传参数</a>  
  50.   </body>  
  51. </html>  

request.jsp

 

 

[html] view plain copy
 
  1. <%@ page language="java" import="java.util.*"  
  2.     contentType="text/html; charset=UTF-8"%>  
  3. <%  
  4.     String path = request.getContextPath();  
  5.     String basePath = request.getScheme() + "://"  
  6.             + request.getServerName() + ":" + request.getServerPort()  
  7.             + path + "/";  
  8. %>  
  9.   
  10. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  11. <html>  
  12. <head>  
  13. <base href="<%=basePath%>">  
  14.   
  15. <title>My JSP 'request.jsp' starting page</title>  
  16.   
  17. <meta http-equiv="pragma" content="no-cache">  
  18. <meta http-equiv="cache-control" content="no-cache">  
  19. <meta http-equiv="expires" content="0">  
  20. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  21. <meta http-equiv="description" content="This is my page">  
  22. <!--  
  23.     <link rel="stylesheet" type="text/css" href="styles.css">  
  24.     -->  
  25.   
  26. </head>  
  27.   
  28. <body>  
  29.     <h1>request对象</h1>  
  30.     <%  
  31.         request.setCharacterEncoding("utf-8");//解决中文乱码问题。但无法解决通过URL传递的中文乱码问题  
  32.         request.setAttribute("password", "123456");//人为创造这个属性,只是为了试下这个方法,没有其他目的  
  33.     %>  
  34.     用户名:<%=request.getParameter("username") %><br>  
  35.     爱好: <%  
  36.             if(request.getParameterValues("favorite") != null){  
  37.                 String[] favorite = request.getParameterValues("favorite");  
  38.                 for(int i=0; i<favorite.length; i++){  
  39.                     out.println(favorite[i]+"&nbsp;&nbsp;");                  
  40.                 }      
  41.             }  
  42.         %><br>  
  43.     密码: <%=request.getAttribute("password") %><br>  
  44.     请求体的MIME类型:<%= request.getContentType() %><br>  
  45.     协议类型和版本号:<%= request.getProtocol() %><br>  
  46.     服务器主机名:<%= request.getServerName() %><br>  
  47.     服务器端口号:<%= request.getServerPort() %><br>  
  48.     请求文件的长度: <%= request.getContentLength() %><br>  
  49.     请求客户端的IP地址:<%= request.getRemoteAddr() %><br>  
  50.     请求的真实路径: <%= request.getRealPath("request.jsp") %><br>  
  51.     请求的上下文路径:<%= request.getContextPath() %<br>  
  52. </body>  
  53. </html>  

注意:如果通过URL传参的方式来传递中文参数,那么接收界面会出现乱码,如何解决这个问题?

 

找到Tomcat文件夹下的conf文件夹,打开server.xml

找到connector 那一段代码,加入 URIEncoding = "utf-8",就能解决URL传中文乱码的问题了。

 

 四、 response对象

 

五、 重定向与转发的区别(笔试面试中常考)

 

 
 

posted on 2017-06-21 12:15  alex5211314  阅读(162)  评论(0编辑  收藏  举报

导航