第11章 JAVA EE基础学习-session范围概述和JSP标签初步

Session范围理解:


简单代码:

add.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="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 'add.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>
    <h3>加法运算</h3>
    <form action="add">
    	<div>
    		<span>加数1:</span>
    		<input type="text" name="add1" value="1">
    	</div>
    	<div>
    		<span>加数2:</span>
    		<input type="text" name="add2" value="2">
    	</div>
    	<div> 
    		<input type="submit"  value="计算">
    	</div>
    </form>
  </body>
</html>


AddServlet.java

/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//编码设置utf-8
		request.setCharacterEncoding("utf-8");
		//获得并赋值请求参数
		int add1=Integer.parseInt(request.getParameter("add1"));
		int add2=Integer.parseInt(request.getParameter("add2"));
		
		int sum=add1+add2;//定义并赋值sum=add1+add2
		//如果有提供sessionId,该方法就会去尝试获取原来的session,如果没有提供sessionId,该方法就回尝试创建一个新的session
		//如果虽然提供了sessionId,但没有找到对应的空间,则还是自己去创建一个新的session。
		HttpSession session=request.getSession();
		session.setAttribute("add1",add1);
		session.setAttribute("add2",add2);
		session.setAttribute("sum",sum);
		
		System.out.println("addSession ID:"+session.getId());
		System.out.println("addSession is new ?"+session.isNew());
		System.out.println("addSession create time:"+new Date(session.getCreationTime()).toLocaleString());
		System.out.println("addSession last access time:"+new Date(session.getLastAccessedTime()).toLocaleString());
		System.out.println("addSession timeout:"+session.getMaxInactiveInterval());
		
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		out.println("<!Doctype HTML>");
		out.println("<html>");
		out.println("<head>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h3>加法运算:</h3><br>");
		out.println("<form action='"+response.encodeUrl("sub")+"' method=\"post\">");
		out.println("<div>被减数:"+sum+"</div>");
		out.println("<div>减数:<input type=\"text\" name=\"sub1\"></div>");
		out.println("<div><input type=\"submit\" value=\"提交\"></div>");
		out.println("</form>");
		out.println("</body>");
		out.println("</html>");
	}

SubServlet.java

/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//编码设置utf-8
		request.setCharacterEncoding("utf-8");
		//获取请求参数
		int sub1=Integer.parseInt(request.getParameter("sub1"));
		
		//获取session参数
		HttpSession session=request.getSession();
		int sum=(Integer)session.getAttribute("sum");
		int add1=(Integer)session.getAttribute("add1");
		int add2=(Integer)session.getAttribute("add2");
		//求和
		int result=sum-sub1;
		
		System.out.println("subSession ID:"+session.getId());
		System.out.println("subSession is new ?"+session.isNew());
		System.out.println("subSession create time:"+new Date(session.getCreationTime()).toLocaleString());
		System.out.println("subSession last access time:"+new Date(session.getLastAccessedTime()).toLocaleString());
		System.out.println("subSession timeout:"+session.getMaxInactiveInterval());
		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.println(add1+"+"+add2+"-"+sub1+"="+result);
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

Student.java

/**
 * 类名:Studnet	<br>
 * 功能描述:	<br> 
 * 创建日期:2018年5月20日 下午11:45:48	<br>
 * 修改备注:
 * @作者信息:Zhou kailun	<br>
 */
public class Student {
	/**学生学号*/
	private Integer stuNo;
	/**学生姓名*/
	private String stuName;
	public Integer getStuNo() {
		return stuNo;
	}
	public void setStuNo(Integer stuNo) {
		this.stuNo = stuNo;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	
}

index.jsp

 

<%@ page language="java" import="java.util.*,edu.fjnu.domain.*" pageEncoding="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>
    <!--
    	 Session 范围中以stu为key去查找一个Student对象,如果有找到,则在本地使用stu局部变量指向它,
    	 如果没有找到,则创建一个Student对象,在本地使用stu局部变量指向,然后以stu为key保存到session范围
    --> <br>
       <%
       /*
    	Object obj=session.getAttribute("stu");
    	Student stu = null;
    	if(obj!=null)
    	{
			stu=(Student)obj;    		
    	}
    	else
    	{
    		stu=new Student();
    		session.setAttribute("stu", stu);
    	}*/
    %>
    <jsp:useBean id="stu" class="edu.fjnu.domain.Student" scope="session"></jsp:useBean>
    this is my JSP Page<br>
  </body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值