java-例子:MVC模式,用jstl和el输出数据到jsp页面.
2013-09-17 11:31
585 查看
例子:利用session完成购物车.
domain:
servlet:
登陆页面:
欢迎页面:
购买页面:
domain:
package cn.itcast.cd.jsp.domain; public class User { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [name=" + name + ", password=" + password + "]"; } } package cn.itcast.cd.jsp.domain; import java.util.ArrayList; import java.util.List; public class ShoppingCar { private List<ShoppingCarItem> list = new ArrayList<ShoppingCarItem>(); public List<ShoppingCarItem> getList() { return list; } public void add(ShoppingCarItem item){ list.add(item); } public void remove(String id){ for (ShoppingCarItem item : list) { if (id.equals(item.getId())){ list.remove(item); break; } } } } package cn.itcast.cd.jsp.domain; public class ShoppingCarItem { private String id; private String name; private double price; private Integer number; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public Integer getNumber() { return number; } public void setNumber(Integer number) { this.number = number; } } package cn.itcast.cd.utils; public class Utils { public static boolean hasLength(String str) { return str != null && !"".equals(str); } }
servlet:
package cn.itcast.cd.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import cn.itcast.cd.jsp.domain.User; /** * Servlet implementation class LoginServlet */ public class LoginServlet extends HttpServlet { protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); //获取参数 String name = request.getParameter("name"); String password = request.getParameter("password"); //封装对象 User user = new User(); user.setPassword(password); user.setName(name); HttpSession session = request.getSession(); session.setAttribute("user", user); //显示到页面 RequestDispatcher dispatcher = request.getRequestDispatcher("/welcome.jsp"); dispatcher.forward(request, response); } } package cn.itcast.cd.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import cn.itcast.cd.jsp.domain.ShoppingCar; import cn.itcast.cd.jsp.domain.ShoppingCarItem; /** * Servlet implementation class BuyServlet */ public class BuyServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String name = request.getParameter("name"); String number = request.getParameter("number"); ShoppingCarItem item = new ShoppingCarItem(); item.setName(name); item.setNumber(Integer.parseInt(number)); HttpSession session = request.getSession(); ShoppingCar car = (ShoppingCar)session.getAttribute("car"); if (car == null) { //car为空,则创建一个购物车,并放到session中,否则,session中有购物车,不需要再添加到session中. car = new ShoppingCar(); session.setAttribute("car", car); } car.add(item); RequestDispatcher dispatcher = request.getRequestDispatcher("/car.jsp"); dispatcher.forward(request, response); } } package cn.itcast.cd.servlet; import java.io.IOException; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import cn.itcast.cd.jsp.domain.ShoppingCar; import cn.itcast.cd.jsp.domain.ShoppingCarItem; /** * Servlet implementation class ShowServlet */ public class ShowServlet extends HttpServlet { protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); HttpSession session = request.getSession(); ShoppingCar car = (ShoppingCar)session.getAttribute("car"); RequestDispatcher dispatcher = request.getRequestDispatcher("/car.jsp"); dispatcher.forward(request, response); } }
登陆页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!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>Insert title here</title> </head> <body> <form action="/login" method="post"> <table border="0" cellpadding="3" cellspacing="0"> <tr> <td>用户名:</td> <td><input type="text" name="name"/></td> </tr> <tr> <td>密 码:</td> <td><input type="password" name="password"/></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="登 陆"/> </td> </tr> </table> </form> </body> </html>
欢迎页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!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>Insert title here</title> </head> <body> 欢迎回来!<font color="blue"><b><i>${sessionScope.user.name}</i></bi></font><br/> <a href="/buy.jsp">开始购物</a> <a href="/show">管理购物车</a> </body> </html>
购买页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!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>Insert title here</title> </head> <body> <form action="/buy" method="post"> <table border="0" cellpadding="3" cellspacing="0"> <tr> <td>商品名称:</td> <td> <select name="name"> <option>鼠标</option> <option>键盘</option> <option>显示器</option> </select> </td> </tr> <tr> <td>数 量:</td> <td><input type="text" name="number"/></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="购买"/> </td> </tr> </table> </form> </body> </html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!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>Insert title here</title> </head> <body> <c:choose> <c:when test="${empty sessionScope.car}"> 购物车没有任何商品...... </c:when> <c:otherwise> <table> <tr> <th>商品名称</th> <th>商品数量</th> </tr> <c:forEach items="${sessionScope.car.list}" var="item"> <tr> <td>${item.name }</td> <td>${item.number }</td> </tr> </c:forEach> </table> </c:otherwise> </c:choose> <form action="/buy.jsp" method="post"> <input type ="submit" value="继续购物"/> </form> </body> </html>
相关文章推荐
- jsp页面中JSTL/EL标签引用java后台静态static字段的方法总结
- jsp页面中JSTL/EL标签引用java后台静态static字段的方法总结
- jsp页面中JSTL/EL标签引用java后台静态static字段的方法总结
- struts2 ognl jstl el java代码在jsp页面混用
- 如何在JSP页面快速输出从Servlet接收的数据_看JSTL常用C标签的威力
- JSP页面中JSTL与Java的数据交互
- 在JSP页面中输出JSON格式数据
- jsp+jstl+servlet+javaBean+Mysql用户登录注册页面
- java 向jsp页面写数据的时候出现乱码
- jstl+el表达式实现list在jsp页面的输出
- Java jsp页面中jstl标签详解
- JSTL+EL-----消灭java的JSP语言(去伪存精版)
- Java程序轻松输出到JSP页面
- JAVA生成二维码输出jsp页面
- 解决java中/jsp页面中 Double类型数据完全显示、不再以科学技术法显示
- 关于使用EL语句来接收servlet给jsp传递及jsp页面之间传递的数据
- java实现网上在线支付--06,07,08_在线支付_编写将数据提交给易宝支付的JSP页面,集成和测试向易宝发送支付请求,实现浏览器自动向易宝发送支付请求
- EL标签和JSTL标签---JSP页面的应用
- jsp页面引入jstl标签后台报java.lang.ArrayIndexOutOfBoundsException
- JAVA-JSP内置对象之out对象进行页面输出