对jsp的一个小结(2)session、cookie、application

4实现数据保存

1.使用session保存用户名

if(username.equals("admin")){
	//不允许注册,注册失败
	request.setAttribute("mess", "注册失败,请更换其他用户名");
	request.getRequestDispatcher("userCreate.jsp").forward(request, response);
}else{
	//允许注册,注册成功
	session.setAttribute("user", username);
	response.sendRedirect("index.jsp");
}


  <%
            Object o=session.getAttribute("user");
            if(o==null){
            	//显示用户名密码,可以在此登录
            %>            	
            	<label>用户名</label><input type="text" name="uname" /><label>密码</label><input type="text" name="upassword" /><button>登录</button>
            <%
            }else{
            	//显示“欢迎你,XXX”
            	out.print("欢迎你,"+o.toString());
            }
            %>


2.会话清除与过期

if(username.equals("admin")){
	//不允许注册,注册失败
	request.setAttribute("mess", "注册失败,请更换其他用户名");
	request.getRequestDispatcher("userCreate.jsp").forward(request, response);
}else{
	//允许注册,注册成功
	session.setAttribute("user", username);
	//session.setMaxInactiveInterval(10);
	response.sendRedirect("index.jsp");
}

<%
//实现注销
//session.removeAttribute("user");
session.invalidate();
response.sendRedirect("index.jsp");
%>

	<session-config>
		<session-timeout>10</session-timeout>
	</session-config>
</web-app>



3使用cookie自动填写用户名

if(username.equals("admin")){
	//不允许注册,注册失败
	request.setAttribute("mess", "注册失败,请更换其他用户名");
	request.getRequestDispatcher("userCreate.jsp").forward(request, response);
}else{
	//允许注册,注册成功
	Cookie cookie=new Cookie("user",username);
	cookie.setMaxAge(60*60);
	response.addCookie(cookie);
	session.setAttribute("user", username);
	//session.setMaxInactiveInterval(10);
	response.sendRedirect("index.jsp");
}

 <%
            Cookie[] cookies=request.getCookies();
            String user="";
            for(int i=0;i<cookies.length;i++){
            	if(cookies[i].getName().equals("user")){
            		user=cookies[i].getValue();
            	}
            }
            Object o=session.getAttribute("user");
            if(o==null){
            	//显示用户名密码,可以在此登录
            %>        


4application实现计数器

 <%
    Object count=application.getAttribute("count");
    if(count==null){
    	//application中未存放count
    	application.setAttribute("count", new Integer(1));
    }else{
    	//application中已经存放count
    	Integer i=(Integer)count;
    	application.setAttribute("count", i.intValue()+1);
    }
    Integer icount=(Integer)application.getAttribute("count");
    out.println("页面被访问了"+icount.intValue()+"次");
    %>
5jsp总结

1.客户端请求新页面一(超链接和js请求,get方式传参)

<script type="text/javascript">
function fun(){
	var uid = document.getElementById("uid").value;
	if(uid == ""){
		alert("请输入数据");
		return false;
	}else{
		return true;
	}
}

	<!-- 链接到page2 -->
	<a href="page2.jsp">链接到page2</a><br>
	<!-- 链接到page2,弹出新窗口 -->
	<a href="page2.jsp" target="_blank">链接到page2</a><br>
	<!-- 相对路径链接到page2 -->
	<a href="./page2.jsp">链接到page2</a><br>
	<a href="../demo1/page2.jsp">链接到page2</a><br>
	<!-- 绝对路径链接到page2 -->
	<a href="http://localhost:8080/web1/demo1/page2.jsp">链接到page2</a><br>
	<a href="<%=request.getContextPath() %>/demo1/page2.jsp">链接到page2</a><br>
	
	<!-- 链接到page2,并传参 -->
	<a href="page2.jsp?uid=admin">链接到page2</a><br>
	<!-- 使用js链接到page2 -->
	<a href='javascript:window.location="page2.jsp";'>链接到page2</a><br>
	<!-- 使用js链接到page2,并传参 -->
	<a href='javascript:window.location="page2.jsp?uid=admin";'>链接到page2</a><br>
	
	<!-- 使用js链接到page2,弹出新窗口,并传参 -->
	<a href='javascript:window.open("page2.jsp?uid=admin");'>链接到page2</a><br>
	<!-- submit提交表单 -->
	<form action="page2.jsp" method="post">
		<input type="text" name="uid">
		<input type="submit" value="提交">
	</form>
	<!-- 验证后提交表单 -->
	<form action="page2.jsp" method="post">
		<input type="text" name="uid" id="uid">
		<input type="submit" value="验证后提交表单" onClick="return fun()">
	</form>
	<!-- url方式提交数据 -->
	<form action="page2.jsp?uid=admin" method="post">
		<input type="text" name="uid">
		<input type="submit" value="提交">
	</form>
	<!-- hidden方式提交数据 -->
	<form action="page2.jsp" method="post">
		<input type="hidden" name="uid2" id="uid2" value="admin">
		<input type="submit" value="提交">
		<script type="text/javascript">
			document.getElementById("uid2").value = "admin22222";
		</script>
	</form>
	<!-- button+js提交表单 -->
	<form id="form1" action="page2.jsp" method="post">
		<input type="text" name="uid">
		<input type="button" value="提交" οnclick='document.getElementById("form1").submit();'>
	</form>
	<!-- <a>+js提交表单 -->
	<form id="form2" action="page2.jsp" method="post">
		<input type="text" name="uid">
		<a href='javascript:document.getElementById("form2").submit();'>提交表单</a>
	</form>


2.客户端请求新页面二
3.中文乱码解决方法

cookie中文转码解码

	<%
 	Cookie c = new Cookie("mess",URLEncoder.encode("中国") );
	response.addCookie(c); 
	%>

	<%
	request.setCharacterEncoding("utf-8");
	String name = request.getParameter("name");
	//name = new String(name.getBytes("ISO-8859-1"),"utf-8");
	out.print("get方式提交的name的值"+name+"<br>");
	String uid = request.getParameter("uid");
	//uid = new String(uid.getBytes("ISO-8859-1"),"utf-8");
	out.print("post方式提交的uid的值"+uid+"<br>");
	%>
	<%
 	Cookie[] cookies = request.getCookies();
	if(cookies != null){
		for(Cookie c : cookies){
			if(c.getName().equals("mess")){
				out.print("Cookie的值为"+URLDecoder.decode(c.getValue()) );
			}
		}
	} 
	%>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答这个问题。首先需要了解这些变量的作用以及如何在Java设置它们的值。 1. Page变量:它表示当前页面的对象,可以在JSP页面通过pageContext对象来获取。在JSP页面设置page变量的值,可以使用JSP脚本语言或JSP自定义标签库。 例如,使用JSP脚本语言设置page变量的值: ``` <% pageContext.setAttribute("pageVariable", "This is a page variable."); %> ``` 2. Request变量:它表示当前请求的对象,可以在JSP页面通过request对象来获取。在JSP页面设置request变量的值,可以使用JSP脚本语言或JSP自定义标签库。 例如,使用JSP脚本语言设置request变量的值: ``` <% request.setAttribute("requestVariable", "This is a request variable."); %> ``` 3. Session变量:它表示当前会话的对象,可以在JSP页面通过session对象来获取。在JSP页面设置session变量的值,可以使用JSP脚本语言或JSP自定义标签库。 例如,使用JSP脚本语言设置session变量的值: ``` <% session.setAttribute("sessionVariable", "This is a session variable."); %> ``` 4. Application变量:它表示当前应用程序的对象,可以在JSP页面通过application对象来获取。在JSP页面设置application变量的值,可以使用JSP脚本语言或JSP自定义标签库。 例如,使用JSP脚本语言设置application变量的值: ``` <% application.setAttribute("applicationVariable", "This is an application variable."); %> ``` 5. Cookie变量:它表示当前请求的Cookie对象,可以在JSP页面通过request对象来获取。在JSP页面设置cookie变量的值,可以使用JSP脚本语言或JSP自定义标签库。 例如,使用JSP脚本语言设置cookie变量的值: ``` <% Cookie cookie = new Cookie("cookieVariable", "This is a cookie variable."); response.addCookie(cookie); %> ``` 在以上的代码,我们通过response对象来添加一个名为cookieVariable值为"This is a cookie variable."的Cookie变量。 接下来,我们可以使用EL表达式在页面输出这些变量的值。 例如,在JSP页面输出page、request、sessionapplication以及cookie变量的值: ``` Page Variable: ${pageContext.pageVariable} Request Variable: ${requestScope.requestVariable} Session Variable: ${sessionScope.sessionVariable} Application Variable: ${applicationScope.applicationVariable} Cookie Variable: ${cookie.cookieVariable.value} ``` 在以上的代码,我们使用EL表达式分别输出了page、request、sessionapplication以及cookie变量的值。注意,cookie变量的值需要通过value属性来获取。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值