黑马程序员_JAVA 学习笔记17 WEB篇4
2014-02-19 13:02
411 查看
----------------------
ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net
Application对象:
<body>
<%
if(application.getAttribute("count") == null)
{
application.setAttribute("count", "1");
}
else
{
String co = application.getAttribute("count").toString();
Integer cou = Integer.parseInt(co);
cou++;
//String c = cou.toString();
application.setAttribute("count",cou);
}
%>
<%=application.getAttribute("count") %>
</body>
其中上面要注意你传入的是什么 对象要强制转化为什么对象。
比如下面我传入的是一个int 值:
<body>
<%
if(application.getAttribute("count") == null)
{
application.setAttribute("count", 1);
}
else
{
int cou = (Integer)application.getAttribute("count");
cou++;
4000
application.setAttribute("count",cou);
}
%>
<%=application.getAttribute("count") %>
</body>
关于这个.setAttribute(String,Object);方法,前面接收的是对象的名字,你是通过这个名字来获取后面Object的值的,而不是String的值传入Object;
application:存活范围最大的对象,只要服务器没有关闭,application对象中的数据就会一直存在。在整个服务器运行过程中,application对象只有一个。
下面是forward:
这是一个servlet程序:
public class Forward extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String username = req.getParameter("username");
RequestDispatcher rd = req.getRequestDispatcher("oneday/requestForward.jsp");
req.setAttribute("user", username);
rd.forward(req, resp);
}
}
RequestDispatcher rd = req.getRequestDispatcher("oneday/requestForward.jsp");
其中这个是请求转发的这个是同一个请求。注意上面程序 的路径不要弄错。
<body>
<form action="Forward">
<input type = "text" name = "username">
<input type = "submit" value = "dengLu">
</form>
</body>
<body>
requestForward:<br>
<%=request.getAttribute("user") %>
</body>
可以用.setAttribute(“list”,List);传递数组对象。
这个是获得这个文件的所在的物理路径。
关于session的小例子:
<body>
<form action = "oneday/sessionYule2.jsp">
<input type = "text" name = "username">
<input type = "submit" value = "dengLu">
</form>
</body>
<body>
<%
String username = request.getParameter("username");
session.setAttribute("name", username);
%>
<form action="oneday/sessionYule3.jsp">
<input type = "text" name = "music">
<input type = "submit" value = "dengLu">
</form>
</body>
<body>
<%
String music = request.getParameter("music");
String name = (String)session.getAttribute("name");
%>
姓名:<%=name %><br>
娱乐:<%=music %>
</body>
结果:
姓名:Myongyun
娱乐:lanQiu
下面是使用hidden来实现上面的结果:
<body>
<form action = "oneday/sessionYule2.jsp">
<input type = "text" name = "username">
<input type = "submit" value = "dengLu">
</form>
</body>
<body>
<%
String username = request.getParameter("username");
%>
<form action="oneday/sessionYule3.jsp">
<input type = "text" name = "music">
<input type = "hidden" value = "<%=username%>" name = "hidden">
<input type = "submit" value = "dengLu">
</form>
</body>
<body>
<%
String music = request.getParameter("music");
String hidden = request.getParameter("hidden");
%>
姓名:<%=hidden %><br>
娱乐:<%=music %><br>
</body>
可以实现与上面相同 的结果。
对于 session来说能不用就不用。
Session太多会占用服务器的资源,用session的时候是登录的时候,其它时候能不用就不要用session。
ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net
Application对象:
<body>
<%
if(application.getAttribute("count") == null)
{
application.setAttribute("count", "1");
}
else
{
String co = application.getAttribute("count").toString();
Integer cou = Integer.parseInt(co);
cou++;
//String c = cou.toString();
application.setAttribute("count",cou);
}
%>
<%=application.getAttribute("count") %>
</body>
其中上面要注意你传入的是什么 对象要强制转化为什么对象。
比如下面我传入的是一个int 值:
<body>
<%
if(application.getAttribute("count") == null)
{
application.setAttribute("count", 1);
}
else
{
int cou = (Integer)application.getAttribute("count");
cou++;
4000
application.setAttribute("count",cou);
}
%>
<%=application.getAttribute("count") %>
</body>
关于这个.setAttribute(String,Object);方法,前面接收的是对象的名字,你是通过这个名字来获取后面Object的值的,而不是String的值传入Object;
application:存活范围最大的对象,只要服务器没有关闭,application对象中的数据就会一直存在。在整个服务器运行过程中,application对象只有一个。
下面是forward:
这是一个servlet程序:
public class Forward extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String username = req.getParameter("username");
RequestDispatcher rd = req.getRequestDispatcher("oneday/requestForward.jsp");
req.setAttribute("user", username);
rd.forward(req, resp);
}
}
RequestDispatcher rd = req.getRequestDispatcher("oneday/requestForward.jsp");
其中这个是请求转发的这个是同一个请求。注意上面程序 的路径不要弄错。
<body>
<form action="Forward">
<input type = "text" name = "username">
<input type = "submit" value = "dengLu">
</form>
</body>
<body>
requestForward:<br>
<%=request.getAttribute("user") %>
</body>
可以用.setAttribute(“list”,List);传递数组对象。
这个是获得这个文件的所在的物理路径。
关于session的小例子:
<body>
<form action = "oneday/sessionYule2.jsp">
<input type = "text" name = "username">
<input type = "submit" value = "dengLu">
</form>
</body>
<body>
<%
String username = request.getParameter("username");
session.setAttribute("name", username);
%>
<form action="oneday/sessionYule3.jsp">
<input type = "text" name = "music">
<input type = "submit" value = "dengLu">
</form>
</body>
<body>
<%
String music = request.getParameter("music");
String name = (String)session.getAttribute("name");
%>
姓名:<%=name %><br>
娱乐:<%=music %>
</body>
结果:
姓名:Myongyun
娱乐:lanQiu
下面是使用hidden来实现上面的结果:
<body>
<form action = "oneday/sessionYule2.jsp">
<input type = "text" name = "username">
<input type = "submit" value = "dengLu">
</form>
</body>
<body>
<%
String username = request.getParameter("username");
%>
<form action="oneday/sessionYule3.jsp">
<input type = "text" name = "music">
<input type = "hidden" value = "<%=username%>" name = "hidden">
<input type = "submit" value = "dengLu">
</form>
</body>
<body>
<%
String music = request.getParameter("music");
String hidden = request.getParameter("hidden");
%>
姓名:<%=hidden %><br>
娱乐:<%=music %><br>
</body>
可以实现与上面相同 的结果。
对于 session来说能不用就不用。
Session太多会占用服务器的资源,用session的时候是登录的时候,其它时候能不用就不要用session。
相关文章推荐