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

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

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


一、 out 对象

(1)


(2) 实例代码

<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
</head>

<body>
	<h1>JSP 内置对象</h1>
	<%
		out.println("<h2>唐诗</h2>");
		out.println("窗前明月光<br>");
		out.println("疑似地上霜<br>");
		out.flush();
		//out.clear(); // 这里会抛出异常
		out.clearBuffer(); //这里不会抛出异常
		out.println("举头望明月<br>");
		out.println("低头思故乡<br>");
	%>
	缓冲区大小:<%=out.getBufferSize()%>byte <br> 
	缓冲区剩余大小:	<%=out.getRemaining()%>	byte <br> 
	是否自动清空缓冲区:<%=out.isAutoFlush()%>	<br>
</body>
</html>


二、 get与post的区别

(1)

(2) 示例代码


<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'login.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>

<body>
	<h1>登录</h1>
	<form action="dologin.jsp" name"loginForm" method="post"> 
		<table>
			<tr>
				<td>用户名:</td>
				<td><input type="text" name="username"></td>
			</tr>
			<tr>
				<td>密码:</td>
				<td><input type="password" name="password"></td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" value="登录"></td>
			</tr>
		</table>
	</form>
</body>
</html>


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

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

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


三、 request对象


代码: reg.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'reg.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <h1>用户注册</h1>
	<form action="request.jsp" method="post">
		<table>
			<tr>
				<td>用户名:</td>
				<td><input type="text" name="username"></td>
			</tr>
			<tr>
				<td>爱好:</td>
				<td>
					<input type="checkbox" name="favorite" value="read">读书
					<input type="checkbox" name="favorite" value="music">音乐
					<input type="checkbox" name="favorite" value="movie">电影
					<input type="checkbox" name="favorite" value="sports">运动
				</td>
			</tr>
			<tr>				 
				<td colspan="2"><input type="submit" value="提交"></td>
			</tr>
		</table>
	</form>
	<br>
	<br>
	<a href="request.jsp?username=李四">测试URL传参数</a>
  </body>
</html>

request.jsp

<%@ page language="java" import="java.util.*"
    contentType="text/html; charset=UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'request.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

</head>

<body>
    <h1>request对象</h1>
    <%
        request.setCharacterEncoding("utf-8");//解决中文乱码问题。但无法解决通过URL传递的中文乱码问题
        request.setAttribute("password", "123456");//人为创造这个属性,只是为了试下这个方法,没有其他目的
    %>
    用户名:<%=request.getParameter("username") %><br>
    爱好: <%
            if(request.getParameterValues("favorite") != null){
                String[] favorite = request.getParameterValues("favorite");
                for(int i=0; i<favorite.length; i++){
                    out.println(favorite[i]+"&nbsp;&nbsp;");                
                }    
            }
        %><br>
    密码: <%=request.getAttribute("password") %><br>
    请求体的MIME类型:<%= request.getContentType() %><br>
    协议类型和版本号:<%= request.getProtocol() %><br>
    服务器主机名:<%= request.getServerName() %><br>
    服务器端口号:<%= request.getServerPort() %><br>
    请求文件的长度: <%= request.getContentLength() %><br>
    请求客户端的IP地址:<%= request.getRemoteAddr() %><br>
    请求的真实路径: <%= request.getRealPath("request.jsp") %><br>
    请求的上下文路径:<%= request.getContextPath() %> <br>
</body>
</html>



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

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

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



 四、 response对象


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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值