登录诸如当当网之类的购物网站,未登录前可以先将产品放入购物车,当点击结账的时候会先跳出登录页面,登录后,继续跳至结账页面。一直想写一个类似的demo,终于昨天写了一个,记录下来。
先看代码吧,看完再讲代码。
代码块
后台
包含两个servlet,一个domain对象。
CheckLogin.java
package com.login; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class CheckLogin */ @WebServlet("/res/viaLogin") public class CheckLogin extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public CheckLogin() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); User user = (User)session.getAttribute("user"); String url = request.getParameter("url"); if (!"".equals(url)) { session.setAttribute("url", url); } if (null != user){ request.getRequestDispatcher("myOrder.jsp").forward(request, response); } else { request.getRequestDispatcher("login.jsp").forward(request, response); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
LoginServlet.java
package com.login; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class LoginServlet */ @WebServlet("/res/login") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); User user = new User(); session.setAttribute("user", user); String url = (String) session.getAttribute("url"); if (null == url || "".equals(url)) { request.getRequestDispatcher("main.jsp").forward(request, response); } else { request.getRequestDispatcher(url).forward(request, response); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
User.java
package com.login; public class User { }
前台页面
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Register</title> </head> <body> <center> <form action="login"> <table> <caption>登陆</caption> <tr> <td>username</td><td><input name="username" /></td> </tr> <tr> <td>password</td><td><input name="password" type="password"/></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Submit" /></td> </tr> </table> </form> </center> </body> </html>
main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Main</title> </head> <body> <center> This is main jsp <br><br> <a href="#" onclick="myOrder(this)">我的订单</a><br><br> <a href="login.jsp">登陆</a> </center> <script type="text/javascript"> function myOrder(obj) { var url = "myOrder.jsp"; window.location.href = "viaLogin?url=" + url; } </script> </body> </html>
myOrder.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>MyOrder</title> </head> <body> <center><h1>我的订单,登陆后可见</h1></center> </body> </html>
页面效果
主页面
登录页面
我的订单页面
代码讲解
首页面包含内容
<center> This is main jsp <br><br> <a href="#" onclick="myOrder(this)">我的订单</a><br><br> <a href="login.jsp">登陆</a> </center> <script type="text/javascript"> function myOrder(obj) { var url = "myOrder.jsp"; window.location.href = "viaLogin?url=" + url; } </script>
其中点击我的订单时,请求登录验证,同时将我的订单页面的URL传递至后台。
登录验证servlet CheckLogin.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); User user = (User)session.getAttribute("user"); String url = request.getParameter("url"); if (!"".equals(url)) { session.setAttribute("url", url); } if (null != user){ request.getRequestDispatcher("myOrder.jsp").forward(request, response); } else { request.getRequestDispatcher("login.jsp").forward(request, response); } }
先获取session,然后在session中获取该user对象。
同时,将URL后面的参数项url,即本应该跳转到的页面保存在session中。
若user对象不为空,则直接跳转至我的订单myOrder.jsp页面;如果user为空,则跳转至登录页面login.jsp页面。
登录servlet LoginServlet.java
/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); User user = new User(); session.setAttribute("user", user); String url = (String) session.getAttribute("url"); if (null == url || "".equals(url)) { request.getRequestDispatcher("main.jsp").forward(request, response); } else { request.getRequestDispatcher(url).forward(request, response); } }
登录处理,new一个user出来,这里只是做示例,没有给user定义属性信息等,将user保存在session中,同时从session中获取到之前存放的url。
判断 url是否为null 或 空字符串:
如果是null或空字符串,则说明上一步的操作就是登录操作,这时候跳转至主页面main.jsp;
如果url不为null,则说明上一步应该是在访问某个页面,但是因为没有登录,被拦截到了登录页面,此时登录后,就应该继续之前的请求。
以上例子疏于整理,校验,如有不详尽之处,烦请提出,后面必改正,谢谢。
时间: 2024-10-05 06:36:13