Session的使用

1,网上商城中的购物车

2,保存登录用户的信息

3,将某些数据放入到Session中,供同一个用户的各个页面使用

4,防止用户非法登录到某个页面

 

(1)LoginCl.java

package com.dtg.controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dtg.model.User;
import com.dtg.model.UserCl;

public class LoginServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/**
		 * 把loginCl.jsp 页面实现的操作,转移到它本应该所在的位置上,就是控制器Servlet
		 * 
		 */
		String id = request.getParameter("id");
		String passw = request.getParameter("passw");
		System.out.println("这是Servlet控制器!!!非常牛逼");
		int pageNow = 1;//当前页
//		int pageSize = 3;//页大小
		int pageCount = 0;//页数
//		int rowCount = 0;//总记录数
		UserCl userCl = new UserCl();
		if (userCl.checkUser(id, passw)) {
			// 用户合法
			/**
			 * sendRedirect “转向” 效率不过,很多软件公司都不适用这个方法,而是使用转发(重定向)
			 * jsp:forward 服务器请求资源(运送)
			 */
			// response.sendRedirect("login_success.jsp");
			
			/**
			 * 在跳转到login_succss.jsp 之前,先把数据准备好 
			 */
//			把用户信息存入session中
			request.getSession().setAttribute("id", id);
			request.getSession().setAttribute("passw", passw);
			
			ArrayList<User> al = userCl.getUserByPage(pageNow);
			pageCount = userCl.getPageCount();
//			将al和pageCount,pageNow放入request中
			request.setAttribute("result", al);
			request.setAttribute("pageCount", pageCount+"");
			request.setAttribute("pageNow", "1");
			
			request.getRequestDispatcher("login_success.jsp").forward(request,
					response);
		} else {
//			 用户不合法
			request.getRequestDispatcher("login_failure.jsp").forward(request,
					response);
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/**
		 * 合二为一
		 * post方法或者get方法都能搞定
		 */
		this.doGet(request, response);

	}

}

 

(2)login_success.jsp

<h2>
欢迎
<font color="red"><%=(String)session.getAttribute("id")%></font>光临
</h2>

 (3)login_success.jsp

</head>
	<body>
		<%
			String id = (String)session.getAttribute("id");
			if(id==null){
			//返回登陆界面(目前学过的内置对象response,request,out,session)
				response.sendRedirect("/WEB-INF/dtg/login.jsp?err=1");
				return ;
			}
		 %>
		<center>
			<h1> 
				登陆成功 
			</h1>
			<h2>
				欢迎
				<font color="red"><%=id %></font>光临
			</h2>
			<a href="/jsp1/index.jsp">返回重新登录</a>
			<hr>
			<%
				//接受用户想要显示的页面
				String s_pageNow = (String)request.getAttribute("pageNow");
				
				int pageNow = Integer.parseInt(s_pageNow);
			
				//要显示的数据,用request来取
				ArrayList<User> al = (ArrayList<User>)request.getAttribute("result");
				
				//	object -->String -->int
				
				String s_pageCount = (String)request.getAttribute("pageCount");
				int pageCount = Integer.parseInt(s_pageCount);
				
				//用表格显示
				%>
					<table border="1">
							<tr><td>userId</td><td>ID</td><td>PASSWORD</td><td>AGE</td><td>SEX</td></tr>
							<%
								for(int i=0;i<al.size();i++){
									User user = (User)al.get(i);
									%>
										<tr><td><%=user.getUserId() %></td><td><%=user.getId() %></td>
										<td><%=user.getPassword() %></td>
										<td><%=user.getAge() %></td><td><%=user.getSex() %></td></tr>
									<%
								}
							 %>
						</table>
				<%
				//显示超链接
				if(pageNow != 1){
						out.print("<a href=UserServlet?pageNow="+(pageNow-1)+" >上一页</a>");
					}
				
				if(pageNow > 6 && pageNow <= (pageCount-4)){
					for(int i=pageNow-5;i<=pageNow+4;i++){
						out.println("<a href=UserServlet?pageNow="+i+">["+i+"]</a>");
					}
				}else if(pageNow >(pageCount-4) ){
					for(int i=pageCount-9;i<=pageCount;i++){
						out.println("<a href=UserServlet?pageNow="+i+">["+i+"]</a>");
					}
				}else{
					for(int i=1;i<=10;i++){
				//要是pageCount > 10  则不显示全部,只显示前10页的超链接
				//for(int i=1;i<=pageCount;i++)
					out.println("<a href=UserServlet?pageNow="+i+">["+i+"]</a>");
					}
				}
				
				if(pageNow != pageCount){
					out.print("<a href=UserServlet?pageNow="+(pageNow+1)+">下一页</a>");
				}
				
			%>
		</center>

	</body>
</html>

 

(4) login.jsp

	</head>

	<body>
		<center>
		<%
			String err = (String)request.getParameter("err");
			if(err!=null){
				out.println("<font color=red>用户没有正确登陆,请重新登陆!</font>");
			}
		 %>
		<h1>登陆界面</h1>
		<hr>
		<form action="LoginServlet" method="post">
			用户名:
			<input type="text" name="id" />
			<br>
			密&nbsp;&nbsp;码:
			<input type="password" name="passw" />
			<br>
			<input type="submit" value="提交" />
			<input type="reset" value="重置" />
			<hr>
		</form>
		</center>
	</body>
</html>

 

 

你可能感兴趣的:(session)