日期:2014-05-19 浏览次数:20662 次
<%@ 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> <title>ues javaBean in jsp and servlet</title> </head> <body> <form action="customer.do" method="post"> <table border="1"> <tr> <td>客户名</td><td><input type="text" name="custName"></td> <td>邮件地址</td><td><input type="text" name="email"></td> <td>电话</td><td><input type="text" name="phone"></td> <td><input type="submit" value="submit"></td> <td><input type="reset" value="reset"></td> </tr> </table> </form> </body> </html>
package info.control; import info.model.CustomerBean; 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; /** * Servlet implementation class CustomerServlet */ public class CustomerServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //设置属性 CustomerBean customer=new CustomerBean(); customer.setCustName(request.getParameter("custName")); customer.setEmail(request.getParameter("email")); customer.setPhone(request.getParameter("phone")); // 在session作用域共享customer对象 HttpSession session=request.getSession(); synchronized(session) { session.setAttribute("customer", customer); } //转发到jsp页面 RequestDispatcher view =request.getRequestDispatcher("/displayCustomer.jsp"); view.forward(request, response); } }
package info.model; public class CustomerBean implements java.io.Serializable { /** * */ private static final long serialVersionUID = 1L; private String custName; private String email; private String phone; // 属性访问 public String getCust